@modern-js/storybook-builder 2.40.0 → 2.41.0

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/cjs/core.js CHANGED
@@ -26,15 +26,19 @@ var import_core = require("@modern-js/core");
26
26
  var import_utils = require("./utils");
27
27
  var import_plugin_storybook = require("./plugin-storybook");
28
28
  async function getCompiler(cwd, builderOptions, options) {
29
- const bundler = builderOptions.bundler || "webpack";
29
+ const { bundler } = builderOptions;
30
30
  const { presets } = options;
31
31
  const entries = await presets.apply("entries", []);
32
32
  const res = await (0, import_utils.runWithErrorMsg)(() => (0, import_core.loadConfig)(cwd, builderOptions.configPath || (0, import_utils.getConfigFileName)()), "Failed to load config");
33
33
  const loadedConfig = res ? res.config : {};
34
34
  const finalConfig = await presets.apply("modern", loadedConfig) || loadedConfig;
35
- const provider = await (0, import_utils.getProvider)(bundler, finalConfig);
35
+ const provider = await (0, import_utils.getProvider)(bundler, (0, import_builder.mergeBuilderConfig)(finalConfig, builderOptions.builderConfig) || {});
36
36
  if (!provider) {
37
- throw new Error(`@modern-js/builder-${bundler}-provider not found `);
37
+ if (bundler) {
38
+ throw new Error(`You choose to use ${bundler}, but @modern-js/builder-${bundler}-provider not found in your project, please install it`);
39
+ } else {
40
+ throw new Error(`Please install one provider first, try install @modern-js/builder-rspack-provider or @modern-js/builder-webpack-provider first`);
41
+ }
38
42
  }
39
43
  const builder = await (0, import_builder.createBuilder)(provider, {
40
44
  cwd,
@@ -89,6 +89,7 @@ const pluginStorybook = (cwd, options) => {
89
89
  await applyCsfPlugin(config, options);
90
90
  };
91
91
  if ("modifyWebpackConfig" in api) {
92
+ addonAdapter(api, options);
92
93
  api.modifyWebpackConfig(modifyConfig);
93
94
  api.modifyWebpackChain(async (chain) => {
94
95
  await (0, import_docgen.applyDocgenWebpack)(chain, options);
@@ -330,6 +331,65 @@ async function watchStories(patterns, cwd, writeModule) {
330
331
  ]);
331
332
  return watcher;
332
333
  }
334
+ function addonAdapter(api, options) {
335
+ const { presets } = options;
336
+ api.modifyBuilderConfig(async (finalConfig) => {
337
+ var _finalConfig;
338
+ const babelOptions = await presets.apply("babel", {}, {
339
+ ...options
340
+ });
341
+ var _tools;
342
+ (_tools = (_finalConfig = finalConfig).tools) !== null && _tools !== void 0 ? _tools : _finalConfig.tools = {};
343
+ const userConfig = finalConfig.tools.babel;
344
+ finalConfig.tools.babel = (config, utils) => {
345
+ const getPluginName = (plugin) => Array.isArray(plugin) ? plugin[0] : plugin;
346
+ const getOptions = (plugin) => Array.isArray(plugin) ? plugin[1] : null;
347
+ const replaceOrInsert = (plugin, plugins) => {
348
+ const pluginName = getPluginName(plugin);
349
+ const append = [];
350
+ for (let i = 0; i < plugins.length; i++) {
351
+ if (getPluginName(plugins[i]) === pluginName) {
352
+ if (getOptions(plugin)) {
353
+ import_utils.logger.info(`Detected duplicated babel plugin or presets: ${pluginName}, overrides with the new one`);
354
+ plugins[i] = plugin;
355
+ }
356
+ } else {
357
+ append.push(plugin);
358
+ }
359
+ }
360
+ plugins.push(...append);
361
+ };
362
+ const currentPlugins = config.plugins || [];
363
+ const currentPresets = config.presets || [];
364
+ for (const plugin of babelOptions.plugins || []) {
365
+ replaceOrInsert(plugin, currentPlugins);
366
+ }
367
+ for (const preset of babelOptions.presets || []) {
368
+ replaceOrInsert(preset, currentPresets);
369
+ }
370
+ const finalConfig2 = {
371
+ ...config,
372
+ ...babelOptions,
373
+ plugins: currentPlugins,
374
+ presets: currentPresets
375
+ };
376
+ if (typeof userConfig === "function") {
377
+ return userConfig(finalConfig2, utils);
378
+ } else if (typeof userConfig === "object") {
379
+ return {
380
+ ...finalConfig2,
381
+ ...userConfig
382
+ };
383
+ } else {
384
+ return finalConfig2;
385
+ }
386
+ };
387
+ });
388
+ api.modifyWebpackConfig(async (config) => {
389
+ const finalDefaultConfig = await presets.apply("webpackFinal", config, options);
390
+ return finalDefaultConfig;
391
+ });
392
+ }
333
393
  // Annotate the CommonJS export names for ESM import in node:
334
394
  0 && (module.exports = {
335
395
  finalize,
package/dist/cjs/utils.js CHANGED
@@ -59,14 +59,15 @@ async function getProvider(bundler, builderConfig) {
59
59
  return builderWebpackProvider({
60
60
  builderConfig
61
61
  });
62
- } else {
62
+ } else if (bundler === "rspack") {
63
63
  const { builderRspackProvider } = await Promise.resolve().then(() => __toESM(require("@modern-js/builder-rspack-provider")));
64
64
  return builderRspackProvider({
65
65
  builderConfig
66
66
  });
67
+ } else {
68
+ return await getProvider("webpack", builderConfig) || await getProvider("rspack", builderConfig);
67
69
  }
68
70
  } catch (e) {
69
- import_utils.logger.error(`Cannot find provider, you need to install @modern-js/builder-${bundler}-provider first`);
70
71
  }
71
72
  }
72
73
  async function virtualModule(tempDir, cwd, virtualModuleMap) {
package/dist/esm/core.js CHANGED
@@ -1,9 +1,9 @@
1
- import { createBuilder } from "@modern-js/builder";
1
+ import { createBuilder, mergeBuilderConfig } from "@modern-js/builder";
2
2
  import { loadConfig } from "@modern-js/core";
3
3
  import { getConfigFileName, getProvider, runWithErrorMsg } from "./utils";
4
4
  import { pluginStorybook } from "./plugin-storybook";
5
5
  async function getCompiler(cwd, builderOptions, options) {
6
- const bundler = builderOptions.bundler || "webpack";
6
+ const { bundler } = builderOptions;
7
7
  const { presets } = options;
8
8
  const entries = await presets.apply("entries", []);
9
9
  const res = await runWithErrorMsg(
@@ -12,9 +12,20 @@ async function getCompiler(cwd, builderOptions, options) {
12
12
  );
13
13
  const loadedConfig = res ? res.config : {};
14
14
  const finalConfig = await presets.apply("modern", loadedConfig) || loadedConfig;
15
- const provider = await getProvider(bundler, finalConfig);
15
+ const provider = await getProvider(
16
+ bundler,
17
+ mergeBuilderConfig(finalConfig, builderOptions.builderConfig) || {}
18
+ );
16
19
  if (!provider) {
17
- throw new Error(`@modern-js/builder-${bundler}-provider not found `);
20
+ if (bundler) {
21
+ throw new Error(
22
+ `You choose to use ${bundler}, but @modern-js/builder-${bundler}-provider not found in your project, please install it`
23
+ );
24
+ } else {
25
+ throw new Error(
26
+ `Please install one provider first, try install @modern-js/builder-rspack-provider or @modern-js/builder-webpack-provider first`
27
+ );
28
+ }
18
29
  }
19
30
  const builder = await createBuilder(provider, {
20
31
  cwd,
@@ -1,5 +1,11 @@
1
1
  import { isAbsolute, join, resolve } from "path";
2
- import { slash, watch, globby, applyOptionsChain } from "@modern-js/utils";
2
+ import {
3
+ slash,
4
+ watch,
5
+ globby,
6
+ applyOptionsChain,
7
+ logger
8
+ } from "@modern-js/utils";
3
9
  import {
4
10
  mergeBuilderConfig
5
11
  } from "@modern-js/builder-shared";
@@ -73,6 +79,7 @@ const pluginStorybook = (cwd, options) => {
73
79
  await applyCsfPlugin(config, options);
74
80
  };
75
81
  if ("modifyWebpackConfig" in api) {
82
+ addonAdapter(api, options);
76
83
  api.modifyWebpackConfig(modifyConfig);
77
84
  api.modifyWebpackChain(async (chain) => {
78
85
  await applyDocgenWebpack(chain, options);
@@ -346,6 +353,65 @@ async function watchStories(patterns, cwd, writeModule) {
346
353
  );
347
354
  return watcher;
348
355
  }
356
+ function addonAdapter(api, options) {
357
+ const { presets } = options;
358
+ api.modifyBuilderConfig(async (finalConfig) => {
359
+ var _a;
360
+ const babelOptions = await presets.apply("babel", {}, { ...options });
361
+ (_a = finalConfig.tools) != null ? _a : finalConfig.tools = {};
362
+ const userConfig = finalConfig.tools.babel;
363
+ finalConfig.tools.babel = (config, utils) => {
364
+ const getPluginName = (plugin) => Array.isArray(plugin) ? plugin[0] : plugin;
365
+ const getOptions = (plugin) => Array.isArray(plugin) ? plugin[1] : null;
366
+ const replaceOrInsert = (plugin, plugins) => {
367
+ const pluginName = getPluginName(plugin);
368
+ const append = [];
369
+ for (let i = 0; i < plugins.length; i++) {
370
+ if (getPluginName(plugins[i]) === pluginName) {
371
+ if (getOptions(plugin)) {
372
+ logger.info(
373
+ `Detected duplicated babel plugin or presets: ${pluginName}, overrides with the new one`
374
+ );
375
+ plugins[i] = plugin;
376
+ }
377
+ } else {
378
+ append.push(plugin);
379
+ }
380
+ }
381
+ plugins.push(...append);
382
+ };
383
+ const currentPlugins = config.plugins || [];
384
+ const currentPresets = config.presets || [];
385
+ for (const plugin of babelOptions.plugins || []) {
386
+ replaceOrInsert(plugin, currentPlugins);
387
+ }
388
+ for (const preset of babelOptions.presets || []) {
389
+ replaceOrInsert(preset, currentPresets);
390
+ }
391
+ const finalConfig2 = {
392
+ ...config,
393
+ ...babelOptions,
394
+ plugins: currentPlugins,
395
+ presets: currentPresets
396
+ };
397
+ if (typeof userConfig === "function") {
398
+ return userConfig(finalConfig2, utils);
399
+ } else if (typeof userConfig === "object") {
400
+ return { ...finalConfig2, ...userConfig };
401
+ } else {
402
+ return finalConfig2;
403
+ }
404
+ };
405
+ });
406
+ api.modifyWebpackConfig(async (config) => {
407
+ const finalDefaultConfig = await presets.apply(
408
+ "webpackFinal",
409
+ config,
410
+ options
411
+ );
412
+ return finalDefaultConfig;
413
+ });
414
+ }
349
415
  export {
350
416
  finalize,
351
417
  pluginStorybook
package/dist/esm/utils.js CHANGED
@@ -15,16 +15,15 @@ async function getProvider(bundler, builderConfig) {
15
15
  return builderWebpackProvider({
16
16
  builderConfig
17
17
  });
18
- } else {
18
+ } else if (bundler === "rspack") {
19
19
  const { builderRspackProvider } = await import("@modern-js/builder-rspack-provider");
20
20
  return builderRspackProvider({
21
21
  builderConfig
22
22
  });
23
+ } else {
24
+ return await getProvider("webpack", builderConfig) || await getProvider("rspack", builderConfig);
23
25
  }
24
26
  } catch (e) {
25
- logger.error(
26
- `Cannot find provider, you need to install @modern-js/builder-${bundler}-provider first`
27
- );
28
27
  }
29
28
  }
30
29
  async function virtualModule(tempDir, cwd, virtualModuleMap) {
@@ -1,9 +1,11 @@
1
+ import { BuilderWebpackProvider } from '@modern-js/builder-webpack-provider';
2
+ import { BuilderRspackProvider } from '@modern-js/builder-rspack-provider';
1
3
  import { AllBuilderConfig } from './types';
2
4
  export declare const VIRTUAL_MODULE_BASE = ".MODERN_STORYBOOK";
3
5
  export declare const STORIES_FILENAME = "storybook-stories.js";
4
6
  export declare const STORYBOOK_CONFIG_ENTRY = "storybook-config-entry.js";
5
7
  export declare const requireResolve: (importer: string, path: string) => void;
6
- export declare function getProvider(bundler: 'webpack' | 'rspack', builderConfig: AllBuilderConfig): Promise<import("@modern-js/builder-webpack-provider").BuilderWebpackProvider | import("@modern-js/builder-rspack-provider").BuilderRspackProvider | undefined>;
8
+ export declare function getProvider(bundler: 'webpack' | 'rspack' | undefined, builderConfig: AllBuilderConfig): Promise<BuilderWebpackProvider | BuilderRspackProvider | undefined>;
7
9
  export declare function virtualModule(tempDir: string, cwd: string, virtualModuleMap: Record<string, string>): Promise<[Record<string, string>, (p: string, content: string) => void]>;
8
10
  export declare function toImportFn(cwd: string, stories: string[]): Promise<string>;
9
11
  export declare function getAbsolutePath<I extends string>(input: I): I;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@modern-js/storybook-builder",
3
- "version": "2.40.0",
3
+ "version": "2.41.0",
4
4
  "description": "modern.js support for storybook",
5
5
  "repository": {
6
6
  "type": "git",
@@ -17,19 +17,19 @@
17
17
  ],
18
18
  "exports": {
19
19
  ".": {
20
- "jsnext:source": "./src/index.ts",
21
20
  "types": "./dist/types/index.d.ts",
21
+ "jsnext:source": "./src/index.ts",
22
22
  "default": "./dist/cjs/index.js"
23
23
  },
24
24
  "./preset": "./dist/esm/preset.js",
25
25
  "./addons": {
26
- "jsnext:source": "./src/addons/preset/preview.ts",
27
26
  "types": "./dist/types/addons/preset/preview.d.ts",
27
+ "jsnext:source": "./src/addons/preset/preview.ts",
28
28
  "default": "./dist/esm/addons/preset/preview.js"
29
29
  },
30
30
  "./types": {
31
- "jsnext:source": "./src/types.ts",
32
31
  "types": "./dist/types/types.d.ts",
32
+ "jsnext:source": "./src/types.ts",
33
33
  "default": "./dist/cjs/types.js"
34
34
  },
35
35
  "./templates/preview.ejs": "./templates/preview.ejs",
@@ -51,7 +51,7 @@
51
51
  },
52
52
  "license": "MIT",
53
53
  "dependencies": {
54
- "@rspack/dev-client": "0.3.2",
54
+ "@rspack/dev-client": "0.4.0",
55
55
  "@storybook/components": "^7.5.1",
56
56
  "@storybook/core-common": "^7.5.1",
57
57
  "@storybook/csf-plugin": "^7.5.1",
@@ -71,11 +71,11 @@
71
71
  "serve-static": "^1.14.1",
72
72
  "tinypool": "^0.8.0",
73
73
  "webpack-hot-middleware": "^2.25.4",
74
- "@modern-js/builder": "2.40.0",
75
- "@modern-js/builder-shared": "2.40.0",
76
- "@modern-js/core": "2.40.0",
77
- "@modern-js/runtime": "2.40.0",
78
- "@modern-js/utils": "2.40.0"
74
+ "@modern-js/builder": "2.41.0",
75
+ "@modern-js/runtime": "2.41.0",
76
+ "@modern-js/builder-shared": "2.41.0",
77
+ "@modern-js/core": "2.41.0",
78
+ "@modern-js/utils": "2.41.0"
79
79
  },
80
80
  "devDependencies": {
81
81
  "@storybook/types": "^7.5.1",
@@ -83,10 +83,22 @@
83
83
  "@types/serve-static": "^1.13.10",
84
84
  "@types/webpack-hot-middleware": "^2.25.6",
85
85
  "typescript": "^5.2.2",
86
- "@modern-js/builder-rspack-provider": "2.40.0",
87
- "@modern-js/builder-webpack-provider": "2.40.0",
88
- "@modern-js/core": "2.40.0",
89
- "@scripts/build": "2.40.0"
86
+ "@modern-js/builder-rspack-provider": "2.41.0",
87
+ "@modern-js/core": "2.41.0",
88
+ "@modern-js/builder-webpack-provider": "2.41.0",
89
+ "@scripts/build": "2.41.0"
90
+ },
91
+ "peerDependencies": {
92
+ "@modern-js/builder-webpack-provider": "^2.41.0",
93
+ "@modern-js/builder-rspack-provider": "^2.41.0"
94
+ },
95
+ "peerDependenciesMeta": {
96
+ "@modern-js/builder-webpack-provider": {
97
+ "optional": true
98
+ },
99
+ "@modern-js/builder-rspack-provider": {
100
+ "optional": true
101
+ }
90
102
  },
91
103
  "publishConfig": {
92
104
  "registry": "https://registry.npmjs.org/",
@@ -27,13 +27,13 @@ window.__STORYBOOK_CLIENT_API__ = new ClientApi({ storyStore: preview.storyStore
27
27
 
28
28
  preview.initialize({ importFn, getProjectAnnotations });
29
29
 
30
- if (import.meta.webpackHot) {
31
- import.meta.webpackHot.accept('./{{storiesFilename}}', () => {
30
+ if (module.hot) {
31
+ module.hot.accept('./{{storiesFilename}}', () => {
32
32
  // importFn has changed so we need to patch the new one in
33
33
  preview.onStoriesChanged({ importFn });
34
34
  });
35
35
 
36
- import.meta.webpackHot.accept([
36
+ module.hot.accept([
37
37
  {{#each previewAnnotations}}'{{this}}',
38
38
  {{/each}}
39
39
  ], () => {