storybook-builder-rsbuild 2.1.5 → 2.1.6

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.mjs CHANGED
@@ -8,12 +8,10 @@ import sirv from 'sirv';
8
8
  import { getBuilderOptions, normalizeStories, loadPreviewOrConfigFile, readTemplate, resolveAddonName, getPresets, stringifyProcessEnvs, isPreservingSymlinks } from 'storybook/internal/common';
9
9
  import { WebpackInvocationError } from 'storybook/internal/server-errors';
10
10
  import { pluginTypeCheck } from '@rsbuild/plugin-type-check';
11
- import { webpack } from '@storybook/addon-docs/preset';
12
11
  import CaseSensitivePathsPlugin from 'case-sensitive-paths-webpack-plugin';
13
12
  import { pluginHtmlMinifierTerser } from 'rsbuild-plugin-html-minifier-terser';
14
13
  import { globalsNameReferenceMap } from 'storybook/internal/preview/globals';
15
14
  import { dedent } from 'ts-dedent';
16
- import { toImportFn } from '@storybook/core-webpack';
17
15
  import { readFile } from 'fs/promises';
18
16
 
19
17
  // ../../node_modules/.pnpm/pretty-hrtime@1.0.3/node_modules/pretty-hrtime/index.js
@@ -87,6 +85,276 @@ var require_pretty_hrtime = __commonJS({
87
85
  }
88
86
  });
89
87
 
88
+ // compiled/@storybook/core-webpack/index.js
89
+ var require_core_webpack = __commonJS({
90
+ "compiled/@storybook/core-webpack/index.js"(exports, module) {
91
+ (() => {
92
+ var __webpack_modules__ = {
93
+ 830: (module2, __unused_webpack_exports, __nccwpck_require__2) => {
94
+ var __defProp = Object.defineProperty;
95
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
96
+ var __getOwnPropNames = Object.getOwnPropertyNames;
97
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
98
+ var __export = (target, all) => {
99
+ for (var name in all)
100
+ __defProp(target, name, { get: all[name], enumerable: true });
101
+ }, __copyProps = (to, from, except, desc) => {
102
+ if (from && typeof from == "object" || typeof from == "function")
103
+ for (let key of __getOwnPropNames(from))
104
+ !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
105
+ get: () => from[key],
106
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
107
+ });
108
+ return to;
109
+ };
110
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
111
+ var index_exports = {};
112
+ __export(index_exports, {
113
+ checkWebpackVersion: () => checkWebpackVersion,
114
+ loadCustomWebpackConfig: () => loadCustomWebpackConfig,
115
+ mergeConfigs: () => mergeConfigs,
116
+ toImportFn: () => toImportFn2,
117
+ toImportFnPart: () => toImportFnPart,
118
+ toRequireContext: () => toRequireContext,
119
+ toRequireContextString: () => toRequireContextString,
120
+ webpackIncludeRegexp: () => webpackIncludeRegexp
121
+ });
122
+ module2.exports = __toCommonJS(index_exports);
123
+ var import_node_path5 = __nccwpck_require__2(928), import_common4 = __nccwpck_require__2(253), webpackConfigs = ["webpack.config", "webpackfile"], loadCustomWebpackConfig = (configDir) => (0, import_common4.serverRequire)(
124
+ webpackConfigs.map(
125
+ (configName) => (0, import_node_path5.resolve)(configDir, configName)
126
+ )
127
+ );
128
+ var import_node_logger = __nccwpck_require__2(519), import_ts_dedent2 = __nccwpck_require__2(980), checkWebpackVersion = (webpack, specifier, caption) => {
129
+ if (!webpack.version) {
130
+ import_node_logger.logger.info(
131
+ "Skipping webpack version check, no version available"
132
+ );
133
+ return;
134
+ }
135
+ webpack.version !== specifier && import_node_logger.logger.warn(import_ts_dedent2.dedent`
136
+ Unexpected webpack version in ${caption}:
137
+ - Received '${webpack.version}'
138
+ - Expected '${specifier}'
139
+
140
+ If you're using Webpack 5 in SB6.2 and upgrading, consider: https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#webpack-5-manager-build
141
+
142
+ For more info about Webpack 5 support: https://gist.github.com/shilman/8856ea1786dcd247139b47b270912324#troubleshooting
143
+ `);
144
+ };
145
+ function mergePluginsField(defaultPlugins = [], customPlugins = []) {
146
+ return [...defaultPlugins, ...customPlugins];
147
+ }
148
+ function mergeRulesField(defaultRules = [], customRules = []) {
149
+ return [...defaultRules, ...customRules];
150
+ }
151
+ function mergeExtensionsField({ extensions: defaultExtensions = [] }, { extensions: customExtensions = [] }) {
152
+ return [...defaultExtensions, ...customExtensions];
153
+ }
154
+ function mergeAliasField({ alias: defaultAlias = {} }, { alias: customAlias = {} }) {
155
+ return { ...defaultAlias, ...customAlias };
156
+ }
157
+ function mergeModuleField(a, b) {
158
+ return {
159
+ ...a,
160
+ ...b,
161
+ rules: mergeRulesField(a.rules || [], b.rules || [])
162
+ };
163
+ }
164
+ function mergeResolveField({ resolve: defaultResolve = {} }, { resolve: customResolve = {} }) {
165
+ return {
166
+ ...defaultResolve,
167
+ ...customResolve,
168
+ alias: mergeAliasField(defaultResolve, customResolve),
169
+ extensions: mergeExtensionsField(defaultResolve, customResolve)
170
+ };
171
+ }
172
+ function mergeOptimizationField({ optimization: defaultOptimization = {} }, { optimization: customOptimization = {} }) {
173
+ return { ...defaultOptimization, ...customOptimization };
174
+ }
175
+ function mergeConfigs(config, customConfig) {
176
+ return {
177
+ ...customConfig,
178
+ ...config,
179
+ devtool: customConfig.devtool || config.devtool,
180
+ plugins: mergePluginsField(config.plugins, customConfig.plugins),
181
+ module: mergeModuleField(
182
+ config.module || {},
183
+ customConfig.module || {}
184
+ ),
185
+ resolve: mergeResolveField(config, customConfig),
186
+ optimization: mergeOptimizationField(config, customConfig)
187
+ };
188
+ }
189
+ var import_common22 = __nccwpck_require__2(253), import_ts_dedent22 = __nccwpck_require__2(980);
190
+ function importPipeline() {
191
+ let importGate = Promise.resolve();
192
+ return async (importFn) => {
193
+ await importGate;
194
+ let moduleExportsPromise = importFn();
195
+ return importGate = importGate.then(async () => {
196
+ await moduleExportsPromise;
197
+ }), moduleExportsPromise;
198
+ };
199
+ }
200
+ function adjustRegexToExcludeNodeModules(originalRegex) {
201
+ let originalRegexString = originalRegex.source, startsWithCaret = originalRegexString.startsWith("^"), excludeNodeModulesPattern = startsWithCaret ? "(?!.*node_modules)" : "^(?!.*node_modules)", adjustedRegexString = startsWithCaret ? `^${excludeNodeModulesPattern}${originalRegexString.substring(1)}` : excludeNodeModulesPattern + originalRegexString;
202
+ return new RegExp(adjustedRegexString);
203
+ }
204
+ function webpackIncludeRegexp(specifier) {
205
+ let { directory, files } = specifier, directoryWithoutLeadingDots = directory.replace(/^(\.+\/)+/, "/"), webpackIncludeGlob = [".", ".."].includes(directory) ? files : `${directoryWithoutLeadingDots}/${files}`, webpackIncludeRegexpWithCaret = webpackIncludeGlob.includes(
206
+ "node_modules"
207
+ ) ? (0, import_common22.globToRegexp)(webpackIncludeGlob) : adjustRegexToExcludeNodeModules(
208
+ (0, import_common22.globToRegexp)(webpackIncludeGlob)
209
+ );
210
+ return new RegExp(
211
+ webpackIncludeRegexpWithCaret.source.replace(/^\^/, "")
212
+ );
213
+ }
214
+ function toImportFnPart(specifier) {
215
+ let { directory, importPathMatcher } = specifier;
216
+ return import_ts_dedent22.dedent`
217
+ async (path) => {
218
+ if (!${importPathMatcher}.exec(path)) {
219
+ return;
220
+ }
221
+
222
+ const pathRemainder = path.substring(${directory.length + 1});
223
+ return import(
224
+ /* webpackChunkName: "[request]" */
225
+ /* webpackInclude: ${webpackIncludeRegexp(specifier)} */
226
+ '${directory}/' + pathRemainder
227
+ );
228
+ }
229
+
230
+ `;
231
+ }
232
+ function toImportFn2(stories, { needPipelinedImport } = {}) {
233
+ let pipelinedImport = "const pipeline = (x) => x();";
234
+ return needPipelinedImport && (pipelinedImport = `
235
+ const importPipeline = ${importPipeline};
236
+ const pipeline = importPipeline();
237
+ `), import_ts_dedent22.dedent`
238
+ ${pipelinedImport}
239
+
240
+ const importers = [
241
+ ${stories.map(toImportFnPart).join(`,
242
+ `)}
243
+ ];
244
+
245
+ export async function importFn(path) {
246
+ for (let i = 0; i < importers.length; i++) {
247
+ const moduleExports = await pipeline(() => importers[i](path));
248
+ if (moduleExports) {
249
+ return moduleExports;
250
+ }
251
+ }
252
+ }
253
+ `;
254
+ }
255
+ var import_common32 = __nccwpck_require__2(253), toRequireContext = (specifier) => {
256
+ let { directory, files } = specifier, match = (0, import_common32.globToRegexp)(`./${files}`);
257
+ return {
258
+ path: directory,
259
+ recursive: files.includes("**") || files.split("/").length > 1,
260
+ match
261
+ };
262
+ }, toRequireContextString = (specifier) => {
263
+ let { path: p, recursive: r, match: m } = toRequireContext(specifier);
264
+ return `require.context('${p}', ${r}, ${m})`;
265
+ };
266
+ },
267
+ 980: (__unused_webpack_module, exports2) => {
268
+ Object.defineProperty(exports2, "__esModule", { value: true });
269
+ exports2.dedent = void 0;
270
+ function dedent2(templ) {
271
+ var values = [];
272
+ for (var _i = 1; _i < arguments.length; _i++) {
273
+ values[_i - 1] = arguments[_i];
274
+ }
275
+ var strings = Array.from(typeof templ === "string" ? [templ] : templ);
276
+ strings[strings.length - 1] = strings[strings.length - 1].replace(
277
+ /\r?\n([\t ]*)$/,
278
+ ""
279
+ );
280
+ var indentLengths = strings.reduce(function(arr, str) {
281
+ var matches = str.match(/\n([\t ]+|(?!\s).)/g);
282
+ if (matches) {
283
+ return arr.concat(
284
+ matches.map(function(match) {
285
+ var _a, _b;
286
+ return (_b = (_a = match.match(/[\t ]/g)) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
287
+ })
288
+ );
289
+ }
290
+ return arr;
291
+ }, []);
292
+ if (indentLengths.length) {
293
+ var pattern_1 = new RegExp(
294
+ "\n[ ]{" + Math.min.apply(Math, indentLengths) + "}",
295
+ "g"
296
+ );
297
+ strings = strings.map(function(str) {
298
+ return str.replace(pattern_1, "\n");
299
+ });
300
+ }
301
+ strings[0] = strings[0].replace(/^\r?\n/, "");
302
+ var string = strings[0];
303
+ values.forEach(function(value, i) {
304
+ var endentations = string.match(/(?:^|\n)( *)$/);
305
+ var endentation = endentations ? endentations[1] : "";
306
+ var indentedValue = value;
307
+ if (typeof value === "string" && value.includes("\n")) {
308
+ indentedValue = String(value).split("\n").map(function(str, i2) {
309
+ return i2 === 0 ? str : "" + endentation + str;
310
+ }).join("\n");
311
+ }
312
+ string += indentedValue + strings[i + 1];
313
+ });
314
+ return string;
315
+ }
316
+ exports2.dedent = dedent2;
317
+ exports2["default"] = dedent2;
318
+ },
319
+ 928: (module2) => {
320
+ module2.exports = __require("path");
321
+ },
322
+ 253: (module2) => {
323
+ module2.exports = __require("storybook/internal/common");
324
+ },
325
+ 519: (module2) => {
326
+ module2.exports = __require("storybook/internal/node-logger");
327
+ }
328
+ };
329
+ var __webpack_module_cache__ = {};
330
+ function __nccwpck_require__(moduleId) {
331
+ var cachedModule = __webpack_module_cache__[moduleId];
332
+ if (cachedModule !== void 0) {
333
+ return cachedModule.exports;
334
+ }
335
+ var module2 = __webpack_module_cache__[moduleId] = { exports: {} };
336
+ var threw = true;
337
+ try {
338
+ __webpack_modules__[moduleId](
339
+ module2,
340
+ module2.exports,
341
+ __nccwpck_require__
342
+ );
343
+ threw = false;
344
+ } finally {
345
+ if (threw)
346
+ delete __webpack_module_cache__[moduleId];
347
+ }
348
+ return module2.exports;
349
+ }
350
+ if (typeof __nccwpck_require__ !== "undefined")
351
+ __nccwpck_require__.ab = __dirname + "/";
352
+ var __webpack_exports__ = __nccwpck_require__(830);
353
+ module.exports = __webpack_exports__;
354
+ })();
355
+ }
356
+ });
357
+
90
358
  // src/index.ts
91
359
  var import_pretty_hrtime = __toESM(require_pretty_hrtime());
92
360
 
@@ -98,6 +366,9 @@ function slash(path) {
98
366
  }
99
367
  return path.replace(/\\/g, "/");
100
368
  }
369
+
370
+ // src/preview/virtual-module-mapping.ts
371
+ var import_core_webpack = __toESM(require_core_webpack());
101
372
  var getVirtualModules = async (options) => {
102
373
  const virtualModules = {};
103
374
  const builderOptions = await getBuilderOptions(options);
@@ -125,7 +396,7 @@ var getVirtualModules = async (options) => {
125
396
  const storiesFilename = "storybook-stories.js";
126
397
  const storiesPath = resolve(join(workingDir, storiesFilename));
127
398
  const needPipelinedImport = !!builderOptions.lazyCompilation && !isProd;
128
- virtualModules[storiesPath] = toImportFn(stories, {
399
+ virtualModules[storiesPath] = (0, import_core_webpack.toImportFn)(stories, {
129
400
  needPipelinedImport
130
401
  });
131
402
  const configEntryPath = resolve(join(workingDir, "storybook-config-entry.js"));
@@ -172,7 +443,12 @@ var storybookPaths = {
172
443
  };
173
444
  var iframe_rsbuild_config_default = async (options, extraWebpackConfig) => {
174
445
  const { rsbuildConfigPath, addonDocs } = await getBuilderOptions(options);
175
- const appliedDocsWebpack = await webpack({}, { ...options, ...addonDocs });
446
+ const webpackConfigFromPresets = await options.presets.apply("webpack", {}, options);
447
+ if (addonDocs) {
448
+ console.warn(
449
+ "`addonDocs` option is deprecated and will be removed in future versions. Please use `@storybook/addon-docs` option instead."
450
+ );
451
+ }
176
452
  const {
177
453
  outputDir = join(".", "public"),
178
454
  quiet,
@@ -420,7 +696,11 @@ var iframe_rsbuild_config_default = async (options, extraWebpackConfig) => {
420
696
  ...config.experiments,
421
697
  ...lazyCompilationConfig
422
698
  };
423
- return mergeConfig(config, extraWebpackConfig || {}, appliedDocsWebpack);
699
+ return mergeConfig(
700
+ config,
701
+ extraWebpackConfig || {},
702
+ webpackConfigFromPresets
703
+ );
424
704
  },
425
705
  htmlPlugin: {
426
706
  filename: "iframe.html",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "storybook-builder-rsbuild",
3
- "version": "2.1.5",
3
+ "version": "2.1.6",
4
4
  "description": "Rsbuild builder for Storybook",
5
5
  "keywords": [
6
6
  "storybook",
@@ -50,6 +50,7 @@
50
50
  "types": "dist/index.d.ts",
51
51
  "files": [
52
52
  "dist/**/*",
53
+ "compiled/**/*",
53
54
  "templates/**/*",
54
55
  "README.md",
55
56
  "*.js",
@@ -58,8 +59,6 @@
58
59
  ],
59
60
  "dependencies": {
60
61
  "@rsbuild/plugin-type-check": "^1.2.4",
61
- "@storybook/addon-docs": "^9.1.15 || ^10.0.0",
62
- "@storybook/core-webpack": "^9.1.15 || ^10.0.0",
63
62
  "browser-assert": "^1.2.1",
64
63
  "case-sensitive-paths-webpack-plugin": "^2.4.0",
65
64
  "cjs-module-lexer": "^1.4.3",
@@ -78,11 +77,13 @@
78
77
  },
79
78
  "devDependencies": {
80
79
  "@rsbuild/core": "^1.5.17",
80
+ "@storybook/core-webpack": "^9.1.15 || ^10.0.0",
81
81
  "@types/find-cache-dir": "^5.0.2",
82
82
  "@types/fs-extra": "^11.0.4",
83
83
  "@types/node": "^18.19.110",
84
84
  "@types/pretty-hrtime": "^1.0.3",
85
85
  "find-cache-dir": "^5.0.0",
86
+ "prebundle": "^1.5.0",
86
87
  "pretty-hrtime": "^1.0.3",
87
88
  "slash": "^5.1.0",
88
89
  "storybook": "9.1.10",
@@ -119,6 +120,6 @@
119
120
  "scripts": {
120
121
  "build": "pnpm run prep --optimized",
121
122
  "check": "jiti ../../scripts/prepare/check.ts",
122
- "prep": "jiti ../../scripts/prepare/bundle.ts"
123
+ "prep": "prebundle && jiti ../../scripts/prepare/bundle.ts"
123
124
  }
124
125
  }