@storybook/builder-vite 11.0.0-alpha.6 → 11.0.0-alpha.8

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.
@@ -1,10 +1,10 @@
1
- import CJS_COMPAT_NODE_URL_zwca6w95nf from 'node:url';
2
- import CJS_COMPAT_NODE_PATH_zwca6w95nf from 'node:path';
3
- import CJS_COMPAT_NODE_MODULE_zwca6w95nf from "node:module";
1
+ import CJS_COMPAT_NODE_URL_dm9z9i8b6a from 'node:url';
2
+ import CJS_COMPAT_NODE_PATH_dm9z9i8b6a from 'node:path';
3
+ import CJS_COMPAT_NODE_MODULE_dm9z9i8b6a from "node:module";
4
4
 
5
- var __filename = CJS_COMPAT_NODE_URL_zwca6w95nf.fileURLToPath(import.meta.url);
6
- var __dirname = CJS_COMPAT_NODE_PATH_zwca6w95nf.dirname(__filename);
7
- var require = CJS_COMPAT_NODE_MODULE_zwca6w95nf.createRequire(import.meta.url);
5
+ var __filename = CJS_COMPAT_NODE_URL_dm9z9i8b6a.fileURLToPath(import.meta.url);
6
+ var __dirname = CJS_COMPAT_NODE_PATH_dm9z9i8b6a.dirname(__filename);
7
+ var require = CJS_COMPAT_NODE_MODULE_dm9z9i8b6a.createRequire(import.meta.url);
8
8
 
9
9
  // ------------------------------------------------------------
10
10
  // end of CJS compatibility banner, injected by Storybook's esbuild configuration
@@ -1261,7 +1261,7 @@ function storybookConfigPlugin(options) {
1261
1261
 
1262
1262
  // src/plugins/storybook-optimize-deps-plugin.ts
1263
1263
  import { loadPreviewOrConfigFile } from "storybook/internal/common";
1264
- import { babelParser, extractMockCalls, findMockRedirect } from "storybook/internal/mocking-utils";
1264
+ import { extractMockCalls, findMockRedirect } from "storybook/internal/mocking-utils";
1265
1265
 
1266
1266
  // node_modules/pathe/dist/shared/pathe.ff20891b.mjs
1267
1267
  var _DRIVE_LETTER_START_RE = /^[A-Za-z]:\//;
@@ -1382,7 +1382,6 @@ function storybookOptimizeDepsPlugin(options) {
1382
1382
  coreOptions: { disableTelemetry: !0 },
1383
1383
  configDir: options.configDir
1384
1384
  },
1385
- babelParser,
1386
1385
  projectRoot,
1387
1386
  findMockRedirect
1388
1387
  )
@@ -1735,7 +1734,6 @@ var ENTRY_PATH = "/vite-inject-mocker-entry.js", viteInjectMockerRuntime = (opti
1735
1734
  // src/plugins/vite-mock/plugin.ts
1736
1735
  import { readFileSync } from "node:fs";
1737
1736
  import {
1738
- babelParser as babelParser2,
1739
1737
  extractMockCalls as extractMockCalls2,
1740
1738
  findMockRedirect as findMockRedirect2,
1741
1739
  getAutomockCode,
@@ -1763,13 +1761,13 @@ function viteMockPlugin(options) {
1763
1761
  viteConfig = config;
1764
1762
  },
1765
1763
  buildStart() {
1766
- mockCalls = extractMockCalls2(options, babelParser2, viteConfig.root, findMockRedirect2);
1764
+ mockCalls = extractMockCalls2(options, viteConfig.root, findMockRedirect2);
1767
1765
  },
1768
1766
  configureServer(server) {
1769
1767
  async function invalidateAffectedFiles(file) {
1770
1768
  if (file === options.previewConfigPath || file.includes("__mocks__")) {
1771
1769
  let oldMockCalls = mockCalls;
1772
- mockCalls = extractMockCalls2(options, babelParser2, viteConfig.root, findMockRedirect2);
1770
+ mockCalls = extractMockCalls2(options, viteConfig.root, findMockRedirect2);
1773
1771
  let previewMod = server.moduleGraph.getModuleById(options.previewConfigPath);
1774
1772
  previewMod && server.moduleGraph.invalidateModule(previewMod);
1775
1773
  for (let call of mockCalls)
@@ -1807,7 +1805,7 @@ function viteMockPlugin(options) {
1807
1805
  }
1808
1806
  try {
1809
1807
  if (!call.redirectPath) {
1810
- let automockedCode = getAutomockCode(code, call.spy, babelParser2);
1808
+ let automockedCode = getAutomockCode(code, call.spy, id);
1811
1809
  return {
1812
1810
  code: automockedCode.toString(),
1813
1811
  map: automockedCode.generateMap().toString()
@@ -1828,7 +1826,7 @@ function viteMockPlugin(options) {
1828
1826
  handler(code, id) {
1829
1827
  if (id === normalizedPreviewConfigPath)
1830
1828
  try {
1831
- return rewriteSbMockImportCalls(code);
1829
+ return rewriteSbMockImportCalls(code, id);
1832
1830
  } catch (e) {
1833
1831
  return logger.debug(`Could not transform sb.mock(import(...)) calls in ${id}: ${e}`), null;
1834
1832
  }
@@ -1845,8 +1843,56 @@ var builderRequire = createRequire(import.meta.url), corePath = dirname(builderR
1845
1843
 
1846
1844
  // src/vite-config.ts
1847
1845
  import { existsSync as existsSync4 } from "node:fs";
1848
- import { resolve as resolve4 } from "node:path";
1849
- import { getBuilderOptions, resolvePathInStorybookCache } from "storybook/internal/common";
1846
+ import { relative as relative4, resolve as resolve4 } from "node:path";
1847
+ import {
1848
+ getBuilderOptions,
1849
+ getProjectRoot,
1850
+ resolvePathInStorybookCache
1851
+ } from "storybook/internal/common";
1852
+ import { once } from "storybook/internal/node-logger";
1853
+
1854
+ // ../../../node_modules/empathic/find.mjs
1855
+ import { join as join3 } from "node:path";
1856
+ import { existsSync, statSync } from "node:fs";
1857
+
1858
+ // ../../../node_modules/empathic/walk.mjs
1859
+ import { dirname as dirname2 } from "node:path";
1860
+
1861
+ // ../../../node_modules/empathic/resolve.mjs
1862
+ import { isAbsolute as isAbsolute2, join as join2, resolve as resolve2 } from "node:path";
1863
+ function absolute(input, root) {
1864
+ return isAbsolute2(input) ? input : resolve2(root || ".", input);
1865
+ }
1866
+
1867
+ // ../../../node_modules/empathic/walk.mjs
1868
+ function up(base, options) {
1869
+ let { last, cwd: cwd2 } = options || {}, tmp = absolute(base, cwd2), root = absolute(last || "/", cwd2), prev, arr = [];
1870
+ for (; prev !== root && (arr.push(tmp), tmp = dirname2(prev = tmp), tmp !== prev); )
1871
+ ;
1872
+ return arr;
1873
+ }
1874
+
1875
+ // ../../../node_modules/empathic/find.mjs
1876
+ function up2(name, options) {
1877
+ let dir, tmp, start = options && options.cwd || "";
1878
+ for (dir of up(start, options))
1879
+ if (tmp = join3(dir, name), existsSync(tmp)) return tmp;
1880
+ }
1881
+ function any(names, options) {
1882
+ let dir, start = options && options.cwd || "", j = 0, len = names.length, tmp, stats, type = options?.type, kind = type ? type === "dir" ? 1 : -1 : 0;
1883
+ for (dir of up(start, options))
1884
+ for (j = 0; j < len; j++)
1885
+ if (tmp = join3(dir, names[j]), kind)
1886
+ try {
1887
+ if (stats = statSync(tmp), kind === 1 ? stats.isDirectory() : stats.isFile()) return tmp;
1888
+ } catch {
1889
+ }
1890
+ else if (existsSync(tmp))
1891
+ return tmp;
1892
+ }
1893
+
1894
+ // src/vite-config.ts
1895
+ import { dedent as dedent4 } from "ts-dedent";
1850
1896
 
1851
1897
  // src/plugins/code-generator-plugin.ts
1852
1898
  import { readFileSync as readFileSync2 } from "node:fs";
@@ -2091,6 +2137,7 @@ async function transformIframeHtml(html, options) {
2091
2137
  }
2092
2138
 
2093
2139
  // src/utils/vite-features.ts
2140
+ import { basename as basename3 } from "node:path";
2094
2141
  import { version } from "vite";
2095
2142
  var shouldUseRolldownOptions = () => {
2096
2143
  try {
@@ -2106,6 +2153,35 @@ function ensureRolldownOptions(config) {
2106
2153
  let rolldown = config.build.rolldownOptions ??= {}, output = rolldown.output ??= {};
2107
2154
  output.strictExecutionOrder = !0;
2108
2155
  }
2156
+ var MAX_CHUNK_BYTES = 500 * 1024, PREVIEW_CHUNK = /[/\\]preview[/\\]_chunks[/\\]/;
2157
+ function eachOutput(config, useRolldown, fn) {
2158
+ config.build ??= {};
2159
+ let key = useRolldown ? "rolldownOptions" : "rollupOptions", build = config.build, options = build[key] ??= {};
2160
+ if (Array.isArray(options.output)) {
2161
+ options.output.length === 0 && options.output.push({});
2162
+ for (let output of options.output)
2163
+ fn(output);
2164
+ return;
2165
+ }
2166
+ options.output ??= {}, fn(options.output);
2167
+ }
2168
+ function applyChunkedPreviewRuntime(config, useRolldown = shouldUseRolldownOptions()) {
2169
+ eachOutput(config, useRolldown, (output) => {
2170
+ if (useRolldown) {
2171
+ output.strictExecutionOrder = !0;
2172
+ let splitting = output.codeSplitting, base = splitting && typeof splitting == "object" ? splitting : {};
2173
+ output.codeSplitting = {
2174
+ ...base,
2175
+ groups: [...base.groups ?? [], { name: "sb", maxSize: MAX_CHUNK_BYTES }]
2176
+ };
2177
+ return;
2178
+ }
2179
+ if (output.manualChunks && typeof output.manualChunks != "function")
2180
+ return;
2181
+ let previous = output.manualChunks;
2182
+ output.manualChunks = (id, meta) => PREVIEW_CHUNK.test(id) ? basename3(id, ".js") : previous?.(id, meta);
2183
+ });
2184
+ }
2109
2185
 
2110
2186
  // src/plugins/code-generator-plugin.ts
2111
2187
  function codeGeneratorPlugin(options) {
@@ -2128,7 +2204,7 @@ function codeGeneratorPlugin(options) {
2128
2204
  build[bundlerOptionsKey] = {
2129
2205
  ...build[bundlerOptionsKey],
2130
2206
  input: iframePath
2131
- }, ensureRolldownOptions(config);
2207
+ }, ensureRolldownOptions(config), options.features?.experimentalChunkedPreviewRuntime && applyChunkedPreviewRuntime(config);
2132
2208
  }
2133
2209
  },
2134
2210
  configResolved(config) {
@@ -3416,34 +3492,6 @@ function writable(path3) {
3416
3492
  return ok(path3, constants.W_OK);
3417
3493
  }
3418
3494
 
3419
- // ../../../node_modules/empathic/find.mjs
3420
- import { join as join4 } from "node:path";
3421
- import { existsSync, statSync } from "node:fs";
3422
-
3423
- // ../../../node_modules/empathic/walk.mjs
3424
- import { dirname as dirname3 } from "node:path";
3425
-
3426
- // ../../../node_modules/empathic/resolve.mjs
3427
- import { isAbsolute as isAbsolute3, join as join3, resolve as resolve3 } from "node:path";
3428
- function absolute(input, root) {
3429
- return isAbsolute3(input) ? input : resolve3(root || ".", input);
3430
- }
3431
-
3432
- // ../../../node_modules/empathic/walk.mjs
3433
- function up(base, options) {
3434
- let { last, cwd: cwd2 } = options || {}, tmp = absolute(base, cwd2), root = absolute(last || "/", cwd2), prev, arr = [];
3435
- for (; prev !== root && (arr.push(tmp), tmp = dirname3(prev = tmp), tmp !== prev); )
3436
- ;
3437
- return arr;
3438
- }
3439
-
3440
- // ../../../node_modules/empathic/find.mjs
3441
- function up2(name, options) {
3442
- let dir, tmp, start = options && options.cwd || "";
3443
- for (dir of up(start, options))
3444
- if (tmp = join4(dir, name), existsSync(tmp)) return tmp;
3445
- }
3446
-
3447
3495
  // ../../../node_modules/empathic/package.mjs
3448
3496
  function up3(options) {
3449
3497
  return up2("package.json", options);
@@ -3505,21 +3553,29 @@ async function storybookExternalGlobalsPlugin(options) {
3505
3553
  async handler(code, id) {
3506
3554
  if (globalsList.every((glob) => !code.includes(glob)))
3507
3555
  return;
3508
- let [imports] = parse3(code), src = new MagicString(code);
3509
- return imports.forEach(({ n: path3, ss: startPosition, se: endPosition }) => {
3556
+ let [imports] = parse3(code), src = new MagicString(code), didRewrite = !1;
3557
+ imports.forEach(({ n: path3, ss: startPosition, se: endPosition }) => {
3510
3558
  let packageName = path3;
3511
3559
  if (packageName && globalsList.includes(packageName)) {
3512
3560
  let importStatement = src.slice(startPosition, endPosition), transformedImport = rewriteImport(importStatement, externals, packageName);
3513
- src.update(startPosition, endPosition, transformedImport);
3561
+ src.update(startPosition, endPosition, transformedImport), didRewrite = !0;
3514
3562
  }
3515
- }), {
3516
- code: src.toString(),
3563
+ });
3564
+ let rewritten = src.toString();
3565
+ return {
3566
+ code: didRewrite ? ensurePreviewRuntimePrecedesGlobals(rewritten, id) : rewritten,
3517
3567
  map: null
3518
3568
  };
3519
3569
  }
3520
3570
  }
3521
3571
  };
3522
3572
  }
3573
+ var bundledPreviewRuntimePattern = /[/\\]dist[/\\]preview[/\\]/;
3574
+ function ensurePreviewRuntimePrecedesGlobals(code, id) {
3575
+ return bundledPreviewRuntimePattern.test(id.split("?")[0]) ? code : `import { setup as __sbInitializePreviewGlobals } from ${JSON.stringify(previewRuntimePath)};
3576
+ __sbInitializePreviewGlobals();
3577
+ ${code}`;
3578
+ }
3523
3579
  function getDefaultImportReplacement(match) {
3524
3580
  let matched = match.match(defaultImportRegExp);
3525
3581
  return matched && `const {default: ${matched[1]}} =`;
@@ -3554,17 +3610,48 @@ var configEnvServe = {
3554
3610
  mode: "production",
3555
3611
  command: "build",
3556
3612
  isSsrBuild: !1
3557
- };
3613
+ }, DEFAULT_VITE_CONFIG_FILES = [
3614
+ "vite.config.js",
3615
+ "vite.config.mjs",
3616
+ "vite.config.ts",
3617
+ "vite.config.cjs",
3618
+ "vite.config.mts",
3619
+ "vite.config.cts"
3620
+ ], warnOnce = once("warn");
3621
+ function reportUnloadedUserViteConfig(configDir, projectRoot) {
3622
+ let root = getProjectRoot();
3623
+ if (relative4(root, projectRoot).startsWith(".."))
3624
+ return;
3625
+ let found = any(DEFAULT_VITE_CONFIG_FILES, {
3626
+ cwd: projectRoot,
3627
+ last: root
3628
+ });
3629
+ if (!found)
3630
+ return;
3631
+ let unloadedConfig = normalize(found);
3632
+ warnOnce(
3633
+ dedent4`
3634
+ No user Vite config was loaded: Storybook auto-discovers a Vite config file in "${projectRoot}" (the directory above your Storybook config dir "${configDir}"), and none was found there.
3635
+ A Vite config was found at "${unloadedConfig}", but it is outside of that directory and was not loaded.
3636
+ To load it, point the "viteConfigPath" builder option at it, e.g. core: { builder: { options: { viteConfigPath: '${unloadedConfig}' } } }.
3637
+ To keep it unloaded and silence this warning, set "viteConfigPath" to null, e.g. core: { builder: { options: { viteConfigPath: null } } }.
3638
+ See: https://storybook.js.org/docs/builders/vite#override-the-default-configuration
3639
+ `
3640
+ );
3641
+ }
3558
3642
  async function loadUserViteConfig(options, type) {
3559
3643
  let configEnv = type === "development" ? configEnvServe : configEnvBuild, { loadConfigFromFile } = await import("vite"), { viteConfigPath, configLoader } = await getBuilderOptions(options), projectRoot = resolve4(options.configDir, "..");
3560
- return (await loadConfigFromFile(
3644
+ if (viteConfigPath === null)
3645
+ return {};
3646
+ let loaded = await loadConfigFromFile(
3561
3647
  configEnv,
3562
3648
  viteConfigPath,
3563
3649
  projectRoot,
3564
3650
  void 0,
3565
3651
  void 0,
3566
3652
  configLoader
3567
- ))?.config ?? {};
3653
+ );
3654
+ return !loaded && viteConfigPath === void 0 && reportUnloadedUserViteConfig(options.configDir, projectRoot), loaded?.config ?? {};
3568
3655
  }
3569
3656
  async function resolveVitePublicDir(options, type) {
3570
3657
  let { publicDir } = await loadUserViteConfig(options, type);
package/dist/index.d.ts CHANGED
@@ -11,7 +11,13 @@ type StorybookConfigVite = {
11
11
  viteFinal?: ViteFinal;
12
12
  };
13
13
  type BuilderOptions = {
14
- /** Path to `vite.config` file, relative to `process.cwd()`. */viteConfigPath?: string;
14
+ /**
15
+ * Path to `vite.config` file, relative to `process.cwd()`.
16
+ *
17
+ * Set to `null` to not load any Vite config file. Use this to keep a config file that
18
+ * Storybook's auto-discovery does not see unloaded, and to silence the matching warning.
19
+ */
20
+ viteConfigPath?: string | null;
15
21
  /**
16
22
  * How Vite loads the config file. Equivalent to Vite's `--configLoader` CLI flag and the
17
23
  * `configLoader` option of `loadConfigFromFile`.
package/dist/index.js CHANGED
@@ -1,10 +1,10 @@
1
- import CJS_COMPAT_NODE_URL_zwca6w95nf from 'node:url';
2
- import CJS_COMPAT_NODE_PATH_zwca6w95nf from 'node:path';
3
- import CJS_COMPAT_NODE_MODULE_zwca6w95nf from "node:module";
1
+ import CJS_COMPAT_NODE_URL_dm9z9i8b6a from 'node:url';
2
+ import CJS_COMPAT_NODE_PATH_dm9z9i8b6a from 'node:path';
3
+ import CJS_COMPAT_NODE_MODULE_dm9z9i8b6a from "node:module";
4
4
 
5
- var __filename = CJS_COMPAT_NODE_URL_zwca6w95nf.fileURLToPath(import.meta.url);
6
- var __dirname = CJS_COMPAT_NODE_PATH_zwca6w95nf.dirname(__filename);
7
- var require = CJS_COMPAT_NODE_MODULE_zwca6w95nf.createRequire(import.meta.url);
5
+ var __filename = CJS_COMPAT_NODE_URL_dm9z9i8b6a.fileURLToPath(import.meta.url);
6
+ var __dirname = CJS_COMPAT_NODE_PATH_dm9z9i8b6a.dirname(__filename);
7
+ var require = CJS_COMPAT_NODE_MODULE_dm9z9i8b6a.createRequire(import.meta.url);
8
8
 
9
9
  // ------------------------------------------------------------
10
10
  // end of CJS compatibility banner, injected by Storybook's esbuild configuration
@@ -15,7 +15,7 @@ import {
15
15
  bundlerOptionsKey,
16
16
  commonConfig,
17
17
  normalize
18
- } from "./_node-chunks/chunk-TTIQ3XRQ.js";
18
+ } from "./_node-chunks/chunk-YTKSCPBY.js";
19
19
 
20
20
  // ../../../node_modules/picocolors/picocolors.js
21
21
  var require_picocolors = __commonJS({
package/dist/preset.js CHANGED
@@ -1,10 +1,10 @@
1
- import CJS_COMPAT_NODE_URL_zwca6w95nf from 'node:url';
2
- import CJS_COMPAT_NODE_PATH_zwca6w95nf from 'node:path';
3
- import CJS_COMPAT_NODE_MODULE_zwca6w95nf from "node:module";
1
+ import CJS_COMPAT_NODE_URL_dm9z9i8b6a from 'node:url';
2
+ import CJS_COMPAT_NODE_PATH_dm9z9i8b6a from 'node:path';
3
+ import CJS_COMPAT_NODE_MODULE_dm9z9i8b6a from "node:module";
4
4
 
5
- var __filename = CJS_COMPAT_NODE_URL_zwca6w95nf.fileURLToPath(import.meta.url);
6
- var __dirname = CJS_COMPAT_NODE_PATH_zwca6w95nf.dirname(__filename);
7
- var require = CJS_COMPAT_NODE_MODULE_zwca6w95nf.createRequire(import.meta.url);
5
+ var __filename = CJS_COMPAT_NODE_URL_dm9z9i8b6a.fileURLToPath(import.meta.url);
6
+ var __dirname = CJS_COMPAT_NODE_PATH_dm9z9i8b6a.dirname(__filename);
7
+ var require = CJS_COMPAT_NODE_MODULE_dm9z9i8b6a.createRequire(import.meta.url);
8
8
 
9
9
  // ------------------------------------------------------------
10
10
  // end of CJS compatibility banner, injected by Storybook's esbuild configuration
@@ -13,7 +13,7 @@ import {
13
13
  optimizeViteDeps,
14
14
  staticDirs,
15
15
  viteCorePlugins
16
- } from "./_node-chunks/chunk-TTIQ3XRQ.js";
16
+ } from "./_node-chunks/chunk-YTKSCPBY.js";
17
17
  export {
18
18
  optimizeViteDeps,
19
19
  staticDirs,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@storybook/builder-vite",
3
- "version": "11.0.0-alpha.6",
3
+ "version": "11.0.0-alpha.8",
4
4
  "description": "A Storybook builder to dev and build with Vite",
5
5
  "keywords": [
6
6
  "storybook",
@@ -47,7 +47,7 @@
47
47
  "!src/**/*"
48
48
  ],
49
49
  "dependencies": {
50
- "ts-dedent": "^2.0.0"
50
+ "ts-dedent": "^2.2.0"
51
51
  },
52
52
  "devDependencies": {
53
53
  "@types/node": "^22.19.1",
@@ -61,7 +61,7 @@
61
61
  "vite": "^7.0.4"
62
62
  },
63
63
  "peerDependencies": {
64
- "storybook": "^11.0.0-alpha.6",
64
+ "storybook": "^11.0.0-alpha.8",
65
65
  "vite": "^6.3.0 || ^7.0.0 || ^8.0.0"
66
66
  },
67
67
  "publishConfig": {