1 | "use strict";
|
2 | var __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 | };
|
13 | var __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 | };
|
29 | var __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 | };
|
33 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
34 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
35 | };
|
36 | Object.defineProperty(exports, "__esModule", { value: true });
|
37 | exports.webpack = exports.managerWebpack = exports.babelDefault = void 0;
|
38 | var path_1 = require("path");
|
39 | var node_logger_1 = require("@storybook/node-logger");
|
40 | var pnp_webpack_plugin_1 = __importDefault(require("pnp-webpack-plugin"));
|
41 | var react_docgen_typescript_plugin_1 = __importDefault(require("react-docgen-typescript-plugin"));
|
42 | var mergePlugins_1 = require("./helpers/mergePlugins");
|
43 | var getReactScriptsPath_1 = require("./helpers/getReactScriptsPath");
|
44 | var processCraConfig_1 = require("./helpers/processCraConfig");
|
45 | var checkPresets_1 = require("./helpers/checkPresets");
|
46 | var getModulePath_1 = require("./helpers/getModulePath");
|
47 | var CWD = process.cwd();
|
48 |
|
49 |
|
50 | var IS_USING_YARN_PNP = typeof process.versions.pnp !== 'undefined';
|
51 | var REACT_SCRIPTS_PATH = IS_USING_YARN_PNP
|
52 | ? getReactScriptsPath_1.getReactScriptsPathWithYarnPnp()
|
53 | : getReactScriptsPath_1.getReactScriptsPath();
|
54 | var OPTION_SCRIPTS_PACKAGE = 'scriptsPackageName';
|
55 |
|
56 |
|
57 | if (!process.env.PUBLIC_URL) {
|
58 | process.env.PUBLIC_URL = '.';
|
59 | }
|
60 |
|
61 | var resolveLoader = {
|
62 | modules: ['node_modules', path_1.join(REACT_SCRIPTS_PATH, 'node_modules')],
|
63 | plugins: [pnp_webpack_plugin_1.default.moduleLoader(module)],
|
64 | };
|
65 |
|
66 | exports.babelDefault = function () { return ({
|
67 | presets: [],
|
68 | plugins: [],
|
69 | }); };
|
70 |
|
71 | exports.managerWebpack = function (webpackConfig) {
|
72 | if (webpackConfig === void 0) { webpackConfig = {}; }
|
73 | return (__assign(__assign({}, webpackConfig), { resolveLoader: resolveLoader }));
|
74 | };
|
75 |
|
76 | exports.webpack = function (webpackConfig, options) {
|
77 | if (webpackConfig === void 0) { webpackConfig = {}; }
|
78 | var scriptsPath = REACT_SCRIPTS_PATH;
|
79 |
|
80 | checkPresets_1.checkPresets(options);
|
81 |
|
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 |
|
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 |
|
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 |
|
108 | var craWebpackConfigPath = path_1.join(scriptsPath, 'config', 'webpack.config');
|
109 |
|
110 | var craWebpackConfig = require(craWebpackConfigPath)(webpackConfig.mode);
|
111 |
|
112 | node_logger_1.logger.info("=> Modifying Create React App rules.");
|
113 | var craRules = processCraConfig_1.processCraConfig(craWebpackConfig, options);
|
114 |
|
115 |
|
116 | var plugins = craWebpackConfig.resolve.plugins.map(function (plugin) {
|
117 | if (plugin.appSrcs) {
|
118 |
|
119 |
|
120 | plugin.appSrcs = __spread(plugin.appSrcs, [path_1.resolve(options.configDir)]);
|
121 | }
|
122 | return plugin;
|
123 | });
|
124 |
|
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 |
|
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 |
|
\ | No newline at end of file |