storybook-builder-rsbuild 1.0.1 → 1.0.3

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/index.d.ts CHANGED
@@ -29,7 +29,12 @@ type BuilderOptions = {
29
29
  /**
30
30
  * Enable Rspack's lazy compilation (experimental).
31
31
  */
32
- lazyCompilation?: boolean;
32
+ lazyCompilation?: NonNullable<RsbuildConfig['performance']>['buildCache'];
33
+ /**
34
+ * Enable Rspack's [persistent cache(experimental)](https://rspack.dev/config/experiments#experimentscache).
35
+ * We continue to use the name `fsCache` here to maintain better compatibility with the webpack builder.
36
+ */
37
+ fsCache?: boolean;
33
38
  /**
34
39
  * Which environment to use from the Rsbuild config.
35
40
  */
package/dist/index.js CHANGED
@@ -102,6 +102,41 @@ var require_pretty_hrtime = __commonJS({
102
102
  }
103
103
  });
104
104
 
105
+ // ../../node_modules/.pnpm/common-path-prefix@3.0.0/node_modules/common-path-prefix/index.js
106
+ var require_common_path_prefix = __commonJS({
107
+ "../../node_modules/.pnpm/common-path-prefix@3.0.0/node_modules/common-path-prefix/index.js"(exports, module2) {
108
+ "use strict";
109
+ var { sep: DEFAULT_SEPARATOR } = require("path");
110
+ var determineSeparator = (paths) => {
111
+ for (const path6 of paths) {
112
+ const match = /(\/|\\)/.exec(path6);
113
+ if (match !== null)
114
+ return match[0];
115
+ }
116
+ return DEFAULT_SEPARATOR;
117
+ };
118
+ module2.exports = function commonPathPrefix2(paths, sep = determineSeparator(paths)) {
119
+ const [first = "", ...remaining] = paths;
120
+ if (first === "" || remaining.length === 0)
121
+ return "";
122
+ const parts = first.split(sep);
123
+ let endOfPrefix = parts.length;
124
+ for (const path6 of remaining) {
125
+ const compare = path6.split(sep);
126
+ for (let i = 0; i < endOfPrefix; i++) {
127
+ if (compare[i] !== parts[i]) {
128
+ endOfPrefix = i;
129
+ }
130
+ }
131
+ if (endOfPrefix === 0)
132
+ return "";
133
+ }
134
+ const prefix = parts.slice(0, endOfPrefix).join(sep);
135
+ return prefix.endsWith(sep) ? prefix : prefix + sep;
136
+ };
137
+ }
138
+ });
139
+
105
140
  // src/index.ts
106
141
  var src_exports = {};
107
142
  __export(src_exports, {
@@ -120,7 +155,7 @@ __export(src_exports, {
120
155
  });
121
156
  module.exports = __toCommonJS(src_exports);
122
157
  var import_node_net = require("net");
123
- var import_node_path5 = require("path");
158
+ var import_node_path9 = require("path");
124
159
  var rsbuildReal = __toESM(require("@rsbuild/core"));
125
160
  var import_fs_extra = __toESM(require("fs-extra"));
126
161
  var import_pretty_hrtime = __toESM(require_pretty_hrtime());
@@ -130,10 +165,10 @@ var import_common3 = require("storybook/internal/common");
130
165
  var import_server_errors = require("storybook/internal/server-errors");
131
166
 
132
167
  // src/preview/iframe-rsbuild.config.ts
133
- var import_node_path3 = require("path");
168
+ var import_node_path7 = require("path");
134
169
  var import_core = require("@rsbuild/core");
135
170
  var import_plugin_type_check = require("@rsbuild/plugin-type-check");
136
- var import_preset = require("@storybook/addon-docs/dist/preset");
171
+ var import_preset = require("@storybook/addon-docs/preset");
137
172
  var import_case_sensitive_paths_webpack_plugin = __toESM(require("case-sensitive-paths-webpack-plugin"));
138
173
  var import_rsbuild_plugin_html_minifier_terser = require("rsbuild-plugin-html-minifier-terser");
139
174
  var import_common2 = require("storybook/internal/common");
@@ -141,19 +176,166 @@ var import_globals = require("storybook/internal/preview/globals");
141
176
  var import_ts_dedent2 = require("ts-dedent");
142
177
 
143
178
  // src/preview/virtual-module-mapping.ts
144
- var import_node_fs = __toESM(require("fs"));
145
- var import_node_path = __toESM(require("path"));
146
- var import_node_path2 = require("path");
179
+ var import_node_fs4 = __toESM(require("fs"));
180
+ var import_node_path5 = __toESM(require("path"));
181
+ var import_node_path6 = require("path");
147
182
  var import_core_webpack = require("@storybook/core-webpack");
148
- var import_find_cache_dir = __toESM(require("find-cache-dir"));
183
+
184
+ // ../../node_modules/.pnpm/find-cache-dir@5.0.0/node_modules/find-cache-dir/index.js
185
+ var import_node_process2 = __toESM(require("process"), 1);
186
+ var import_node_path4 = __toESM(require("path"), 1);
187
+ var import_node_fs3 = __toESM(require("fs"), 1);
188
+ var import_common_path_prefix = __toESM(require_common_path_prefix(), 1);
189
+
190
+ // ../../node_modules/.pnpm/pkg-dir@7.0.0/node_modules/pkg-dir/index.js
191
+ var import_node_path3 = __toESM(require("path"), 1);
192
+
193
+ // ../../node_modules/.pnpm/find-up@6.3.0/node_modules/find-up/index.js
194
+ var import_node_path2 = __toESM(require("path"), 1);
195
+ var import_node_url2 = require("url");
196
+
197
+ // ../../node_modules/.pnpm/locate-path@7.2.0/node_modules/locate-path/index.js
198
+ var import_node_process = __toESM(require("process"), 1);
199
+ var import_node_path = __toESM(require("path"), 1);
200
+ var import_node_fs = __toESM(require("fs"), 1);
201
+ var import_node_url = require("url");
202
+ var typeMappings = {
203
+ directory: "isDirectory",
204
+ file: "isFile"
205
+ };
206
+ function checkType(type) {
207
+ if (Object.hasOwnProperty.call(typeMappings, type)) {
208
+ return;
209
+ }
210
+ throw new Error(`Invalid type specified: ${type}`);
211
+ }
212
+ var matchType = (type, stat) => stat[typeMappings[type]]();
213
+ var toPath = (urlOrPath) => urlOrPath instanceof URL ? (0, import_node_url.fileURLToPath)(urlOrPath) : urlOrPath;
214
+ function locatePathSync(paths, {
215
+ cwd: cwd2 = import_node_process.default.cwd(),
216
+ type = "file",
217
+ allowSymlinks = true
218
+ } = {}) {
219
+ checkType(type);
220
+ cwd2 = toPath(cwd2);
221
+ const statFunction = allowSymlinks ? import_node_fs.default.statSync : import_node_fs.default.lstatSync;
222
+ for (const path_ of paths) {
223
+ try {
224
+ const stat = statFunction(import_node_path.default.resolve(cwd2, path_), {
225
+ throwIfNoEntry: false
226
+ });
227
+ if (!stat) {
228
+ continue;
229
+ }
230
+ if (matchType(type, stat)) {
231
+ return path_;
232
+ }
233
+ } catch {
234
+ }
235
+ }
236
+ }
237
+
238
+ // ../../node_modules/.pnpm/path-exists@5.0.0/node_modules/path-exists/index.js
239
+ var import_node_fs2 = __toESM(require("fs"), 1);
240
+
241
+ // ../../node_modules/.pnpm/find-up@6.3.0/node_modules/find-up/index.js
242
+ var toPath2 = (urlOrPath) => urlOrPath instanceof URL ? (0, import_node_url2.fileURLToPath)(urlOrPath) : urlOrPath;
243
+ var findUpStop = Symbol("findUpStop");
244
+ function findUpMultipleSync(name, options = {}) {
245
+ let directory = import_node_path2.default.resolve(toPath2(options.cwd) || "");
246
+ const { root } = import_node_path2.default.parse(directory);
247
+ const stopAt = options.stopAt || root;
248
+ const limit = options.limit || Number.POSITIVE_INFINITY;
249
+ const paths = [name].flat();
250
+ const runMatcher = (locateOptions) => {
251
+ if (typeof name !== "function") {
252
+ return locatePathSync(paths, locateOptions);
253
+ }
254
+ const foundPath = name(locateOptions.cwd);
255
+ if (typeof foundPath === "string") {
256
+ return locatePathSync([foundPath], locateOptions);
257
+ }
258
+ return foundPath;
259
+ };
260
+ const matches = [];
261
+ while (true) {
262
+ const foundPath = runMatcher({ ...options, cwd: directory });
263
+ if (foundPath === findUpStop) {
264
+ break;
265
+ }
266
+ if (foundPath) {
267
+ matches.push(import_node_path2.default.resolve(directory, foundPath));
268
+ }
269
+ if (directory === stopAt || matches.length >= limit) {
270
+ break;
271
+ }
272
+ directory = import_node_path2.default.dirname(directory);
273
+ }
274
+ return matches;
275
+ }
276
+ function findUpSync(name, options = {}) {
277
+ const matches = findUpMultipleSync(name, { ...options, limit: 1 });
278
+ return matches[0];
279
+ }
280
+
281
+ // ../../node_modules/.pnpm/pkg-dir@7.0.0/node_modules/pkg-dir/index.js
282
+ function packageDirectorySync({ cwd: cwd2 } = {}) {
283
+ const filePath = findUpSync("package.json", { cwd: cwd2 });
284
+ return filePath && import_node_path3.default.dirname(filePath);
285
+ }
286
+
287
+ // ../../node_modules/.pnpm/find-cache-dir@5.0.0/node_modules/find-cache-dir/index.js
288
+ var { env, cwd } = import_node_process2.default;
289
+ var isWritable = (path6) => {
290
+ try {
291
+ import_node_fs3.default.accessSync(path6, import_node_fs3.default.constants.W_OK);
292
+ return true;
293
+ } catch {
294
+ return false;
295
+ }
296
+ };
297
+ function useDirectory(directory, options) {
298
+ if (options.create) {
299
+ import_node_fs3.default.mkdirSync(directory, { recursive: true });
300
+ }
301
+ return directory;
302
+ }
303
+ function getNodeModuleDirectory(directory) {
304
+ const nodeModules = import_node_path4.default.join(directory, "node_modules");
305
+ if (!isWritable(nodeModules) && (import_node_fs3.default.existsSync(nodeModules) || !isWritable(import_node_path4.default.join(directory)))) {
306
+ return;
307
+ }
308
+ return nodeModules;
309
+ }
310
+ function findCacheDirectory(options = {}) {
311
+ if (env.CACHE_DIR && !["true", "false", "1", "0"].includes(env.CACHE_DIR)) {
312
+ return useDirectory(import_node_path4.default.join(env.CACHE_DIR, options.name), options);
313
+ }
314
+ let { cwd: directory = cwd(), files } = options;
315
+ if (files) {
316
+ if (!Array.isArray(files)) {
317
+ throw new TypeError(`Expected \`files\` option to be an array, got \`${typeof files}\`.`);
318
+ }
319
+ directory = (0, import_common_path_prefix.default)(files.map((file) => import_node_path4.default.resolve(directory, file)));
320
+ }
321
+ directory = packageDirectorySync({ cwd: directory });
322
+ if (!directory) {
323
+ return;
324
+ }
325
+ const nodeModules = getNodeModuleDirectory(directory);
326
+ if (!nodeModules) {
327
+ return;
328
+ }
329
+ return useDirectory(import_node_path4.default.join(directory, "node_modules", ".cache", options.name), options);
330
+ }
149
331
 
150
332
  // ../../node_modules/.pnpm/slash@5.1.0/node_modules/slash/index.js
151
- function slash(path2) {
152
- const isExtendedLengthPath = path2.startsWith("\\\\?\\");
333
+ function slash(path6) {
334
+ const isExtendedLengthPath = path6.startsWith("\\\\?\\");
153
335
  if (isExtendedLengthPath) {
154
- return path2;
336
+ return path6;
155
337
  }
156
- return path2.replace(/\\/g, "/");
338
+ return path6.replace(/\\/g, "/");
157
339
  }
158
340
 
159
341
  // src/preview/virtual-module-mapping.ts
@@ -161,17 +343,17 @@ var import_common = require("storybook/internal/common");
161
343
  var import_ts_dedent = require("ts-dedent");
162
344
  var getVirtualModules = async (options) => {
163
345
  const virtualModules = {};
164
- const cwd = process.cwd();
346
+ const cwd2 = process.cwd();
165
347
  const workingDir = options.cache ? (
166
348
  // TODO: This is a hard code cache dir, as Rspack doesn't support virtual modules now.
167
349
  // Remove this when Rspack supports virtual modules.
168
- (0, import_find_cache_dir.default)({
350
+ findCacheDirectory({
169
351
  name: "storybook-rsbuild-builder",
170
352
  create: true
171
353
  })
172
354
  ) : process.cwd();
173
- if (!import_node_fs.default.existsSync(workingDir)) {
174
- import_node_fs.default.mkdirSync(workingDir, { recursive: true });
355
+ if (!import_node_fs4.default.existsSync(workingDir)) {
356
+ import_node_fs4.default.mkdirSync(workingDir, { recursive: true });
175
357
  }
176
358
  const isProd = options.configType === "PRODUCTION";
177
359
  const nonNormalizedStories = await options.presets.apply("stories", []);
@@ -180,7 +362,7 @@ var getVirtualModules = async (options) => {
180
362
  configDir: options.configDir,
181
363
  workingDir
182
364
  });
183
- const realPathRelativeToCwd = import_node_path.default.relative(workingDir, cwd).split(import_node_path.default.sep).join(import_node_path.default.posix.sep);
365
+ const realPathRelativeToCwd = import_node_path5.default.relative(workingDir, cwd2).split(import_node_path5.default.sep).join(import_node_path5.default.posix.sep);
184
366
  const previewAnnotations = [
185
367
  ...(await options.presets.apply(
186
368
  "previewAnnotations",
@@ -195,13 +377,13 @@ var getVirtualModules = async (options) => {
195
377
  (0, import_common.loadPreviewOrConfigFile)(options)
196
378
  ].filter(Boolean);
197
379
  const storiesFilename = "storybook-stories.js";
198
- const storiesPath = (0, import_node_path2.resolve)((0, import_node_path2.join)(workingDir, storiesFilename));
380
+ const storiesPath = (0, import_node_path6.resolve)((0, import_node_path6.join)(workingDir, storiesFilename));
199
381
  const builderOptions = await (0, import_common.getBuilderOptions)(options);
200
382
  const needPipelinedImport = !!builderOptions.lazyCompilation && !isProd;
201
383
  virtualModules[storiesPath] = toImportFn(stories, realPathRelativeToCwd, {
202
384
  needPipelinedImport
203
385
  });
204
- const configEntryPath = (0, import_node_path2.resolve)((0, import_node_path2.join)(workingDir, "storybook-config-entry.js"));
386
+ const configEntryPath = (0, import_node_path6.resolve)((0, import_node_path6.join)(workingDir, "storybook-config-entry.js"));
205
387
  virtualModules[configEntryPath] = (await (0, import_common.readTemplate)(
206
388
  require.resolve("storybook-builder-rsbuild/templates/virtualModuleModernEntry.js")
207
389
  )).replaceAll(`'{{storiesFilename}}'`, `'./${storiesFilename}'`).replaceAll(
@@ -213,7 +395,7 @@ var getVirtualModules = async (options) => {
213
395
  ).replace(/\\/g, "\\\\");
214
396
  entries.push(configEntryPath);
215
397
  for (const [key, value] of Object.entries(virtualModules)) {
216
- import_node_fs.default.writeFileSync(key, value);
398
+ import_node_fs4.default.writeFileSync(key, value);
217
399
  }
218
400
  return {
219
401
  virtualModules,
@@ -288,7 +470,7 @@ function importPipeline() {
288
470
  }
289
471
 
290
472
  // src/preview/iframe-rsbuild.config.ts
291
- var getAbsolutePath = (input) => (0, import_node_path3.dirname)(require.resolve((0, import_node_path3.join)(input, "package.json")));
473
+ var getAbsolutePath = (input) => (0, import_node_path7.dirname)(require.resolve((0, import_node_path7.join)(input, "package.json")));
292
474
  var maybeGetAbsolutePath = (input) => {
293
475
  try {
294
476
  return getAbsolutePath(input);
@@ -323,7 +505,7 @@ var iframe_rsbuild_config_default = async (options, extraWebpackConfig) => {
323
505
  const { rsbuildConfigPath, addonDocs } = await (0, import_common2.getBuilderOptions)(options);
324
506
  const appliedDocsWebpack = await (0, import_preset.webpack)({}, { ...options, ...addonDocs });
325
507
  const {
326
- outputDir = (0, import_node_path3.join)(".", "public"),
508
+ outputDir = (0, import_node_path7.join)(".", "public"),
327
509
  quiet,
328
510
  packageJson,
329
511
  configType,
@@ -370,6 +552,7 @@ var iframe_rsbuild_config_default = async (options, extraWebpackConfig) => {
370
552
  const shouldCheckTs = typescriptOptions.check && !typescriptOptions.skipCompiler;
371
553
  const tsCheckOptions = typescriptOptions.checkOptions || {};
372
554
  const builderOptions = await (0, import_common2.getBuilderOptions)(options);
555
+ const cacheConfig = builderOptions.fsCache ? true : void 0;
373
556
  const lazyCompilationConfig = builderOptions.lazyCompilation && !isProd ? {
374
557
  lazyCompilation: { entries: false }
375
558
  } : {};
@@ -440,7 +623,7 @@ var iframe_rsbuild_config_default = async (options, extraWebpackConfig) => {
440
623
  css: !options.build?.test?.disableSourcemaps
441
624
  },
442
625
  distPath: {
443
- root: (0, import_node_path3.resolve)(process.cwd(), outputDir)
626
+ root: (0, import_node_path7.resolve)(process.cwd(), outputDir)
444
627
  },
445
628
  filename: {
446
629
  js: isProd ? "[name].[contenthash:8].iframe.bundle.js" : "[name].iframe.bundle.js",
@@ -481,7 +664,8 @@ var iframe_rsbuild_config_default = async (options, extraWebpackConfig) => {
481
664
  splitChunks: {
482
665
  chunks: "all"
483
666
  }
484
- }
667
+ },
668
+ buildCache: cacheConfig
485
669
  },
486
670
  plugins: [
487
671
  shouldCheckTs ? (0, import_plugin_type_check.pluginTypeCheck)(tsCheckOptions) : null,
@@ -553,7 +737,7 @@ var iframe_rsbuild_config_default = async (options, extraWebpackConfig) => {
553
737
  ...config.experiments,
554
738
  ...lazyCompilationConfig
555
739
  };
556
- return mergeConfig(config, extraWebpackConfig, appliedDocsWebpack);
740
+ return mergeConfig(config, extraWebpackConfig || {}, appliedDocsWebpack);
557
741
  },
558
742
  htmlPlugin: {
559
743
  filename: "iframe.html",
@@ -590,7 +774,7 @@ var iframe_rsbuild_config_default = async (options, extraWebpackConfig) => {
590
774
 
591
775
  // src/react-shims.ts
592
776
  var import_promises = require("fs/promises");
593
- var import_node_path4 = require("path");
777
+ var import_node_path8 = require("path");
594
778
  var getIsReactVersion18or19 = async (options) => {
595
779
  const { legacyRootApi } = await options.presets.apply(
596
780
  "frameworkOptions"
@@ -602,12 +786,12 @@ var getIsReactVersion18or19 = async (options) => {
602
786
  "resolvedReact",
603
787
  {}
604
788
  );
605
- const reactDom = resolvedReact.reactDom || (0, import_node_path4.dirname)(require.resolve("react-dom/package.json"));
606
- if (!(0, import_node_path4.isAbsolute)(reactDom)) {
789
+ const reactDom = resolvedReact.reactDom || (0, import_node_path8.dirname)(require.resolve("react-dom/package.json"));
790
+ if (!(0, import_node_path8.isAbsolute)(reactDom)) {
607
791
  return false;
608
792
  }
609
793
  const { version } = JSON.parse(
610
- await (0, import_promises.readFile)((0, import_node_path4.join)(reactDom, "package.json"), "utf-8")
794
+ await (0, import_promises.readFile)((0, import_node_path8.join)(reactDom, "package.json"), "utf-8")
611
795
  );
612
796
  return version.startsWith("18") || version.startsWith("19") || version.startsWith("0.0.0");
613
797
  };
@@ -650,13 +834,14 @@ var rsbuild = async (_, options) => {
650
834
  "webpackFinal",
651
835
  // TODO: using empty webpack config as base for now. It's better to using the composed rspack
652
836
  // config in `iframe-rsbuild.config.ts` as base config. But when `tools.rspack` is an async function,
653
- // the following `tools.rspack` raise an ` Promises are not supported` error.
837
+ // the following `tools.rspack` raise an `Promises are not supported` error.
654
838
  {
655
839
  output: {},
656
840
  module: {},
657
841
  plugins: [],
658
842
  resolve: {},
659
- devServer: {},
843
+ // https://github.com/web-infra-dev/rsbuild/blob/8dc35dc1d1500d2f119875d46b6a07e27986d532/packages/core/src/provider/rspackConfig.ts#L167
844
+ devServer: void 0,
660
845
  optimization: {},
661
846
  performance: {},
662
847
  externals: {},
@@ -732,7 +917,7 @@ var start = async ({
732
917
  error: new Error("Missing Rsbuild build instance at runtime!")
733
918
  });
734
919
  }
735
- const previewResolvedDir = (0, import_node_path5.join)(import_core_path.corePath, "dist/preview");
920
+ const previewResolvedDir = (0, import_node_path9.join)(import_core_path.corePath, "dist/preview");
736
921
  const previewDirOrigin = previewResolvedDir;
737
922
  router.use(
738
923
  "/sb-preview",
@@ -755,16 +940,16 @@ var build = async ({ options }) => {
755
940
  cwd: process.cwd(),
756
941
  rsbuildConfig: config
757
942
  });
758
- const previewResolvedDir = (0, import_node_path5.join)(import_core_path.corePath, "dist/preview");
943
+ const previewResolvedDir = (0, import_node_path9.join)(import_core_path.corePath, "dist/preview");
759
944
  const previewDirOrigin = previewResolvedDir;
760
- const previewDirTarget = (0, import_node_path5.join)(options.outputDir || "", "sb-preview");
945
+ const previewDirTarget = (0, import_node_path9.join)(options.outputDir || "", "sb-preview");
761
946
  let stats;
762
947
  rsbuildBuild.onAfterBuild((params) => {
763
948
  stats = params.stats;
764
949
  });
765
950
  const previewFiles = import_fs_extra.default.copy(previewDirOrigin, previewDirTarget, {
766
951
  filter: (src) => {
767
- const { ext } = (0, import_node_path5.parse)(src);
952
+ const { ext } = (0, import_node_path9.parse)(src);
768
953
  if (ext) {
769
954
  return ext === ".js";
770
955
  }
@@ -778,7 +963,7 @@ var build = async ({ options }) => {
778
963
  await close();
779
964
  return stats;
780
965
  };
781
- var corePresets = [(0, import_node_path5.join)(__dirname, "./preview-preset.js")];
966
+ var corePresets = [(0, import_node_path9.join)(__dirname, "./preview-preset.js")];
782
967
  var previewMainTemplate = () => require.resolve("storybook-builder-rsbuild/templates/preview.ejs");
783
968
  function getRandomPort(host) {
784
969
  return new Promise((resolve3, reject) => {
package/dist/index.mjs CHANGED
@@ -1,22 +1,23 @@
1
- import { __commonJS, __toESM, __require } from './chunk-TTFRSOOU.mjs';
1
+ import { __commonJS, __require, __toESM } from './chunk-TTFRSOOU.mjs';
2
2
  import { createServer } from 'net';
3
- import path, { join, resolve, parse, dirname, isAbsolute } from 'path';
3
+ import path4, { join, resolve, parse, dirname, isAbsolute } from 'path';
4
4
  import * as rsbuildReal from '@rsbuild/core';
5
5
  import { loadConfig, mergeRsbuildConfig } from '@rsbuild/core';
6
- import fs2 from 'fs-extra';
6
+ import fs4 from 'fs-extra';
7
7
  import sirv from 'sirv';
8
8
  import { corePath } from 'storybook/core-path';
9
9
  import { normalizeStories, loadPreviewOrConfigFile, getBuilderOptions, readTemplate, resolveAddonName, getPresets, stringifyProcessEnvs, isPreservingSymlinks } from 'storybook/internal/common';
10
10
  import { WebpackInvocationError } from 'storybook/internal/server-errors';
11
11
  import { pluginTypeCheck } from '@rsbuild/plugin-type-check';
12
- import { webpack } from '@storybook/addon-docs/dist/preset';
12
+ import { webpack } from '@storybook/addon-docs/preset';
13
13
  import CaseSensitivePathsPlugin from 'case-sensitive-paths-webpack-plugin';
14
14
  import { pluginHtmlMinifierTerser } from 'rsbuild-plugin-html-minifier-terser';
15
15
  import { globalsNameReferenceMap } from 'storybook/internal/preview/globals';
16
16
  import { dedent } from 'ts-dedent';
17
- import fs from 'fs';
17
+ import fs2 from 'fs';
18
18
  import { webpackIncludeRegexp } from '@storybook/core-webpack';
19
- import findCacheDirectory from 'find-cache-dir';
19
+ import process2 from 'process';
20
+ import { fileURLToPath } from 'url';
20
21
  import { readFile } from 'fs/promises';
21
22
 
22
23
  // ../../node_modules/.pnpm/pretty-hrtime@1.0.3/node_modules/pretty-hrtime/index.js
@@ -90,20 +91,183 @@ var require_pretty_hrtime = __commonJS({
90
91
  }
91
92
  });
92
93
 
94
+ // ../../node_modules/.pnpm/common-path-prefix@3.0.0/node_modules/common-path-prefix/index.js
95
+ var require_common_path_prefix = __commonJS({
96
+ "../../node_modules/.pnpm/common-path-prefix@3.0.0/node_modules/common-path-prefix/index.js"(exports, module) {
97
+ var { sep: DEFAULT_SEPARATOR } = __require("path");
98
+ var determineSeparator = (paths) => {
99
+ for (const path6 of paths) {
100
+ const match = /(\/|\\)/.exec(path6);
101
+ if (match !== null)
102
+ return match[0];
103
+ }
104
+ return DEFAULT_SEPARATOR;
105
+ };
106
+ module.exports = function commonPathPrefix2(paths, sep = determineSeparator(paths)) {
107
+ const [first = "", ...remaining] = paths;
108
+ if (first === "" || remaining.length === 0)
109
+ return "";
110
+ const parts = first.split(sep);
111
+ let endOfPrefix = parts.length;
112
+ for (const path6 of remaining) {
113
+ const compare = path6.split(sep);
114
+ for (let i = 0; i < endOfPrefix; i++) {
115
+ if (compare[i] !== parts[i]) {
116
+ endOfPrefix = i;
117
+ }
118
+ }
119
+ if (endOfPrefix === 0)
120
+ return "";
121
+ }
122
+ const prefix = parts.slice(0, endOfPrefix).join(sep);
123
+ return prefix.endsWith(sep) ? prefix : prefix + sep;
124
+ };
125
+ }
126
+ });
127
+
93
128
  // src/index.ts
94
129
  var import_pretty_hrtime = __toESM(require_pretty_hrtime());
95
130
 
131
+ // ../../node_modules/.pnpm/find-cache-dir@5.0.0/node_modules/find-cache-dir/index.js
132
+ var import_common_path_prefix = __toESM(require_common_path_prefix(), 1);
133
+ var typeMappings = {
134
+ directory: "isDirectory",
135
+ file: "isFile"
136
+ };
137
+ function checkType(type) {
138
+ if (Object.hasOwnProperty.call(typeMappings, type)) {
139
+ return;
140
+ }
141
+ throw new Error(`Invalid type specified: ${type}`);
142
+ }
143
+ var matchType = (type, stat) => stat[typeMappings[type]]();
144
+ var toPath = (urlOrPath) => urlOrPath instanceof URL ? fileURLToPath(urlOrPath) : urlOrPath;
145
+ function locatePathSync(paths, {
146
+ cwd: cwd2 = process2.cwd(),
147
+ type = "file",
148
+ allowSymlinks = true
149
+ } = {}) {
150
+ checkType(type);
151
+ cwd2 = toPath(cwd2);
152
+ const statFunction = allowSymlinks ? fs2.statSync : fs2.lstatSync;
153
+ for (const path_ of paths) {
154
+ try {
155
+ const stat = statFunction(path4.resolve(cwd2, path_), {
156
+ throwIfNoEntry: false
157
+ });
158
+ if (!stat) {
159
+ continue;
160
+ }
161
+ if (matchType(type, stat)) {
162
+ return path_;
163
+ }
164
+ } catch {
165
+ }
166
+ }
167
+ }
168
+
169
+ // ../../node_modules/.pnpm/find-up@6.3.0/node_modules/find-up/index.js
170
+ var toPath2 = (urlOrPath) => urlOrPath instanceof URL ? fileURLToPath(urlOrPath) : urlOrPath;
171
+ var findUpStop = Symbol("findUpStop");
172
+ function findUpMultipleSync(name, options = {}) {
173
+ let directory = path4.resolve(toPath2(options.cwd) || "");
174
+ const { root } = path4.parse(directory);
175
+ const stopAt = options.stopAt || root;
176
+ const limit = options.limit || Number.POSITIVE_INFINITY;
177
+ const paths = [name].flat();
178
+ const runMatcher = (locateOptions) => {
179
+ if (typeof name !== "function") {
180
+ return locatePathSync(paths, locateOptions);
181
+ }
182
+ const foundPath = name(locateOptions.cwd);
183
+ if (typeof foundPath === "string") {
184
+ return locatePathSync([foundPath], locateOptions);
185
+ }
186
+ return foundPath;
187
+ };
188
+ const matches = [];
189
+ while (true) {
190
+ const foundPath = runMatcher({ ...options, cwd: directory });
191
+ if (foundPath === findUpStop) {
192
+ break;
193
+ }
194
+ if (foundPath) {
195
+ matches.push(path4.resolve(directory, foundPath));
196
+ }
197
+ if (directory === stopAt || matches.length >= limit) {
198
+ break;
199
+ }
200
+ directory = path4.dirname(directory);
201
+ }
202
+ return matches;
203
+ }
204
+ function findUpSync(name, options = {}) {
205
+ const matches = findUpMultipleSync(name, { ...options, limit: 1 });
206
+ return matches[0];
207
+ }
208
+
209
+ // ../../node_modules/.pnpm/pkg-dir@7.0.0/node_modules/pkg-dir/index.js
210
+ function packageDirectorySync({ cwd: cwd2 } = {}) {
211
+ const filePath = findUpSync("package.json", { cwd: cwd2 });
212
+ return filePath && path4.dirname(filePath);
213
+ }
214
+
215
+ // ../../node_modules/.pnpm/find-cache-dir@5.0.0/node_modules/find-cache-dir/index.js
216
+ var { env, cwd } = process2;
217
+ var isWritable = (path6) => {
218
+ try {
219
+ fs2.accessSync(path6, fs2.constants.W_OK);
220
+ return true;
221
+ } catch {
222
+ return false;
223
+ }
224
+ };
225
+ function useDirectory(directory, options) {
226
+ if (options.create) {
227
+ fs2.mkdirSync(directory, { recursive: true });
228
+ }
229
+ return directory;
230
+ }
231
+ function getNodeModuleDirectory(directory) {
232
+ const nodeModules = path4.join(directory, "node_modules");
233
+ if (!isWritable(nodeModules) && (fs2.existsSync(nodeModules) || !isWritable(path4.join(directory)))) {
234
+ return;
235
+ }
236
+ return nodeModules;
237
+ }
238
+ function findCacheDirectory(options = {}) {
239
+ if (env.CACHE_DIR && !["true", "false", "1", "0"].includes(env.CACHE_DIR)) {
240
+ return useDirectory(path4.join(env.CACHE_DIR, options.name), options);
241
+ }
242
+ let { cwd: directory = cwd(), files } = options;
243
+ if (files) {
244
+ if (!Array.isArray(files)) {
245
+ throw new TypeError(`Expected \`files\` option to be an array, got \`${typeof files}\`.`);
246
+ }
247
+ directory = (0, import_common_path_prefix.default)(files.map((file) => path4.resolve(directory, file)));
248
+ }
249
+ directory = packageDirectorySync({ cwd: directory });
250
+ if (!directory) {
251
+ return;
252
+ }
253
+ const nodeModules = getNodeModuleDirectory(directory);
254
+ if (!nodeModules) {
255
+ return;
256
+ }
257
+ return useDirectory(path4.join(directory, "node_modules", ".cache", options.name), options);
258
+ }
259
+
96
260
  // ../../node_modules/.pnpm/slash@5.1.0/node_modules/slash/index.js
97
- function slash(path2) {
98
- const isExtendedLengthPath = path2.startsWith("\\\\?\\");
261
+ function slash(path6) {
262
+ const isExtendedLengthPath = path6.startsWith("\\\\?\\");
99
263
  if (isExtendedLengthPath) {
100
- return path2;
264
+ return path6;
101
265
  }
102
- return path2.replace(/\\/g, "/");
266
+ return path6.replace(/\\/g, "/");
103
267
  }
104
268
  var getVirtualModules = async (options) => {
105
269
  const virtualModules = {};
106
- const cwd = process.cwd();
270
+ const cwd2 = process.cwd();
107
271
  const workingDir = options.cache ? (
108
272
  // TODO: This is a hard code cache dir, as Rspack doesn't support virtual modules now.
109
273
  // Remove this when Rspack supports virtual modules.
@@ -112,8 +276,8 @@ var getVirtualModules = async (options) => {
112
276
  create: true
113
277
  })
114
278
  ) : process.cwd();
115
- if (!fs.existsSync(workingDir)) {
116
- fs.mkdirSync(workingDir, { recursive: true });
279
+ if (!fs2.existsSync(workingDir)) {
280
+ fs2.mkdirSync(workingDir, { recursive: true });
117
281
  }
118
282
  const isProd = options.configType === "PRODUCTION";
119
283
  const nonNormalizedStories = await options.presets.apply("stories", []);
@@ -122,7 +286,7 @@ var getVirtualModules = async (options) => {
122
286
  configDir: options.configDir,
123
287
  workingDir
124
288
  });
125
- const realPathRelativeToCwd = path.relative(workingDir, cwd).split(path.sep).join(path.posix.sep);
289
+ const realPathRelativeToCwd = path4.relative(workingDir, cwd2).split(path4.sep).join(path4.posix.sep);
126
290
  const previewAnnotations = [
127
291
  ...(await options.presets.apply(
128
292
  "previewAnnotations",
@@ -157,7 +321,7 @@ var getVirtualModules = async (options) => {
157
321
  ).replace(/\\/g, "\\\\");
158
322
  entries.push(configEntryPath);
159
323
  for (const [key, value] of Object.entries(virtualModules)) {
160
- fs.writeFileSync(key, value);
324
+ fs2.writeFileSync(key, value);
161
325
  }
162
326
  return {
163
327
  virtualModules,
@@ -314,6 +478,7 @@ var iframe_rsbuild_config_default = async (options, extraWebpackConfig) => {
314
478
  const shouldCheckTs = typescriptOptions.check && !typescriptOptions.skipCompiler;
315
479
  const tsCheckOptions = typescriptOptions.checkOptions || {};
316
480
  const builderOptions = await getBuilderOptions(options);
481
+ const cacheConfig = builderOptions.fsCache ? true : void 0;
317
482
  const lazyCompilationConfig = builderOptions.lazyCompilation && !isProd ? {
318
483
  lazyCompilation: { entries: false }
319
484
  } : {};
@@ -425,7 +590,8 @@ var iframe_rsbuild_config_default = async (options, extraWebpackConfig) => {
425
590
  splitChunks: {
426
591
  chunks: "all"
427
592
  }
428
- }
593
+ },
594
+ buildCache: cacheConfig
429
595
  },
430
596
  plugins: [
431
597
  shouldCheckTs ? pluginTypeCheck(tsCheckOptions) : null,
@@ -499,7 +665,7 @@ var iframe_rsbuild_config_default = async (options, extraWebpackConfig) => {
499
665
  ...config.experiments,
500
666
  ...lazyCompilationConfig
501
667
  };
502
- return mergeConfig(config, extraWebpackConfig, appliedDocsWebpack);
668
+ return mergeConfig(config, extraWebpackConfig || {}, appliedDocsWebpack);
503
669
  },
504
670
  htmlPlugin: {
505
671
  filename: "iframe.html",
@@ -592,13 +758,14 @@ var rsbuild = async (_, options) => {
592
758
  "webpackFinal",
593
759
  // TODO: using empty webpack config as base for now. It's better to using the composed rspack
594
760
  // config in `iframe-rsbuild.config.ts` as base config. But when `tools.rspack` is an async function,
595
- // the following `tools.rspack` raise an ` Promises are not supported` error.
761
+ // the following `tools.rspack` raise an `Promises are not supported` error.
596
762
  {
597
763
  output: {},
598
764
  module: {},
599
765
  plugins: [],
600
766
  resolve: {},
601
- devServer: {},
767
+ // https://github.com/web-infra-dev/rsbuild/blob/8dc35dc1d1500d2f119875d46b6a07e27986d532/packages/core/src/provider/rspackConfig.ts#L167
768
+ devServer: void 0,
602
769
  optimization: {},
603
770
  performance: {},
604
771
  externals: {},
@@ -704,7 +871,7 @@ var build = async ({ options }) => {
704
871
  rsbuildBuild.onAfterBuild((params) => {
705
872
  stats = params.stats;
706
873
  });
707
- const previewFiles = fs2.copy(previewDirOrigin, previewDirTarget, {
874
+ const previewFiles = fs4.copy(previewDirOrigin, previewDirTarget, {
708
875
  filter: (src) => {
709
876
  const { ext } = parse(src);
710
877
  if (ext) {
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "storybook-builder-rsbuild",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Rsbuild builder for Storybook",
5
5
  "keywords": [
6
6
  "storybook",
7
7
  "rsbuild",
8
8
  "rspack"
9
9
  ],
10
- "homepage": "https://storybook-rsbuild.netlify.app",
10
+ "homepage": "https://storybook.rsbuild.dev",
11
11
  "bugs": {
12
12
  "url": "https://github.com/rspack-contrib/storybook-rsbuild/issues"
13
13
  },
@@ -57,15 +57,14 @@
57
57
  "!src/**/*"
58
58
  ],
59
59
  "dependencies": {
60
- "@rsbuild/plugin-type-check": "^1.2.1",
61
- "@storybook/addon-docs": "^8.2.1",
62
- "@storybook/core-webpack": "^8.2.1",
60
+ "@rsbuild/plugin-type-check": "^1.2.2",
61
+ "@storybook/addon-docs": "^8.6.14",
62
+ "@storybook/core-webpack": "^8.6.14",
63
63
  "browser-assert": "^1.2.1",
64
64
  "case-sensitive-paths-webpack-plugin": "^2.4.0",
65
65
  "cjs-module-lexer": "^1.4.3",
66
66
  "constants-browserify": "^1.0.0",
67
67
  "es-module-lexer": "^1.6.0",
68
- "find-cache-dir": "^5.0.0",
69
68
  "fs-extra": "^11.3.0",
70
69
  "magic-string": "^0.30.17",
71
70
  "path-browserify": "^1.0.1",
@@ -78,14 +77,15 @@
78
77
  "util-deprecate": "^1.0.2"
79
78
  },
80
79
  "devDependencies": {
81
- "@rsbuild/core": "^1.1.13",
80
+ "@rsbuild/core": "^1.3.22",
82
81
  "@types/find-cache-dir": "^5.0.2",
83
82
  "@types/fs-extra": "^11.0.4",
84
83
  "@types/node": "^18.0.0",
85
84
  "@types/pretty-hrtime": "^1.0.3",
85
+ "find-cache-dir": "^5.0.0",
86
86
  "pretty-hrtime": "^1.0.3",
87
87
  "slash": "^5.1.0",
88
- "storybook": "8.6.3",
88
+ "storybook": "8.6.14",
89
89
  "typescript": "^5.7.3"
90
90
  },
91
91
  "peerDependencies": {