@storybook/builder-vite 0.4.0 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/plugins/mdx-plugin.js +21 -1
- package/dist/plugins/mdx-plugin.js.map +1 -1
- package/dist/vite-config.js +14 -5
- package/dist/vite-config.js.map +1 -1
- package/package.json +10 -3
- package/plugins/mdx-plugin.ts +25 -1
- package/vite-config.ts +15 -5
|
@@ -22,9 +22,28 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
22
22
|
__setModuleDefault(result, mod);
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
25
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
29
|
exports.mdxPlugin = void 0;
|
|
30
|
+
const node_path_1 = require("node:path");
|
|
31
|
+
const slash_1 = __importDefault(require("slash"));
|
|
27
32
|
const isStorybookMdx = (id) => id.endsWith('stories.mdx') || id.endsWith('story.mdx');
|
|
33
|
+
/**
|
|
34
|
+
* Grab the mdx compiler from the @mdx-js/react that comes with @storybook/mdx1-csf,
|
|
35
|
+
* and add it to the top of the code.
|
|
36
|
+
* Equivilent to https://github.com/storybookjs/mdx1-csf/blob/d58cb032a8902b3f24ad487b6a7aae11ba8b33f6/loader.js#L12-L16
|
|
37
|
+
*/
|
|
38
|
+
function injectRenderer(code) {
|
|
39
|
+
const mdxReactPackage = (0, slash_1.default)((0, node_path_1.dirname)(require.resolve('@mdx-js/react/package.json', {
|
|
40
|
+
paths: [(0, node_path_1.dirname)(require.resolve('@storybook/mdx1-csf/package.json'))],
|
|
41
|
+
})));
|
|
42
|
+
return `
|
|
43
|
+
import { mdx } from '${mdxReactPackage}';
|
|
44
|
+
${code}
|
|
45
|
+
`;
|
|
46
|
+
}
|
|
28
47
|
/**
|
|
29
48
|
* Storybook uses two different loaders when dealing with MDX:
|
|
30
49
|
*
|
|
@@ -46,8 +65,9 @@ function mdxPlugin(options) {
|
|
|
46
65
|
: await Promise.resolve().then(() => __importStar(require('@storybook/mdx1-csf')));
|
|
47
66
|
// TODO: we don't currently support setting mdx options. Storybook 7.0 does
|
|
48
67
|
const mdxCode = String(await compile(src, { skipCsf: !isStorybookMdx(id) }));
|
|
68
|
+
const code = (features === null || features === void 0 ? void 0 : features.previewMdx2) ? mdxCode : injectRenderer(mdxCode);
|
|
49
69
|
return {
|
|
50
|
-
code
|
|
70
|
+
code,
|
|
51
71
|
map: null,
|
|
52
72
|
};
|
|
53
73
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mdx-plugin.js","sourceRoot":"","sources":["../../plugins/mdx-plugin.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"mdx-plugin.js","sourceRoot":"","sources":["../../plugins/mdx-plugin.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,yCAAoC;AAGpC,kDAA0B;AAE1B,MAAM,cAAc,GAAG,CAAC,EAAU,EAAE,EAAE,CAAC,EAAE,CAAC,QAAQ,CAAC,aAAa,CAAC,IAAI,EAAE,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;AAE9F;;;;GAIG;AACH,SAAS,cAAc,CAAC,IAAY;IAClC,MAAM,eAAe,GAAG,IAAA,eAAK,EAC3B,IAAA,mBAAO,EACL,OAAO,CAAC,OAAO,CAAC,4BAA4B,EAAE;QAC5C,KAAK,EAAE,CAAC,IAAA,mBAAO,EAAC,OAAO,CAAC,OAAO,CAAC,kCAAkC,CAAC,CAAC,CAAC;KACtE,CAAC,CACH,CACF,CAAC;IAEF,OAAO;2BACkB,eAAe;MACpC,IAAI;KACL,CAAC;AACN,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,SAAS,CAAC,OAAgB;IACxC,MAAM,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC;IAE7B,OAAO;QACL,IAAI,EAAE,2BAA2B;QACjC,OAAO,EAAE,KAAK;QACd,KAAK,CAAC,SAAS,CAAC,GAAG,EAAE,EAAE;YACrB,IAAI,EAAE,CAAC,KAAK,CAAC,SAAS,CAAC,EAAE;gBACvB,aAAa;gBACb,MAAM,EAAE,OAAO,EAAE,GAAG,CAAA,QAAQ,aAAR,QAAQ,uBAAR,QAAQ,CAAE,WAAW;oBACvC,CAAC,CAAC,wDAAa,qBAAqB,GAAC;oBACrC,CAAC,CAAC,wDAAa,qBAAqB,GAAC,CAAC;gBAExC,4EAA4E;gBAC5E,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM,OAAO,CAAC,GAAG,EAAE,EAAE,OAAO,EAAE,CAAC,cAAc,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;gBAE7E,MAAM,IAAI,GAAG,CAAA,QAAQ,aAAR,QAAQ,uBAAR,QAAQ,CAAE,WAAW,EAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;gBAEvE,OAAO;oBACL,IAAI;oBACJ,GAAG,EAAE,IAAI;iBACV,CAAC;aACH;QACH,CAAC;KACF,CAAC;AACJ,CAAC;AAzBD,8BAyBC","sourcesContent":["import { dirname } from 'node:path';\nimport type { Options } from '@storybook/core-common';\nimport { Plugin } from 'vite';\nimport slash from 'slash';\n\nconst isStorybookMdx = (id: string) => id.endsWith('stories.mdx') || id.endsWith('story.mdx');\n\n/**\n * Grab the mdx compiler from the @mdx-js/react that comes with @storybook/mdx1-csf,\n * and add it to the top of the code.\n * Equivilent to https://github.com/storybookjs/mdx1-csf/blob/d58cb032a8902b3f24ad487b6a7aae11ba8b33f6/loader.js#L12-L16\n */\nfunction injectRenderer(code: string) {\n const mdxReactPackage = slash(\n dirname(\n require.resolve('@mdx-js/react/package.json', {\n paths: [dirname(require.resolve('@storybook/mdx1-csf/package.json'))],\n })\n )\n );\n\n return `\n import { mdx } from '${mdxReactPackage}';\n ${code}\n `;\n}\n\n/**\n * Storybook uses two different loaders when dealing with MDX:\n *\n * - *stories.mdx and *story.mdx are compiled with the CSF compiler\n * - *.mdx are compiled with the MDX compiler directly\n *\n * @see https://github.com/storybookjs/storybook/blob/next/addons/docs/docs/recipes.md#csf-stories-with-arbitrary-mdx\n */\nexport function mdxPlugin(options: Options): Plugin {\n const { features } = options;\n\n return {\n name: 'storybook-vite-mdx-plugin',\n enforce: 'pre',\n async transform(src, id) {\n if (id.match(/\\.mdx?$/)) {\n // @ts-ignore\n const { compile } = features?.previewMdx2\n ? await import('@storybook/mdx2-csf')\n : await import('@storybook/mdx1-csf');\n\n // TODO: we don't currently support setting mdx options. Storybook 7.0 does\n const mdxCode = String(await compile(src, { skipCsf: !isStorybookMdx(id) }));\n\n const code = features?.previewMdx2 ? mdxCode : injectRenderer(mdxCode);\n\n return {\n code,\n map: null,\n };\n }\n },\n };\n}\n"]}
|
package/dist/vite-config.js
CHANGED
|
@@ -209,18 +209,27 @@ async function pluginConfig(options, _type) {
|
|
|
209
209
|
plugins.push(require('@preact/preset-vite').default());
|
|
210
210
|
}
|
|
211
211
|
if (framework === 'react') {
|
|
212
|
+
// First, look for plugin-react
|
|
212
213
|
try {
|
|
213
214
|
const reactPlugin = require('@vitejs/plugin-react');
|
|
214
215
|
plugins.push(reactPlugin());
|
|
215
216
|
}
|
|
216
217
|
catch (err) {
|
|
217
218
|
if (err.code === 'MODULE_NOT_FOUND') {
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
219
|
+
// Next, check if plugin-react-swc is installed, and if so, use that
|
|
220
|
+
try {
|
|
221
|
+
const reactSWCPlugin = require('@vitejs/plugin-react-swc');
|
|
222
|
+
plugins.push(reactSWCPlugin());
|
|
223
|
+
}
|
|
224
|
+
catch (err2) {
|
|
225
|
+
if (err2.code === 'MODULE_NOT_FOUND') {
|
|
226
|
+
throw new Error('@storybook/builder-vite requires @vitejs/plugin-react or @vitejs/plugin-react-swc ' +
|
|
227
|
+
'to be installed when using @storybook/react. \n' +
|
|
228
|
+
'Please install one of them and start storybook again.');
|
|
229
|
+
}
|
|
230
|
+
throw err;
|
|
231
|
+
}
|
|
222
232
|
}
|
|
223
|
-
throw err;
|
|
224
233
|
}
|
|
225
234
|
const { reactDocgen: reactDocgenOption, reactDocgenTypescriptOptions } = await presets.apply('typescript', {});
|
|
226
235
|
let typescriptPresent;
|
package/dist/vite-config.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vite-config.js","sourceRoot":"","sources":["../vite-config.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,2CAA6B;AAC7B,4CAAoB;AAGpB,+DAAuC;AAEvC,iCAAuD;AACvD,mEAA8D;AAC9D,6EAAuE;AACvE,qDAAiD;AACjD,+CAA2C;AAC3C,iEAA4D;AAO5D,SAAgB,eAAe,CAAC,MAAc,OAAO,CAAC,GAAG,EAAE;IACzD,MAAM,eAAe,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,cAAc,CAAC,CAAC;IAC1D,IAAI,CAAC,YAAE,CAAC,UAAU,CAAC,eAAe,CAAC,EAAE;QACnC,OAAO,KAAK,CAAC;KACd;IAED,MAAM,WAAW,GAAG,YAAE,CAAC,YAAY,CAAC,eAAe,EAAE,MAAM,CAAC,CAAC;IAC7D,OAAO,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;AACjC,CAAC;AARD,0CAQC;AAED,gEAAgE;AACzD,KAAK,UAAU,YAAY,CAChC,OAAwB,EACxB,KAAuB;IAEvB,MAAM,EAAE,SAAS,EAAE,GAAG,OAAO,CAAC;IAC9B,MAAM,OAAO,GAAG,SAAS,KAAK,MAAM,CAAC,CAAC,CAAC,6BAA6B,CAAC,CAAC,CAAC,qBAAqB,CAAC;IAE7F,OAAO;QACL,UAAU,EAAE,KAAK;QACjB,IAAI,EAAE,IAAI,CAAC,OAAO,CAAC,OAAO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G,WAAW,CAAC,EAAE,OAAO,EAAE,iBAAiB,KAAK,cAAc,CAAC,CAAC,CAAC,oBAAoB,CAAC,CAAC,CAAC,eAAe,EAAE,CAAC,CACxG,CAAC;SACH;KACF;IAED,IAAI,SAAS,KAAK,UAAU,EAAE;QAC5B,MAAM,MAAM,GAAG,OAAO,CAAC,gCAAgC,CAAC,CAAC;QACzD,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC,CAAC;KAChC;IAED,OAAO,OAAO,CAAC;AACjB,CAAC;AAlND,oCAkNC","sourcesContent":["import * as path from 'path';\nimport fs from 'fs';\nimport { Plugin } from 'vite';\nimport { TypescriptConfig } from '@storybook/core-common';\nimport semver from '@storybook/semver';\n\nimport { allowedEnvPrefix as envPrefix } from './envs';\nimport { codeGeneratorPlugin } from './code-generator-plugin';\nimport { injectExportOrderPlugin } from './inject-export-order-plugin';\nimport { mdxPlugin } from './plugins/mdx-plugin';\nimport { noFouc } from './plugins/no-fouc';\nimport { sourceLoaderPlugin } from './source-loader-plugin';\n\nimport type { UserConfig } from 'vite';\nimport type { ExtendedOptions } from './types';\n\nexport type PluginConfigType = 'build' | 'development';\n\nexport function readPackageJson(dir: string = process.cwd()): Record<string, any> | false {\n const packageJsonPath = path.resolve(dir, 'package.json');\n if (!fs.existsSync(packageJsonPath)) {\n return false;\n }\n\n const jsonContent = fs.readFileSync(packageJsonPath, 'utf8');\n return JSON.parse(jsonContent);\n}\n\n// Vite config that is common to development and production mode\nexport async function commonConfig(\n options: ExtendedOptions,\n _type: PluginConfigType\n): Promise<UserConfig & { configFile: false; root: string }> {\n const { framework } = options;\n const vuePath = framework === 'vue3' ? 'vue/dist/vue.esm-bundler.js' : 'vue/dist/vue.esm.js';\n\n return {\n configFile: false,\n root: path.resolve(options.configDir, '..'),\n cacheDir: 'node_modules/.vite-storybook',\n envPrefix,\n define: {},\n resolve: /^vue3?$/.test(framework)\n ? {\n alias: {\n vue: vuePath,\n },\n }\n : {},\n plugins: await pluginConfig(options, _type),\n };\n}\n\nexport async function pluginConfig(options: ExtendedOptions, _type: PluginConfigType) {\n const { framework, presets } = options;\n const svelteOptions: Record<string, any> = await presets.apply('svelteOptions', {}, options);\n const root = path.resolve(options.configDir, '..');\n\n const plugins = [\n codeGeneratorPlugin(options),\n sourceLoaderPlugin(options),\n mdxPlugin(options),\n noFouc(),\n injectExportOrderPlugin,\n {\n name: 'vite-plugin-storybook-allow',\n enforce: 'post',\n config(config) {\n // if there is no allow list then Vite allows anything in the root directory\n // if there is an allow list then Vite allows anything in the listed directories\n // add the .storybook directory only if there's an allow list so that we don't end up\n // disallowing the root directory unless it's already disallowed\n if (config?.server?.fs?.allow) {\n config.server.fs.allow.push('.storybook');\n }\n },\n },\n ] as Plugin[];\n\n if (framework === 'vue') {\n const pkgJson = readPackageJson(root);\n const vueVer: string | false | undefined = pkgJson && (pkgJson?.dependencies?.vue ?? pkgJson?.devDependencies?.vue);\n // Default to 2.7, but check if package.json has a version that is less than 2.7\n const is26 = vueVer && semver.gtr('2.7.0', vueVer);\n try {\n const vuePlugin = is26 ? require('vite-plugin-vue2').createVuePlugin : require('@vitejs/plugin-vue2');\n plugins.push(vuePlugin());\n } catch (err) {\n if ((err as NodeJS.ErrnoException).code === 'MODULE_NOT_FOUND') {\n throw new Error(`\n @storybook/builder-vite requires ${\n is26 ? 'vite-plugin-vue2' : '@vitejs/plugin-vue2'\n } to be installed when using @storybook/vue.\n Please install it and start storybook again.\n `);\n }\n throw err;\n }\n try {\n const { vueDocgen } = await import('./plugins/vue-docgen');\n plugins.push(vueDocgen(2));\n } catch (err) {\n if ((err as NodeJS.ErrnoException).code === 'MODULE_NOT_FOUND') {\n throw new Error(\n '@storybook/builder-vite requires vue-docgen-api to be installed ' +\n 'when using @storybook/vue.' +\n ' Please install it and start storybook again.'\n );\n }\n throw err;\n }\n }\n\n if (framework === 'vue3') {\n try {\n const vuePlugin = require('@vitejs/plugin-vue');\n plugins.push(vuePlugin());\n } catch (err) {\n if ((err as NodeJS.ErrnoException).code === 'MODULE_NOT_FOUND') {\n throw new Error(`\n @storybook/builder-vite requires @vitejs/plugin-vue to be installed when using @storybook/vue3.\n Please install it and start storybook again.\n `);\n }\n throw err;\n }\n\n try {\n const { vueDocgen } = await import('./plugins/vue-docgen');\n plugins.push(vueDocgen(3));\n } catch (err) {\n if ((err as NodeJS.ErrnoException).code === 'MODULE_NOT_FOUND') {\n throw new Error(\n '@storybook/builder-vite requires vue-docgen-api to be installed ' +\n 'when using @storybook/vue3.' +\n ' Please install it and start storybook again.'\n );\n }\n throw err;\n }\n }\n\n if (framework === 'svelte') {\n try {\n const sveltePlugin = require('@sveltejs/vite-plugin-svelte').svelte;\n\n // We need to create two separate svelte plugins, one for stories, and one for other svelte files\n // because stories.svelte files cannot be hot-module-reloaded.\n // Suggested in: https://github.com/sveltejs/vite-plugin-svelte/issues/321#issuecomment-1113205509\n\n // First, create an array containing user exclude patterns, to combine with ours.\n const userExclude = Array.isArray(svelteOptions?.exclude)\n ? svelteOptions?.exclude\n : svelteOptions?.exclude\n ? [svelteOptions?.exclude]\n : [];\n\n // These are the svelte stories we need to exclude from HMR\n const storyPatterns = ['**/*.story.svelte', '**/*.stories.svelte'];\n // Non-story svelte files\n // Starting in 1.0.0-next.42, svelte.config.js is included by default.\n // We disable that, but allow it to be overridden in svelteOptions\n plugins.push(sveltePlugin({ ...svelteOptions, exclude: [...userExclude, ...storyPatterns] }));\n // Svelte stories without HMR\n const storySveltePlugin = sveltePlugin({\n ...svelteOptions,\n exclude: userExclude,\n include: storyPatterns,\n hot: false,\n });\n plugins.push({\n // Starting in 1.0.0-next.43, the plugin function returns an array of plugins. We only want the first one here.\n ...(Array.isArray(storySveltePlugin) ? storySveltePlugin[0] : storySveltePlugin),\n name: 'vite-plugin-svelte-stories',\n });\n } catch (err) {\n if ((err as NodeJS.ErrnoException).code === 'MODULE_NOT_FOUND') {\n throw new Error(\n '@storybook/builder-vite requires @sveltejs/vite-plugin-svelte to be installed' +\n ' when using @storybook/svelte.' +\n ' Please install it and start storybook again.'\n );\n }\n throw err;\n }\n\n const { loadSvelteConfig } = require('@sveltejs/vite-plugin-svelte');\n const config = { ...loadSvelteConfig(), ...svelteOptions };\n\n try {\n const csfPlugin = require('./svelte/csf-plugin').default;\n plugins.push(csfPlugin(config));\n } catch (err) {\n // Not all projects use `.stories.svelte` for stories, and by default 6.5+ does not auto-install @storybook/addon-svelte-csf.\n // If it's any other kind of error, re-throw.\n if ((err as NodeJS.ErrnoException).code !== 'MODULE_NOT_FOUND') {\n throw err;\n }\n }\n\n const { svelteDocgen } = await import('./plugins/svelte-docgen');\n plugins.push(svelteDocgen(config));\n }\n\n if (framework === 'preact') {\n plugins.push(require('@preact/preset-vite').default());\n }\n\n if (framework === 'react') {\n try {\n const reactPlugin = require('@vitejs/plugin-react');\n plugins.push(reactPlugin());\n } catch (err) {\n if ((err as NodeJS.ErrnoException).code === 'MODULE_NOT_FOUND') {\n throw new Error(`\n @storybook/builder-vite requires @vitejs/plugin-react to be installed when using @storybook/react.\n Please install it and start storybook again.\n `);\n }\n throw err;\n }\n\n const { reactDocgen: reactDocgenOption, reactDocgenTypescriptOptions } = await presets.apply(\n 'typescript',\n {} as TypescriptConfig\n );\n\n let typescriptPresent;\n\n try {\n require.resolve('typescript');\n typescriptPresent = true;\n } catch (e) {\n typescriptPresent = false;\n }\n\n if (reactDocgenOption === 'react-docgen-typescript' && typescriptPresent) {\n plugins.push(\n require('@joshwooding/vite-plugin-react-docgen-typescript')({\n ...reactDocgenTypescriptOptions,\n // We *need* this set so that RDT returns default values in the same format as react-docgen\n savePropValueAsString: true,\n })\n );\n }\n\n // Add react-docgen so long as the option is not false\n if (typeof reactDocgenOption === 'string') {\n const { reactDocgen } = await import('./plugins/react-docgen');\n // Needs to run before the react plugin, so add to the front\n plugins.unshift(\n // If react-docgen is specified, use it for everything, otherwise only use it for non-typescript files\n reactDocgen({ include: reactDocgenOption === 'react-docgen' ? /\\.(mjs|tsx?|jsx?)$/ : /\\.(mjs|jsx?)$/ })\n );\n }\n }\n\n if (framework === 'glimmerx') {\n const plugin = require('vite-plugin-glimmerx/index.cjs');\n plugins.push(plugin.default());\n }\n\n return plugins;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"vite-config.js","sourceRoot":"","sources":["../vite-config.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,2CAA6B;AAC7B,4CAAoB;AAGpB,+DAAuC;AAEvC,iCAAuD;AACvD,mEAA8D;AAC9D,6EAAuE;AACvE,qDAAiD;AACjD,+CAA2C;AAC3C,iEAA4D;AAO5D,SAAgB,eAAe,CAAC,MAAc,OAAO,CAAC,GAAG,EAAE;IACzD,MAAM,eAAe,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,cAAc,CAAC,CAAC;IAC1D,IAAI,CAAC,YAAE,CAAC,UAAU,CAAC,eAAe,CAAC,EAAE;QACnC,OAAO,KAAK,CAAC;KACd;IAED,MAAM,WAAW,GAAG,YAAE,CAAC,YAAY,CAAC,eAAe,EAAE,MAAM,CAAC,CAAC;IAC7D,OAAO,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;AACjC,CAAC;AARD,0CAQC;AAED,gEAAgE;AACzD,KAAK,UAAU,YAAY,CAChC,OAAwB,EACxB,KAAuB;IAEvB,MAAM,EAAE,SAAS,EAAE,GAAG,OAAO,CAAC;IAC9B,MAAM,OAAO,GAAG,SAAS,KAAK,MAAM,CAAC,CAAC,CAAC,6BAA6B,CAAC,CAAC,CAAC,qBAAqB,CAAC;IAE7F,OAAO;QACL,UAAU,EAAE,KAAK;QACjB,IAAI,EAAE,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,SAAS,EAAE,IAAI,CAAC;QAC3C,QAAQ,EAAE,8BAA8B;QACxC,SAAS,EAAT,uBAAS;QACT,MAAM,EAAE,EAAE;QACV,OAAO,EAAE,SAAS,CAAC,IAAI,CAAC,SAAS,CAAC;YAChC,CAAC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AAM,EAAE,WAAW,EAAE,GAAG,wDAAa,wBAAwB,GAAC,CAAC;YAC/D,4DAA4D;YAC5D,OAAO,CAAC,OAAO;YACb,sGAAsG;YACtG,WAAW,CAAC,EAAE,OAAO,EAAE,iBAAiB,KAAK,cAAc,CAAC,CAAC,CAAC,oBAAoB,CAAC,CAAC,CAAC,eAAe,EAAE,CAAC,CACxG,CAAC;SACH;KACF;IAED,IAAI,SAAS,KAAK,UAAU,EAAE;QAC5B,MAAM,MAAM,GAAG,OAAO,CAAC,gCAAgC,CAAC,CAAC;QACzD,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC,CAAC;KAChC;IAED,OAAO,OAAO,CAAC;AACjB,CAAC;AA5ND,oCA4NC","sourcesContent":["import * as path from 'path';\nimport fs from 'fs';\nimport { Plugin } from 'vite';\nimport { TypescriptConfig } from '@storybook/core-common';\nimport semver from '@storybook/semver';\n\nimport { allowedEnvPrefix as envPrefix } from './envs';\nimport { codeGeneratorPlugin } from './code-generator-plugin';\nimport { injectExportOrderPlugin } from './inject-export-order-plugin';\nimport { mdxPlugin } from './plugins/mdx-plugin';\nimport { noFouc } from './plugins/no-fouc';\nimport { sourceLoaderPlugin } from './source-loader-plugin';\n\nimport type { UserConfig } from 'vite';\nimport type { ExtendedOptions } from './types';\n\nexport type PluginConfigType = 'build' | 'development';\n\nexport function readPackageJson(dir: string = process.cwd()): Record<string, any> | false {\n const packageJsonPath = path.resolve(dir, 'package.json');\n if (!fs.existsSync(packageJsonPath)) {\n return false;\n }\n\n const jsonContent = fs.readFileSync(packageJsonPath, 'utf8');\n return JSON.parse(jsonContent);\n}\n\n// Vite config that is common to development and production mode\nexport async function commonConfig(\n options: ExtendedOptions,\n _type: PluginConfigType\n): Promise<UserConfig & { configFile: false; root: string }> {\n const { framework } = options;\n const vuePath = framework === 'vue3' ? 'vue/dist/vue.esm-bundler.js' : 'vue/dist/vue.esm.js';\n\n return {\n configFile: false,\n root: path.resolve(options.configDir, '..'),\n cacheDir: 'node_modules/.vite-storybook',\n envPrefix,\n define: {},\n resolve: /^vue3?$/.test(framework)\n ? {\n alias: {\n vue: vuePath,\n },\n }\n : {},\n plugins: await pluginConfig(options, _type),\n };\n}\n\nexport async function pluginConfig(options: ExtendedOptions, _type: PluginConfigType) {\n const { framework, presets } = options;\n const svelteOptions: Record<string, any> = await presets.apply('svelteOptions', {}, options);\n const root = path.resolve(options.configDir, '..');\n\n const plugins = [\n codeGeneratorPlugin(options),\n sourceLoaderPlugin(options),\n mdxPlugin(options),\n noFouc(),\n injectExportOrderPlugin,\n {\n name: 'vite-plugin-storybook-allow',\n enforce: 'post',\n config(config) {\n // if there is no allow list then Vite allows anything in the root directory\n // if there is an allow list then Vite allows anything in the listed directories\n // add the .storybook directory only if there's an allow list so that we don't end up\n // disallowing the root directory unless it's already disallowed\n if (config?.server?.fs?.allow) {\n config.server.fs.allow.push('.storybook');\n }\n },\n },\n ] as Plugin[];\n\n if (framework === 'vue') {\n const pkgJson = readPackageJson(root);\n const vueVer: string | false | undefined = pkgJson && (pkgJson?.dependencies?.vue ?? pkgJson?.devDependencies?.vue);\n // Default to 2.7, but check if package.json has a version that is less than 2.7\n const is26 = vueVer && semver.gtr('2.7.0', vueVer);\n try {\n const vuePlugin = is26 ? require('vite-plugin-vue2').createVuePlugin : require('@vitejs/plugin-vue2');\n plugins.push(vuePlugin());\n } catch (err) {\n if ((err as NodeJS.ErrnoException).code === 'MODULE_NOT_FOUND') {\n throw new Error(`\n @storybook/builder-vite requires ${\n is26 ? 'vite-plugin-vue2' : '@vitejs/plugin-vue2'\n } to be installed when using @storybook/vue.\n Please install it and start storybook again.\n `);\n }\n throw err;\n }\n try {\n const { vueDocgen } = await import('./plugins/vue-docgen');\n plugins.push(vueDocgen(2));\n } catch (err) {\n if ((err as NodeJS.ErrnoException).code === 'MODULE_NOT_FOUND') {\n throw new Error(\n '@storybook/builder-vite requires vue-docgen-api to be installed ' +\n 'when using @storybook/vue.' +\n ' Please install it and start storybook again.'\n );\n }\n throw err;\n }\n }\n\n if (framework === 'vue3') {\n try {\n const vuePlugin = require('@vitejs/plugin-vue');\n plugins.push(vuePlugin());\n } catch (err) {\n if ((err as NodeJS.ErrnoException).code === 'MODULE_NOT_FOUND') {\n throw new Error(`\n @storybook/builder-vite requires @vitejs/plugin-vue to be installed when using @storybook/vue3.\n Please install it and start storybook again.\n `);\n }\n throw err;\n }\n\n try {\n const { vueDocgen } = await import('./plugins/vue-docgen');\n plugins.push(vueDocgen(3));\n } catch (err) {\n if ((err as NodeJS.ErrnoException).code === 'MODULE_NOT_FOUND') {\n throw new Error(\n '@storybook/builder-vite requires vue-docgen-api to be installed ' +\n 'when using @storybook/vue3.' +\n ' Please install it and start storybook again.'\n );\n }\n throw err;\n }\n }\n\n if (framework === 'svelte') {\n try {\n const sveltePlugin = require('@sveltejs/vite-plugin-svelte').svelte;\n\n // We need to create two separate svelte plugins, one for stories, and one for other svelte files\n // because stories.svelte files cannot be hot-module-reloaded.\n // Suggested in: https://github.com/sveltejs/vite-plugin-svelte/issues/321#issuecomment-1113205509\n\n // First, create an array containing user exclude patterns, to combine with ours.\n const userExclude = Array.isArray(svelteOptions?.exclude)\n ? svelteOptions?.exclude\n : svelteOptions?.exclude\n ? [svelteOptions?.exclude]\n : [];\n\n // These are the svelte stories we need to exclude from HMR\n const storyPatterns = ['**/*.story.svelte', '**/*.stories.svelte'];\n // Non-story svelte files\n // Starting in 1.0.0-next.42, svelte.config.js is included by default.\n // We disable that, but allow it to be overridden in svelteOptions\n plugins.push(sveltePlugin({ ...svelteOptions, exclude: [...userExclude, ...storyPatterns] }));\n // Svelte stories without HMR\n const storySveltePlugin = sveltePlugin({\n ...svelteOptions,\n exclude: userExclude,\n include: storyPatterns,\n hot: false,\n });\n plugins.push({\n // Starting in 1.0.0-next.43, the plugin function returns an array of plugins. We only want the first one here.\n ...(Array.isArray(storySveltePlugin) ? storySveltePlugin[0] : storySveltePlugin),\n name: 'vite-plugin-svelte-stories',\n });\n } catch (err) {\n if ((err as NodeJS.ErrnoException).code === 'MODULE_NOT_FOUND') {\n throw new Error(\n '@storybook/builder-vite requires @sveltejs/vite-plugin-svelte to be installed' +\n ' when using @storybook/svelte.' +\n ' Please install it and start storybook again.'\n );\n }\n throw err;\n }\n\n const { loadSvelteConfig } = require('@sveltejs/vite-plugin-svelte');\n const config = { ...loadSvelteConfig(), ...svelteOptions };\n\n try {\n const csfPlugin = require('./svelte/csf-plugin').default;\n plugins.push(csfPlugin(config));\n } catch (err) {\n // Not all projects use `.stories.svelte` for stories, and by default 6.5+ does not auto-install @storybook/addon-svelte-csf.\n // If it's any other kind of error, re-throw.\n if ((err as NodeJS.ErrnoException).code !== 'MODULE_NOT_FOUND') {\n throw err;\n }\n }\n\n const { svelteDocgen } = await import('./plugins/svelte-docgen');\n plugins.push(svelteDocgen(config));\n }\n\n if (framework === 'preact') {\n plugins.push(require('@preact/preset-vite').default());\n }\n\n if (framework === 'react') {\n // First, look for plugin-react\n try {\n const reactPlugin = require('@vitejs/plugin-react');\n plugins.push(reactPlugin());\n } catch (err) {\n if ((err as NodeJS.ErrnoException).code === 'MODULE_NOT_FOUND') {\n // Next, check if plugin-react-swc is installed, and if so, use that\n try {\n const reactSWCPlugin = require('@vitejs/plugin-react-swc');\n plugins.push(reactSWCPlugin());\n } catch (err2) {\n if ((err2 as NodeJS.ErrnoException).code === 'MODULE_NOT_FOUND') {\n throw new Error(\n '@storybook/builder-vite requires @vitejs/plugin-react or @vitejs/plugin-react-swc ' +\n 'to be installed when using @storybook/react. \\n' +\n 'Please install one of them and start storybook again.'\n );\n }\n throw err;\n }\n }\n }\n\n const { reactDocgen: reactDocgenOption, reactDocgenTypescriptOptions } = await presets.apply(\n 'typescript',\n {} as TypescriptConfig\n );\n\n let typescriptPresent;\n\n try {\n require.resolve('typescript');\n typescriptPresent = true;\n } catch (e) {\n typescriptPresent = false;\n }\n\n if (reactDocgenOption === 'react-docgen-typescript' && typescriptPresent) {\n plugins.push(\n require('@joshwooding/vite-plugin-react-docgen-typescript')({\n ...reactDocgenTypescriptOptions,\n // We *need* this set so that RDT returns default values in the same format as react-docgen\n savePropValueAsString: true,\n })\n );\n }\n\n // Add react-docgen so long as the option is not false\n if (typeof reactDocgenOption === 'string') {\n const { reactDocgen } = await import('./plugins/react-docgen');\n // Needs to run before the react plugin, so add to the front\n plugins.unshift(\n // If react-docgen is specified, use it for everything, otherwise only use it for non-typescript files\n reactDocgen({ include: reactDocgenOption === 'react-docgen' ? /\\.(mjs|tsx?|jsx?)$/ : /\\.(mjs|jsx?)$/ })\n );\n }\n }\n\n if (framework === 'glimmerx') {\n const plugin = require('vite-plugin-glimmerx/index.cjs');\n plugins.push(plugin.default());\n }\n\n return plugins;\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@storybook/builder-vite",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.2",
|
|
4
4
|
"description": "A plugin to run and build Storybooks with Vite",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -22,12 +22,12 @@
|
|
|
22
22
|
"lint-ci": "yarn lint-ci:prettier && yarn lint-ci:eslint",
|
|
23
23
|
"lint-ci:prettier": "prettier . --check",
|
|
24
24
|
"lint-ci:eslint": "eslint \"**/*.{ts,tsx,js,jsx,mjs,cjs}\" --max-warnings=0",
|
|
25
|
-
"update-examples": "cd ../../examples/lit-ts && yarn && cd ../preact && yarn && cd ../react && cd ../react-18 && yarn && cd ../react-ts && yarn && cd ../svelte && yarn && cd ../vue2.6 && yarn && cd ../vue2.7 && yarn && cd ../vue3 && yarn && cd ../workspaces && yarn"
|
|
25
|
+
"update-examples": "cd ../../examples/lit-ts && yarn && cd ../preact && yarn && cd ../react && yarn && cd ../react-18 && yarn && cd ../react-ts && yarn && cd ../svelte && yarn && cd ../vue2.6 && yarn && cd ../vue2.7 && yarn && cd ../vue3 && yarn && cd ../workspaces && yarn"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"@joshwooding/vite-plugin-react-docgen-typescript": "0.2.1",
|
|
29
29
|
"@storybook/core-common": "^6.4.3",
|
|
30
|
-
"@storybook/mdx1-csf": "
|
|
30
|
+
"@storybook/mdx1-csf": "1.0.0-next.0",
|
|
31
31
|
"@storybook/node-logger": "^6.4.3",
|
|
32
32
|
"@storybook/semver": "^7.3.2",
|
|
33
33
|
"@storybook/source-loader": "^6.4.3",
|
|
@@ -63,6 +63,7 @@
|
|
|
63
63
|
"@storybook/mdx2-csf": ">=1.0.0-next.0",
|
|
64
64
|
"@sveltejs/vite-plugin-svelte": "^2.0.0",
|
|
65
65
|
"@vitejs/plugin-react": "^3.0.0",
|
|
66
|
+
"@vitejs/plugin-react-swc": "^3.0.0",
|
|
66
67
|
"@vitejs/plugin-vue": "^4.0.0",
|
|
67
68
|
"vite": ">= 4.0.0",
|
|
68
69
|
"vue-docgen-api": "^4.40.0"
|
|
@@ -74,6 +75,12 @@
|
|
|
74
75
|
"@sveltejs/vite-plugin-svelte": {
|
|
75
76
|
"optional": true
|
|
76
77
|
},
|
|
78
|
+
"@vitejs/plugin-react": {
|
|
79
|
+
"optional": true
|
|
80
|
+
},
|
|
81
|
+
"@vitejs/plugin-react-swc": {
|
|
82
|
+
"optional": true
|
|
83
|
+
},
|
|
77
84
|
"@vitejs/plugin-vue": {
|
|
78
85
|
"optional": true
|
|
79
86
|
},
|
package/plugins/mdx-plugin.ts
CHANGED
|
@@ -1,8 +1,30 @@
|
|
|
1
|
+
import { dirname } from 'node:path';
|
|
1
2
|
import type { Options } from '@storybook/core-common';
|
|
2
3
|
import { Plugin } from 'vite';
|
|
4
|
+
import slash from 'slash';
|
|
3
5
|
|
|
4
6
|
const isStorybookMdx = (id: string) => id.endsWith('stories.mdx') || id.endsWith('story.mdx');
|
|
5
7
|
|
|
8
|
+
/**
|
|
9
|
+
* Grab the mdx compiler from the @mdx-js/react that comes with @storybook/mdx1-csf,
|
|
10
|
+
* and add it to the top of the code.
|
|
11
|
+
* Equivilent to https://github.com/storybookjs/mdx1-csf/blob/d58cb032a8902b3f24ad487b6a7aae11ba8b33f6/loader.js#L12-L16
|
|
12
|
+
*/
|
|
13
|
+
function injectRenderer(code: string) {
|
|
14
|
+
const mdxReactPackage = slash(
|
|
15
|
+
dirname(
|
|
16
|
+
require.resolve('@mdx-js/react/package.json', {
|
|
17
|
+
paths: [dirname(require.resolve('@storybook/mdx1-csf/package.json'))],
|
|
18
|
+
})
|
|
19
|
+
)
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
return `
|
|
23
|
+
import { mdx } from '${mdxReactPackage}';
|
|
24
|
+
${code}
|
|
25
|
+
`;
|
|
26
|
+
}
|
|
27
|
+
|
|
6
28
|
/**
|
|
7
29
|
* Storybook uses two different loaders when dealing with MDX:
|
|
8
30
|
*
|
|
@@ -27,8 +49,10 @@ export function mdxPlugin(options: Options): Plugin {
|
|
|
27
49
|
// TODO: we don't currently support setting mdx options. Storybook 7.0 does
|
|
28
50
|
const mdxCode = String(await compile(src, { skipCsf: !isStorybookMdx(id) }));
|
|
29
51
|
|
|
52
|
+
const code = features?.previewMdx2 ? mdxCode : injectRenderer(mdxCode);
|
|
53
|
+
|
|
30
54
|
return {
|
|
31
|
-
code
|
|
55
|
+
code,
|
|
32
56
|
map: null,
|
|
33
57
|
};
|
|
34
58
|
}
|
package/vite-config.ts
CHANGED
|
@@ -207,17 +207,27 @@ export async function pluginConfig(options: ExtendedOptions, _type: PluginConfig
|
|
|
207
207
|
}
|
|
208
208
|
|
|
209
209
|
if (framework === 'react') {
|
|
210
|
+
// First, look for plugin-react
|
|
210
211
|
try {
|
|
211
212
|
const reactPlugin = require('@vitejs/plugin-react');
|
|
212
213
|
plugins.push(reactPlugin());
|
|
213
214
|
} catch (err) {
|
|
214
215
|
if ((err as NodeJS.ErrnoException).code === 'MODULE_NOT_FOUND') {
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
216
|
+
// Next, check if plugin-react-swc is installed, and if so, use that
|
|
217
|
+
try {
|
|
218
|
+
const reactSWCPlugin = require('@vitejs/plugin-react-swc');
|
|
219
|
+
plugins.push(reactSWCPlugin());
|
|
220
|
+
} catch (err2) {
|
|
221
|
+
if ((err2 as NodeJS.ErrnoException).code === 'MODULE_NOT_FOUND') {
|
|
222
|
+
throw new Error(
|
|
223
|
+
'@storybook/builder-vite requires @vitejs/plugin-react or @vitejs/plugin-react-swc ' +
|
|
224
|
+
'to be installed when using @storybook/react. \n' +
|
|
225
|
+
'Please install one of them and start storybook again.'
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
throw err;
|
|
229
|
+
}
|
|
219
230
|
}
|
|
220
|
-
throw err;
|
|
221
231
|
}
|
|
222
232
|
|
|
223
233
|
const { reactDocgen: reactDocgenOption, reactDocgenTypescriptOptions } = await presets.apply(
|