This repository has been archived by the owner on Oct 28, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
182 lines (149 loc) · 4.89 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const {
createWebpackDevConfig,
createWebpackProdConfig,
} = require("@craco/craco");
const { logger } = require("@storybook/node-logger");
const { resolve, relative, dirname, join } = require("path");
const {
mergePlugins,
} = require("@storybook/preset-create-react-app/dist/helpers/mergePlugins");
const {
processCraConfig,
} = require("@storybook/preset-create-react-app/dist/helpers/processCraConfig");
const {
getModulePath,
} = require("@storybook/preset-create-react-app/dist/helpers/getModulePath");
const CWD = process.cwd();
const babelDefault = () => ({
presets: [],
plugins: [],
});
const incompatiblePresets = [
"@storybook/preset-scss",
"@storybook/preset-typescript",
"@storybook/preset-create-react-app",
];
const checkPresets = (options) => {
let presetsList = options.presetsList || [];
presetsList.forEach((preset) => {
const presetName = typeof preset === "string" ? preset : preset.name;
if (incompatiblePresets.includes(presetName)) {
logger.warn(
`\`${presetName}\` may not be compatible with \`storybook-preset-craco\``
);
}
});
};
const webpack = (webpackConfig = {}, options) => {
const createWebpackConfig =
webpackConfig.mode === "production"
? createWebpackProdConfig
: createWebpackDevConfig;
checkPresets(options);
const cracoConfigFile =
options.cracoConfigFile || resolve(CWD, "craco.config.js");
const cracoConfig = require(cracoConfigFile);
logger.info(
`=> Loading Craco configuration from \`${relative(CWD, cracoConfigFile)}\``
);
const scriptsPackageName = cracoConfig.reactScriptsVersion || "react-scripts";
const scriptsPath = dirname(
require.resolve(`${scriptsPackageName}/package.json`)
);
logger.info(`=> Using react-scripts from \`${relative(CWD, scriptsPath)}\``);
const cracoWebpackConfig = createWebpackConfig(cracoConfig);
const resolveLoader = {
modules: ["node_modules", join(scriptsPath, "node_modules")],
};
// Remove existing rules related to JavaScript and TypeScript.
logger.info(`=> Removing existing JavaScript and TypeScript rules.`);
const filteredRules =
webpackConfig.module &&
webpackConfig.module.rules.filter(
({ test }) =>
!(
test instanceof RegExp &&
((test && test.test(".js")) || test.test(".ts"))
)
);
// Select the relevant craco rules and add the Storybook config directory.
logger.info(`=> Modifying craco rules.`);
const craRules = processCraConfig(cracoWebpackConfig, options);
// CRA uses the `ModuleScopePlugin` to limit support to the `src` directory.
// Here, we select the plugin and modify its configuration to include Storybook config directory.
const plugins = cracoWebpackConfig.resolve.plugins.map((plugin) => {
if (plugin.appSrcs) {
// Mutate the plugin directly as opposed to recreating it.
// eslint-disable-next-line no-param-reassign
plugin.appSrcs = [...plugin.appSrcs, resolve(options.configDir)];
}
return plugin;
});
// Return the new config.
return {
...webpackConfig,
module: {
...webpackConfig.module,
rules: [...(filteredRules || []), ...craRules],
},
plugins: mergePlugins(
...(webpackConfig.plugins || []),
...cracoWebpackConfig.plugins
),
resolve: {
...webpackConfig.resolve,
alias: {...webpackConfig.resolve.alias, ...cracoWebpackConfig.resolve.alias},
extensions: cracoWebpackConfig.resolve.extensions,
modules: [
...((webpackConfig.resolve && webpackConfig.resolve.modules) || []),
...((cracoWebpackConfig.resolve && cracoWebpackConfig.resolve.modules) || []),
join(scriptsPath, "node_modules"),
...getModulePath(CWD),
],
plugins: plugins,
},
resolveLoader,
};
};
const webpackFinal = (webpackConfig = {}, options) => {
logger.info(`=> Removing storybook default rules.`);
// these are suppressed by storybook when @storybook/preset-create-react-app is present.
const rules = webpackConfig.module.rules.filter(
(rule) =>
!(
rule.test instanceof RegExp &&
(rule.test.test(".css") ||
rule.test.test(".svg") ||
rule.test.test(".mp4"))
)
);
return {
...webpackConfig,
module: {
...webpackConfig.module,
rules: rules,
},
};
};
const managerWebpack = (webpackConfig = {}, options) => {
const cracoConfigFile =
options.cracoConfigFile || resolve(CWD, "craco.config.js");
const cracoConfig = require(cracoConfigFile);
const scriptsPackageName = cracoConfig.reactScriptsVersion || "react-scripts";
const scriptsPath = dirname(
require.resolve(`${scriptsPackageName}/package.json`)
);
const resolveLoader = {
modules: ["node_modules", join(scriptsPath, "node_modules")],
};
return {
...webpackConfig,
resolveLoader,
};
};
module.exports = {
babelDefault,
webpack,
webpackFinal,
managerWebpack,
};