UNPKG

7.19 kBJavaScriptView Raw
1"use strict";
2var __assign = (this && this.__assign) || function () {
3 __assign = Object.assign || function(t) {
4 for (var s, i = 1, n = arguments.length; i < n; i++) {
5 s = arguments[i];
6 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7 t[p] = s[p];
8 }
9 return t;
10 };
11 return __assign.apply(this, arguments);
12};
13var __read = (this && this.__read) || function (o, n) {
14 var m = typeof Symbol === "function" && o[Symbol.iterator];
15 if (!m) return o;
16 var i = m.call(o), r, ar = [], e;
17 try {
18 while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
19 }
20 catch (error) { e = { error: error }; }
21 finally {
22 try {
23 if (r && !r.done && (m = i["return"])) m.call(i);
24 }
25 finally { if (e) throw e.error; }
26 }
27 return ar;
28};
29var __spread = (this && this.__spread) || function () {
30 for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
31 return ar;
32};
33var __importDefault = (this && this.__importDefault) || function (mod) {
34 return (mod && mod.__esModule) ? mod : { "default": mod };
35};
36Object.defineProperty(exports, "__esModule", { value: true });
37exports.webpack = exports.managerWebpack = exports.babelDefault = void 0;
38var path_1 = require("path");
39var node_logger_1 = require("@storybook/node-logger");
40var pnp_webpack_plugin_1 = __importDefault(require("pnp-webpack-plugin"));
41var react_docgen_typescript_plugin_1 = __importDefault(require("react-docgen-typescript-plugin"));
42var mergePlugins_1 = require("./helpers/mergePlugins");
43var getReactScriptsPath_1 = require("./helpers/getReactScriptsPath");
44var processCraConfig_1 = require("./helpers/processCraConfig");
45var checkPresets_1 = require("./helpers/checkPresets");
46var getModulePath_1 = require("./helpers/getModulePath");
47var CWD = process.cwd();
48// When operating under PnP environments, this value will be set to a number
49// indicating the version of the PnP standard, see: https://yarnpkg.com/advanced/pnpapi#processversionspnp
50var IS_USING_YARN_PNP = typeof process.versions.pnp !== 'undefined';
51var REACT_SCRIPTS_PATH = IS_USING_YARN_PNP
52 ? getReactScriptsPath_1.getReactScriptsPathWithYarnPnp()
53 : getReactScriptsPath_1.getReactScriptsPath();
54var OPTION_SCRIPTS_PACKAGE = 'scriptsPackageName';
55// Ensures that assets are served from the correct path when Storybook is built.
56// Resolves: https://github.com/storybookjs/storybook/issues/4645
57if (!process.env.PUBLIC_URL) {
58 process.env.PUBLIC_URL = '.';
59}
60// This loader is shared by both the `managerWebpack` and `webpack` functions.
61var resolveLoader = {
62 modules: ['node_modules', path_1.join(REACT_SCRIPTS_PATH, 'node_modules')],
63 plugins: [pnp_webpack_plugin_1.default.moduleLoader(module)],
64};
65// Don't use Storybook's default Babel config.
66exports.babelDefault = function () { return ({
67 presets: [],
68 plugins: [],
69}); };
70// Ensure that loaders are resolved from react-scripts.
71exports.managerWebpack = function (webpackConfig) {
72 if (webpackConfig === void 0) { webpackConfig = {}; }
73 return (__assign(__assign({}, webpackConfig), { resolveLoader: resolveLoader }));
74};
75// Update the core Webpack config.
76exports.webpack = function (webpackConfig, options) {
77 if (webpackConfig === void 0) { webpackConfig = {}; }
78 var scriptsPath = REACT_SCRIPTS_PATH;
79 // Flag any potentially conflicting presets.
80 checkPresets_1.checkPresets(options);
81 // If the user has provided a package by name, try to resolve it.
82 var scriptsPackageName = options[OPTION_SCRIPTS_PACKAGE];
83 if (typeof scriptsPackageName === 'string') {
84 try {
85 scriptsPath = IS_USING_YARN_PNP
86 ? getReactScriptsPath_1.getReactScriptsPathWithYarnPnp(scriptsPackageName)
87 : path_1.dirname(require.resolve(scriptsPackageName + "/package.json"));
88 }
89 catch (e) {
90 node_logger_1.logger.warn("A `" + OPTION_SCRIPTS_PACKAGE + "` was provided, but couldn't be resolved.");
91 }
92 }
93 // If there isn't a scripts-path set, return the Webpack config unmodified.
94 if (!scriptsPath) {
95 node_logger_1.logger.error('Failed to resolve a `react-scripts` package.');
96 return webpackConfig;
97 }
98 node_logger_1.logger.info("=> Loading Webpack configuration from `" + path_1.relative(CWD, scriptsPath) + "`");
99 // Remove existing rules related to JavaScript and TypeScript.
100 node_logger_1.logger.info("=> Removing existing JavaScript and TypeScript rules.");
101 var filteredRules = webpackConfig.module &&
102 webpackConfig.module.rules.filter(function (_a) {
103 var test = _a.test;
104 return !(test instanceof RegExp &&
105 ((test && test.test('.js')) || test.test('.ts')));
106 });
107 // Require the CRA config and set the appropriate mode.
108 var craWebpackConfigPath = path_1.join(scriptsPath, 'config', 'webpack.config');
109 // eslint-disable-next-line global-require, import/no-dynamic-require, @typescript-eslint/no-var-requires
110 var craWebpackConfig = require(craWebpackConfigPath)(webpackConfig.mode);
111 // Select the relevent CRA rules and add the Storybook config directory.
112 node_logger_1.logger.info("=> Modifying Create React App rules.");
113 var craRules = processCraConfig_1.processCraConfig(craWebpackConfig, options);
114 // CRA uses the `ModuleScopePlugin` to limit suppot to the `src` directory.
115 // Here, we select the plugin and modify its configuration to include Storybook config directory.
116 var plugins = craWebpackConfig.resolve.plugins.map(function (plugin) {
117 if (plugin.appSrcs) {
118 // Mutate the plugin directly as opposed to recreating it.
119 // eslint-disable-next-line no-param-reassign
120 plugin.appSrcs = __spread(plugin.appSrcs, [path_1.resolve(options.configDir)]);
121 }
122 return plugin;
123 });
124 // NOTE: These are set by default in Storybook 6.
125 var _a = options.typescriptOptions, typescriptOptions = _a === void 0 ? {
126 reactDocgen: 'react-docgen-typescript',
127 reactDocgenTypescriptOptions: {},
128 } : _a;
129 var tsDocgenPlugin = typescriptOptions.reactDocgen === 'react-docgen-typescript'
130 ? [
131 new react_docgen_typescript_plugin_1.default(typescriptOptions.reactDocgenTypescriptOptions),
132 ]
133 : [];
134 // Return the new config.
135 return __assign(__assign({}, webpackConfig), { module: __assign(__assign({}, webpackConfig.module), { rules: __spread((filteredRules || []), craRules) }), plugins: mergePlugins_1.mergePlugins.apply(void 0, __spread((webpackConfig.plugins || []), craWebpackConfig.plugins, tsDocgenPlugin)), resolve: __assign(__assign({}, webpackConfig.resolve), { extensions: craWebpackConfig.resolve.extensions, modules: __spread(((webpackConfig.resolve && webpackConfig.resolve.modules) || []), [
136 path_1.join(REACT_SCRIPTS_PATH, 'node_modules')
137 ], getModulePath_1.getModulePath(CWD)), plugins: __spread(plugins, [pnp_webpack_plugin_1.default]) }), resolveLoader: resolveLoader });
138};
139//# sourceMappingURL=index.js.map
\No newline at end of file