@storybook/core-webpack 7.0.0-alpha.1 → 7.0.0-alpha.12

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,9 +1,5 @@
1
1
  "use strict";
2
2
 
3
- require("core-js/modules/es.array.slice.js");
4
-
5
- require("core-js/modules/es.object.freeze.js");
6
-
7
3
  Object.defineProperty(exports, "__esModule", {
8
4
  value: true
9
5
  });
@@ -11,15 +7,9 @@ exports.checkWebpackVersion = void 0;
11
7
 
12
8
  var _nodeLogger = require("@storybook/node-logger");
13
9
 
14
- var _tsDedent = _interopRequireDefault(require("ts-dedent"));
15
-
16
- var _templateObject;
17
-
18
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
+ var _tsDedent = require("ts-dedent");
19
11
 
20
- function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
21
-
22
- var checkWebpackVersion = function checkWebpackVersion(webpack, specifier, caption) {
12
+ const checkWebpackVersion = (webpack, specifier, caption) => {
23
13
  if (!webpack.version) {
24
14
  _nodeLogger.logger.info('Skipping webpack version check, no version available');
25
15
 
@@ -27,7 +17,15 @@ var checkWebpackVersion = function checkWebpackVersion(webpack, specifier, capti
27
17
  }
28
18
 
29
19
  if (webpack.version !== specifier) {
30
- _nodeLogger.logger.warn((0, _tsDedent.default)(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n Unexpected webpack version in ", ":\n - Received '", "'\n - Expected '", "'\n\n 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\n\n For more info about Webpack 5 support: https://gist.github.com/shilman/8856ea1786dcd247139b47b270912324#troubleshooting\n "])), caption, webpack.version, specifier));
20
+ _nodeLogger.logger.warn((0, _tsDedent.dedent)`
21
+ Unexpected webpack version in ${caption}:
22
+ - Received '${webpack.version}'
23
+ - Expected '${specifier}'
24
+
25
+ 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
26
+
27
+ For more info about Webpack 5 support: https://gist.github.com/shilman/8856ea1786dcd247139b47b270912324#troubleshooting
28
+ `);
31
29
  }
32
30
  };
33
31
 
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.importPipeline = importPipeline;
7
+
8
+ // If an import is in flight when another import arrives, block it until the first completes.
9
+ // This is to avoid a situation where webpack kicks off a second compilation whilst the
10
+ // first is still completing, cf: https://github.com/webpack/webpack/issues/15541#issuecomment-1143138832
11
+ // Note the way this code works if N futher `import()`s occur while the first is in flight,
12
+ // they will all be kicked off in the same tick and not block each other. This is by design,
13
+ // Webpack can handle multiple invalidations simutaneously, just not in quick succession.
14
+ function importPipeline() {
15
+ let importGate = Promise.resolve();
16
+ return async importFn => {
17
+ await importGate;
18
+ const moduleExportsPromise = importFn();
19
+ importGate = importGate.then(async () => {
20
+ await moduleExportsPromise;
21
+ });
22
+ return moduleExportsPromise;
23
+ };
24
+ }
package/dist/cjs/index.js CHANGED
@@ -1,11 +1,5 @@
1
1
  "use strict";
2
2
 
3
- require("core-js/modules/es.object.to-string.js");
4
-
5
- require("core-js/modules/web.dom-collections.for-each.js");
6
-
7
- require("core-js/modules/es.object.keys.js");
8
-
9
3
  Object.defineProperty(exports, "__esModule", {
10
4
  value: true
11
5
  });
@@ -17,7 +11,7 @@ Object.keys(_types).forEach(function (key) {
17
11
  if (key in exports && exports[key] === _types[key]) return;
18
12
  Object.defineProperty(exports, key, {
19
13
  enumerable: true,
20
- get: function get() {
14
+ get: function () {
21
15
  return _types[key];
22
16
  }
23
17
  });
@@ -30,7 +24,7 @@ Object.keys(_loadCustomWebpackConfig).forEach(function (key) {
30
24
  if (key in exports && exports[key] === _loadCustomWebpackConfig[key]) return;
31
25
  Object.defineProperty(exports, key, {
32
26
  enumerable: true,
33
- get: function get() {
27
+ get: function () {
34
28
  return _loadCustomWebpackConfig[key];
35
29
  }
36
30
  });
@@ -43,7 +37,7 @@ Object.keys(_checkWebpackVersion).forEach(function (key) {
43
37
  if (key in exports && exports[key] === _checkWebpackVersion[key]) return;
44
38
  Object.defineProperty(exports, key, {
45
39
  enumerable: true,
46
- get: function get() {
40
+ get: function () {
47
41
  return _checkWebpackVersion[key];
48
42
  }
49
43
  });
@@ -56,8 +50,34 @@ Object.keys(_mergeWebpackConfig).forEach(function (key) {
56
50
  if (key in exports && exports[key] === _mergeWebpackConfig[key]) return;
57
51
  Object.defineProperty(exports, key, {
58
52
  enumerable: true,
59
- get: function get() {
53
+ get: function () {
60
54
  return _mergeWebpackConfig[key];
61
55
  }
62
56
  });
57
+ });
58
+
59
+ var _toImportFn = require("./to-importFn");
60
+
61
+ Object.keys(_toImportFn).forEach(function (key) {
62
+ if (key === "default" || key === "__esModule") return;
63
+ if (key in exports && exports[key] === _toImportFn[key]) return;
64
+ Object.defineProperty(exports, key, {
65
+ enumerable: true,
66
+ get: function () {
67
+ return _toImportFn[key];
68
+ }
69
+ });
70
+ });
71
+
72
+ var _toRequireContext = require("./to-require-context");
73
+
74
+ Object.keys(_toRequireContext).forEach(function (key) {
75
+ if (key === "default" || key === "__esModule") return;
76
+ if (key in exports && exports[key] === _toRequireContext[key]) return;
77
+ Object.defineProperty(exports, key, {
78
+ enumerable: true,
79
+ get: function () {
80
+ return _toRequireContext[key];
81
+ }
82
+ });
63
83
  });
@@ -5,20 +5,14 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.loadCustomWebpackConfig = void 0;
7
7
 
8
- require("core-js/modules/es.array.map.js");
9
-
10
8
  var _path = _interopRequireDefault(require("path"));
11
9
 
12
10
  var _coreCommon = require("@storybook/core-common");
13
11
 
14
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
13
 
16
- var webpackConfigs = ['webpack.config', 'webpackfile'];
14
+ const webpackConfigs = ['webpack.config', 'webpackfile'];
17
15
 
18
- var loadCustomWebpackConfig = function loadCustomWebpackConfig(configDir) {
19
- return (0, _coreCommon.serverRequire)(webpackConfigs.map(function (configName) {
20
- return _path.default.resolve(configDir, configName);
21
- }));
22
- };
16
+ const loadCustomWebpackConfig = configDir => (0, _coreCommon.serverRequire)(webpackConfigs.map(configName => _path.default.resolve(configDir, configName)));
23
17
 
24
18
  exports.loadCustomWebpackConfig = loadCustomWebpackConfig;
@@ -1,119 +1,65 @@
1
1
  "use strict";
2
2
 
3
- require("core-js/modules/es.symbol.js");
4
-
5
- require("core-js/modules/es.symbol.description.js");
6
-
7
- require("core-js/modules/es.object.to-string.js");
8
-
9
- require("core-js/modules/es.symbol.iterator.js");
10
-
11
- require("core-js/modules/es.array.iterator.js");
12
-
13
- require("core-js/modules/es.string.iterator.js");
14
-
15
- require("core-js/modules/web.dom-collections.iterator.js");
16
-
17
- require("core-js/modules/es.array.from.js");
18
-
19
- require("core-js/modules/es.array.slice.js");
20
-
21
- require("core-js/modules/es.function.name.js");
22
-
23
- require("core-js/modules/es.regexp.exec.js");
24
-
25
3
  Object.defineProperty(exports, "__esModule", {
26
4
  value: true
27
5
  });
28
6
  exports.mergeConfigs = mergeConfigs;
29
7
 
30
- require("core-js/modules/es.array.concat.js");
31
-
32
- require("core-js/modules/es.object.assign.js");
33
-
34
- function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
35
-
36
- function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
37
-
38
- function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
39
-
40
- function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
41
-
42
- function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
43
-
44
- function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
45
-
46
- function plugins(_ref, _ref2) {
47
- var _ref$plugins = _ref.plugins,
48
- defaultPlugins = _ref$plugins === void 0 ? [] : _ref$plugins;
49
- var _ref2$plugins = _ref2.plugins,
50
- customPlugins = _ref2$plugins === void 0 ? [] : _ref2$plugins;
51
- return [].concat(_toConsumableArray(defaultPlugins), _toConsumableArray(customPlugins));
8
+ function mergePluginsField(defaultPlugins = [], customPlugins = []) {
9
+ return [...defaultPlugins, ...customPlugins];
52
10
  }
53
11
 
54
- function rules(_ref3, _ref4) {
55
- var _ref3$rules = _ref3.rules,
56
- defaultRules = _ref3$rules === void 0 ? [] : _ref3$rules;
57
- var _ref4$rules = _ref4.rules,
58
- customRules = _ref4$rules === void 0 ? [] : _ref4$rules;
59
- return [].concat(_toConsumableArray(defaultRules), _toConsumableArray(customRules));
12
+ function mergeRulesField(defaultRules = [], customRules = []) {
13
+ return [...defaultRules, ...customRules];
60
14
  }
61
15
 
62
- function extensions(_ref5, _ref6) {
63
- var _ref5$extensions = _ref5.extensions,
64
- defaultExtensions = _ref5$extensions === void 0 ? [] : _ref5$extensions;
65
- var _ref6$extensions = _ref6.extensions,
66
- customExtensions = _ref6$extensions === void 0 ? [] : _ref6$extensions;
67
- return [].concat(_toConsumableArray(defaultExtensions), _toConsumableArray(customExtensions));
16
+ function mergeExtensionsField({
17
+ extensions: defaultExtensions = []
18
+ }, {
19
+ extensions: customExtensions = []
20
+ }) {
21
+ return [...defaultExtensions, ...customExtensions];
68
22
  }
69
23
 
70
- function alias(_ref7, _ref8) {
71
- var _ref7$alias = _ref7.alias,
72
- defaultAlias = _ref7$alias === void 0 ? {} : _ref7$alias;
73
- var _ref8$alias = _ref8.alias,
74
- customAlias = _ref8$alias === void 0 ? {} : _ref8$alias;
24
+ function mergeAliasField({
25
+ alias: defaultAlias = {}
26
+ }, {
27
+ alias: customAlias = {}
28
+ }) {
75
29
  return Object.assign({}, defaultAlias, customAlias);
76
30
  }
77
31
 
78
- function _module(_ref9, _ref10) {
79
- var _ref9$module = _ref9.module,
80
- defaultModule = _ref9$module === void 0 ? {
81
- rules: []
82
- } : _ref9$module;
83
- var _ref10$module = _ref10.module,
84
- customModule = _ref10$module === void 0 ? {
85
- rules: []
86
- } : _ref10$module;
87
- return Object.assign({}, defaultModule, customModule, {
88
- rules: rules(defaultModule, customModule)
32
+ function mergeModuleField(a, b) {
33
+ return Object.assign({}, a, b, {
34
+ rules: mergeRulesField(a.rules || [], b.rules || [])
89
35
  });
90
36
  }
91
37
 
92
- function resolve(_ref11, _ref12) {
93
- var _ref11$resolve = _ref11.resolve,
94
- defaultResolve = _ref11$resolve === void 0 ? {} : _ref11$resolve;
95
- var _ref12$resolve = _ref12.resolve,
96
- customResolve = _ref12$resolve === void 0 ? {} : _ref12$resolve;
38
+ function mergeResolveField({
39
+ resolve: defaultResolve = {}
40
+ }, {
41
+ resolve: customResolve = {}
42
+ }) {
97
43
  return Object.assign({}, defaultResolve, customResolve, {
98
- alias: alias(defaultResolve, customResolve),
99
- extensions: extensions(defaultResolve, customResolve)
44
+ alias: mergeAliasField(defaultResolve, customResolve),
45
+ extensions: mergeExtensionsField(defaultResolve, customResolve)
100
46
  });
101
47
  }
102
48
 
103
- function optimization(_ref13, _ref14) {
104
- var _ref13$optimization = _ref13.optimization,
105
- defaultOptimization = _ref13$optimization === void 0 ? {} : _ref13$optimization;
106
- var _ref14$optimization = _ref14.optimization,
107
- customOptimization = _ref14$optimization === void 0 ? {} : _ref14$optimization;
49
+ function mergeOptimizationField({
50
+ optimization: defaultOptimization = {}
51
+ }, {
52
+ optimization: customOptimization = {}
53
+ }) {
108
54
  return Object.assign({}, defaultOptimization, customOptimization);
109
55
  }
110
56
 
111
57
  function mergeConfigs(config, customConfig) {
112
58
  return Object.assign({}, customConfig, config, {
113
59
  devtool: customConfig.devtool || config.devtool,
114
- plugins: plugins(config, customConfig),
115
- module: _module(config, customConfig),
116
- resolve: resolve(config, customConfig),
117
- optimization: optimization(config, customConfig)
60
+ plugins: mergePluginsField(config.plugins, customConfig.plugins),
61
+ module: mergeModuleField(config.module || {}, customConfig.module || {}),
62
+ resolve: mergeResolveField(config, customConfig),
63
+ optimization: mergeOptimizationField(config, customConfig)
118
64
  });
119
65
  }
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.toImportFn = toImportFn;
7
+ exports.toImportFnPart = toImportFnPart;
8
+ exports.webpackIncludeRegexp = webpackIncludeRegexp;
9
+
10
+ var _tsDedent = require("ts-dedent");
11
+
12
+ var _coreCommon = require("@storybook/core-common");
13
+
14
+ var _importPipeline = require("./importPipeline");
15
+
16
+ function webpackIncludeRegexp(specifier) {
17
+ const {
18
+ directory,
19
+ files
20
+ } = specifier; // It appears webpack passes *something* similar to the absolute path to the file
21
+ // on disk (prefixed with something unknown) to the matcher.
22
+ // We don't want to include the absolute path in our bundle, so we will just pull any leading
23
+ // `./` or `../` off our directory and match on that.
24
+ // It's imperfect as it could match extra things in extremely unusual cases, but it'll do for now.
25
+ // NOTE: directory is "slashed" so will contain only `/` (no `\`), even on windows
26
+
27
+ const directoryWithoutLeadingDots = directory.replace(/^(\.+\/)+/, '/');
28
+ const webpackIncludeGlob = ['.', '..'].includes(directory) ? files : `${directoryWithoutLeadingDots}/${files}`;
29
+ const webpackIncludeRegexpWithCaret = (0, _coreCommon.globToRegexp)(webpackIncludeGlob); // picomatch is creating an exact match, but we are only matching the end of the filename
30
+
31
+ return new RegExp(webpackIncludeRegexpWithCaret.source.replace(/^\^/, ''));
32
+ }
33
+
34
+ function toImportFnPart(specifier) {
35
+ const {
36
+ directory,
37
+ importPathMatcher
38
+ } = specifier;
39
+ return (0, _tsDedent.dedent)`
40
+ async (path) => {
41
+ if (!${importPathMatcher}.exec(path)) {
42
+ return;
43
+ }
44
+
45
+ const pathRemainder = path.substring(${directory.length + 1});
46
+ return import(
47
+ /* webpackChunkName: "[request]" */
48
+ /* webpackInclude: ${webpackIncludeRegexp(specifier)} */
49
+ '${directory}/' + pathRemainder
50
+ );
51
+ }
52
+
53
+ `;
54
+ }
55
+
56
+ function toImportFn(stories, {
57
+ needPipelinedImport
58
+ } = {}) {
59
+ let pipelinedImport = `const pipeline = (x) => x();`;
60
+
61
+ if (needPipelinedImport) {
62
+ pipelinedImport = `
63
+ const importPipeline = ${_importPipeline.importPipeline};
64
+ const pipeline = importPipeline();
65
+ `;
66
+ }
67
+
68
+ return (0, _tsDedent.dedent)`
69
+ ${pipelinedImport}
70
+
71
+ const importers = [
72
+ ${stories.map(toImportFnPart).join(',\n')}
73
+ ];
74
+
75
+ export async function importFn(path) {
76
+ for (let i = 0; i < importers.length; i++) {
77
+ const moduleExports = await pipeline(() => importers[i](path));
78
+ if (moduleExports) {
79
+ return moduleExports;
80
+ }
81
+ }
82
+ }
83
+ `;
84
+ }
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.toRequireContextString = exports.toRequireContext = void 0;
7
+
8
+ var _coreCommon = require("@storybook/core-common");
9
+
10
+ const toRequireContext = specifier => {
11
+ const {
12
+ directory,
13
+ files
14
+ } = specifier; // The importPathMatcher is a `./`-prefixed matcher that includes the directory
15
+ // For `require.context()` we want the same thing, relative to directory
16
+
17
+ const match = (0, _coreCommon.globToRegexp)(`./${files}`);
18
+ return {
19
+ path: directory,
20
+ recursive: files.includes('**') || files.split('/').length > 1,
21
+ match
22
+ };
23
+ };
24
+
25
+ exports.toRequireContext = toRequireContext;
26
+
27
+ const toRequireContextString = specifier => {
28
+ const {
29
+ path: p,
30
+ recursive: r,
31
+ match: m
32
+ } = toRequireContext(specifier);
33
+ const result = `require.context('${p}', ${r}, ${m})`;
34
+ return result;
35
+ };
36
+
37
+ exports.toRequireContextString = toRequireContextString;
@@ -1,5 +1,5 @@
1
1
  import { logger } from '@storybook/node-logger';
2
- import dedent from 'ts-dedent';
2
+ import { dedent } from 'ts-dedent';
3
3
  export const checkWebpackVersion = (webpack, specifier, caption) => {
4
4
  if (!webpack.version) {
5
5
  logger.info('Skipping webpack version check, no version available');
@@ -0,0 +1,17 @@
1
+ // If an import is in flight when another import arrives, block it until the first completes.
2
+ // This is to avoid a situation where webpack kicks off a second compilation whilst the
3
+ // first is still completing, cf: https://github.com/webpack/webpack/issues/15541#issuecomment-1143138832
4
+ // Note the way this code works if N futher `import()`s occur while the first is in flight,
5
+ // they will all be kicked off in the same tick and not block each other. This is by design,
6
+ // Webpack can handle multiple invalidations simutaneously, just not in quick succession.
7
+ export function importPipeline() {
8
+ let importGate = Promise.resolve();
9
+ return async importFn => {
10
+ await importGate;
11
+ const moduleExportsPromise = importFn();
12
+ importGate = importGate.then(async () => {
13
+ await moduleExportsPromise;
14
+ });
15
+ return moduleExportsPromise;
16
+ };
17
+ }
package/dist/esm/index.js CHANGED
@@ -1,4 +1,6 @@
1
1
  export * from './types';
2
2
  export * from './load-custom-webpack-config';
3
3
  export * from './check-webpack-version';
4
- export * from './merge-webpack-config';
4
+ export * from './merge-webpack-config';
5
+ export * from './to-importFn';
6
+ export * from './to-require-context';
@@ -1,20 +1,12 @@
1
- function plugins({
2
- plugins: defaultPlugins = []
3
- }, {
4
- plugins: customPlugins = []
5
- }) {
1
+ function mergePluginsField(defaultPlugins = [], customPlugins = []) {
6
2
  return [...defaultPlugins, ...customPlugins];
7
3
  }
8
4
 
9
- function rules({
10
- rules: defaultRules = []
11
- }, {
12
- rules: customRules = []
13
- }) {
5
+ function mergeRulesField(defaultRules = [], customRules = []) {
14
6
  return [...defaultRules, ...customRules];
15
7
  }
16
8
 
17
- function extensions({
9
+ function mergeExtensionsField({
18
10
  extensions: defaultExtensions = []
19
11
  }, {
20
12
  extensions: customExtensions = []
@@ -22,7 +14,7 @@ function extensions({
22
14
  return [...defaultExtensions, ...customExtensions];
23
15
  }
24
16
 
25
- function alias({
17
+ function mergeAliasField({
26
18
  alias: defaultAlias = {}
27
19
  }, {
28
20
  alias: customAlias = {}
@@ -30,32 +22,24 @@ function alias({
30
22
  return Object.assign({}, defaultAlias, customAlias);
31
23
  }
32
24
 
33
- function module({
34
- module: defaultModule = {
35
- rules: []
36
- }
37
- }, {
38
- module: customModule = {
39
- rules: []
40
- }
41
- }) {
42
- return Object.assign({}, defaultModule, customModule, {
43
- rules: rules(defaultModule, customModule)
25
+ function mergeModuleField(a, b) {
26
+ return Object.assign({}, a, b, {
27
+ rules: mergeRulesField(a.rules || [], b.rules || [])
44
28
  });
45
29
  }
46
30
 
47
- function resolve({
31
+ function mergeResolveField({
48
32
  resolve: defaultResolve = {}
49
33
  }, {
50
34
  resolve: customResolve = {}
51
35
  }) {
52
36
  return Object.assign({}, defaultResolve, customResolve, {
53
- alias: alias(defaultResolve, customResolve),
54
- extensions: extensions(defaultResolve, customResolve)
37
+ alias: mergeAliasField(defaultResolve, customResolve),
38
+ extensions: mergeExtensionsField(defaultResolve, customResolve)
55
39
  });
56
40
  }
57
41
 
58
- function optimization({
42
+ function mergeOptimizationField({
59
43
  optimization: defaultOptimization = {}
60
44
  }, {
61
45
  optimization: customOptimization = {}
@@ -66,9 +50,9 @@ function optimization({
66
50
  export function mergeConfigs(config, customConfig) {
67
51
  return Object.assign({}, customConfig, config, {
68
52
  devtool: customConfig.devtool || config.devtool,
69
- plugins: plugins(config, customConfig),
70
- module: module(config, customConfig),
71
- resolve: resolve(config, customConfig),
72
- optimization: optimization(config, customConfig)
53
+ plugins: mergePluginsField(config.plugins, customConfig.plugins),
54
+ module: mergeModuleField(config.module || {}, customConfig.module || {}),
55
+ resolve: mergeResolveField(config, customConfig),
56
+ optimization: mergeOptimizationField(config, customConfig)
73
57
  });
74
58
  }
@@ -0,0 +1,70 @@
1
+ import { dedent } from 'ts-dedent';
2
+ import { globToRegexp } from '@storybook/core-common';
3
+ import { importPipeline } from './importPipeline';
4
+ export function webpackIncludeRegexp(specifier) {
5
+ const {
6
+ directory,
7
+ files
8
+ } = specifier; // It appears webpack passes *something* similar to the absolute path to the file
9
+ // on disk (prefixed with something unknown) to the matcher.
10
+ // We don't want to include the absolute path in our bundle, so we will just pull any leading
11
+ // `./` or `../` off our directory and match on that.
12
+ // It's imperfect as it could match extra things in extremely unusual cases, but it'll do for now.
13
+ // NOTE: directory is "slashed" so will contain only `/` (no `\`), even on windows
14
+
15
+ const directoryWithoutLeadingDots = directory.replace(/^(\.+\/)+/, '/');
16
+ const webpackIncludeGlob = ['.', '..'].includes(directory) ? files : `${directoryWithoutLeadingDots}/${files}`;
17
+ const webpackIncludeRegexpWithCaret = globToRegexp(webpackIncludeGlob); // picomatch is creating an exact match, but we are only matching the end of the filename
18
+
19
+ return new RegExp(webpackIncludeRegexpWithCaret.source.replace(/^\^/, ''));
20
+ }
21
+ export function toImportFnPart(specifier) {
22
+ const {
23
+ directory,
24
+ importPathMatcher
25
+ } = specifier;
26
+ return dedent`
27
+ async (path) => {
28
+ if (!${importPathMatcher}.exec(path)) {
29
+ return;
30
+ }
31
+
32
+ const pathRemainder = path.substring(${directory.length + 1});
33
+ return import(
34
+ /* webpackChunkName: "[request]" */
35
+ /* webpackInclude: ${webpackIncludeRegexp(specifier)} */
36
+ '${directory}/' + pathRemainder
37
+ );
38
+ }
39
+
40
+ `;
41
+ }
42
+ export function toImportFn(stories, {
43
+ needPipelinedImport
44
+ } = {}) {
45
+ let pipelinedImport = `const pipeline = (x) => x();`;
46
+
47
+ if (needPipelinedImport) {
48
+ pipelinedImport = `
49
+ const importPipeline = ${importPipeline};
50
+ const pipeline = importPipeline();
51
+ `;
52
+ }
53
+
54
+ return dedent`
55
+ ${pipelinedImport}
56
+
57
+ const importers = [
58
+ ${stories.map(toImportFnPart).join(',\n')}
59
+ ];
60
+
61
+ export async function importFn(path) {
62
+ for (let i = 0; i < importers.length; i++) {
63
+ const moduleExports = await pipeline(() => importers[i](path));
64
+ if (moduleExports) {
65
+ return moduleExports;
66
+ }
67
+ }
68
+ }
69
+ `;
70
+ }
@@ -0,0 +1,24 @@
1
+ import { globToRegexp } from '@storybook/core-common';
2
+ export const toRequireContext = specifier => {
3
+ const {
4
+ directory,
5
+ files
6
+ } = specifier; // The importPathMatcher is a `./`-prefixed matcher that includes the directory
7
+ // For `require.context()` we want the same thing, relative to directory
8
+
9
+ const match = globToRegexp(`./${files}`);
10
+ return {
11
+ path: directory,
12
+ recursive: files.includes('**') || files.split('/').length > 1,
13
+ match
14
+ };
15
+ };
16
+ export const toRequireContextString = specifier => {
17
+ const {
18
+ path: p,
19
+ recursive: r,
20
+ match: m
21
+ } = toRequireContext(specifier);
22
+ const result = `require.context('${p}', ${r}, ${m})`;
23
+ return result;
24
+ };
@@ -0,0 +1,3 @@
1
+ declare type ModuleExports = Record<string, any>;
2
+ export declare function importPipeline(): (importFn: () => Promise<ModuleExports>) => Promise<ModuleExports>;
3
+ export {};
@@ -2,3 +2,5 @@ export * from './types';
2
2
  export * from './load-custom-webpack-config';
3
3
  export * from './check-webpack-version';
4
4
  export * from './merge-webpack-config';
5
+ export * from './to-importFn';
6
+ export * from './to-require-context';
@@ -1,2 +1,2 @@
1
- import type { CommonWebpackConfiguration as Configuration } from './types';
1
+ import type { WebpackConfiguration as Configuration } from './types';
2
2
  export declare function mergeConfigs(config: Configuration, customConfig: Configuration): Configuration;
@@ -0,0 +1,6 @@
1
+ import type { NormalizedStoriesSpecifier } from '@storybook/core-common';
2
+ export declare function webpackIncludeRegexp(specifier: NormalizedStoriesSpecifier): RegExp;
3
+ export declare function toImportFnPart(specifier: NormalizedStoriesSpecifier): string;
4
+ export declare function toImportFn(stories: NormalizedStoriesSpecifier[], { needPipelinedImport }?: {
5
+ needPipelinedImport?: boolean;
6
+ }): string;
@@ -0,0 +1,7 @@
1
+ import type { NormalizedStoriesSpecifier } from '@storybook/core-common';
2
+ export declare const toRequireContext: (specifier: NormalizedStoriesSpecifier) => {
3
+ path: string;
4
+ recursive: boolean;
5
+ match: RegExp;
6
+ };
7
+ export declare const toRequireContextString: (specifier: NormalizedStoriesSpecifier) => string;
@@ -1,27 +1,29 @@
1
- import type { Options, StorybookConfig as BaseStorybookConfig } from '@storybook/core-common';
1
+ import type { Options, StorybookConfig as StorybookConfigBase } from '@storybook/core-common';
2
2
  export type { Options, Preset, BuilderResult, TypescriptOptions } from '@storybook/core-common';
3
- export interface CommonWebpackConfiguration {
3
+ export declare type RulesConfig = any;
4
+ export declare type ModuleConfig = {
5
+ rules?: RulesConfig[];
6
+ };
7
+ export declare type ResolveConfig = {
8
+ extensions?: string[];
9
+ mainFields?: (string | string[])[] | undefined;
10
+ alias?: any;
11
+ };
12
+ export interface WebpackConfiguration {
4
13
  plugins?: any[];
5
- module?: {
6
- rules?: any[];
7
- };
8
- resolve?: {
9
- extensions?: string[];
10
- mainFields?: string[] | string[][];
11
- alias?: any;
12
- };
14
+ module?: ModuleConfig;
15
+ resolve?: ResolveConfig;
13
16
  optimization?: any;
14
- devtool?: boolean | string;
17
+ devtool?: false | string;
15
18
  }
16
- export interface StorybookWebpackConfig<TConfiguration extends CommonWebpackConfiguration> {
19
+ export declare type StorybookConfig<TWebpackConfiguration = WebpackConfiguration> = StorybookConfigBase & {
17
20
  /**
18
21
  * Modify or return a custom Webpack config after the Storybook's default configuration
19
22
  * has run (mostly used by addons).
20
23
  */
21
- webpack?: (config: TConfiguration, options: Options) => TConfiguration | Promise<TConfiguration>;
24
+ webpack?: (config: TWebpackConfiguration, options: Options) => TWebpackConfiguration | Promise<TWebpackConfiguration>;
22
25
  /**
23
26
  * Modify or return a custom Webpack config after every addon has run.
24
27
  */
25
- webpackFinal?: (config: TConfiguration, options: Options) => TConfiguration | Promise<TConfiguration>;
26
- }
27
- export declare type StorybookConfig<TWebpackConfiguration extends CommonWebpackConfiguration = CommonWebpackConfiguration> = BaseStorybookConfig & StorybookWebpackConfig<TWebpackConfiguration>;
28
+ webpackFinal?: (config: TWebpackConfiguration, options: Options) => TWebpackConfiguration | Promise<TWebpackConfiguration>;
29
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@storybook/core-webpack",
3
- "version": "7.0.0-alpha.1",
3
+ "version": "7.0.0-alpha.12",
4
4
  "description": "Storybook framework-agnostic API",
5
5
  "keywords": [
6
6
  "storybook"
@@ -30,17 +30,21 @@
30
30
  "*.d.ts"
31
31
  ],
32
32
  "scripts": {
33
+ "check": "tsc --noEmit",
33
34
  "prepare": "node ../../scripts/prepare.js"
34
35
  },
35
36
  "dependencies": {
36
- "@storybook/core-common": "7.0.0-alpha.1",
37
- "@storybook/node-logger": "7.0.0-alpha.1",
37
+ "@storybook/core-common": "7.0.0-alpha.12",
38
+ "@storybook/node-logger": "7.0.0-alpha.12",
38
39
  "@types/node": "^14.0.10 || ^16.0.0",
39
40
  "core-js": "^3.8.2",
40
41
  "ts-dedent": "^2.0.0"
41
42
  },
43
+ "devDependencies": {
44
+ "typescript": "~4.6.3"
45
+ },
42
46
  "publishConfig": {
43
47
  "access": "public"
44
48
  },
45
- "gitHead": "b90b85210f66da59656c2ef58b0910b156256bea"
49
+ "gitHead": "5070efff271ecb5c26b3eb94c128c4896171cffe"
46
50
  }