-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
109 lines (96 loc) · 2.89 KB
/
webpack.config.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
const path = require('path');
const getCoreConfig = require('@storybook/core/dist/server/config');
const reactOptions = require('@storybook/react/dist/server/options');
const { patchWebpackRules } = require('./utils');
const CONFIG_NAME_CLIENT = 'client';
const CONFIG_NAME_SERVER = 'server';
const COMPILE_INDEX_SERVER = 0;
const COMPILE_INDEX_CLIENT = 1;
const cache = {};
const ROOT_PKG_PATH = path.join(__dirname, '../../');
const getPlugins = (plugins) => ([
...plugins.filter((plugin) => (
!['HtmlWebpackPlugin', 'VirtualModulePlugin'].includes(plugin.constructor.name)
)),
]);
const getRulesWithoutStyleLoaders = (rules) => patchWebpackRules(
rules,
['test.css', 'test.module.scss', 'test.scss'],
'style-loader',
(rule) => {
const [, cssLoader, ...otherLoaders] = rule.use;
const newUse = [
{
...cssLoader,
options: {
...cssLoader.options,
onlyLocals: true,
},
},
...otherLoaders,
];
return { ...rule, use: newUse };
});
const getRulesForLocalSrc = (rules) => patchWebpackRules(
rules,
['test.jsx'],
'babel-loader',
(rule) => ({
...rule,
include: [
...rule.include,
path.join(__dirname, 'src/'),
],
}));
const getConfig = async({ webpackHotMiddlewarePath }) => {
const baseConfig = await getCoreConfig.default({
configType: 'DEVELOPMENT',
outputDir: path.dirname(require.resolve('@storybook/core/dist/server/public/favicon.ico')),
cache,
corePresets: [require.resolve('@storybook/core/dist/server/preview/preview-preset.js')],
overridePresets: [require.resolve('@storybook/core/dist/server/preview/custom-webpack-preset.js')],
configDir: path.join(ROOT_PKG_PATH, '.storybook'),
...reactOptions.default,
});
return [
{
...baseConfig,
name: CONFIG_NAME_SERVER,
mode: 'development',
entry: path.join(__dirname, 'src/prerender.jsx'),
output: {
...baseConfig.output,
libraryTarget: 'commonjs',
},
target: 'node',
module: {
...baseConfig.module,
rules: getRulesForLocalSrc(getRulesWithoutStyleLoaders(baseConfig.module.rules)),
},
plugins: getPlugins([]),
optimization: {},
},
{
...baseConfig,
name: CONFIG_NAME_CLIENT,
mode: 'development',
devtool: 'inline-source-map',
entry: [
...baseConfig.entry.filter((entry) => /@storybook/.test(entry)), // TODO: whitelist vs blacklist?
`webpack-hot-middleware/client?path=${webpackHotMiddlewarePath}&name=${CONFIG_NAME_CLIENT}&noInfo=true`,
path.join(__dirname, 'src/index.jsx'),
],
module: {
...baseConfig.module,
rules: getRulesForLocalSrc(baseConfig.module.rules),
},
plugins: getPlugins(baseConfig.plugins),
optimization: {},
},
];
};
module.exports = {
getConfig,
COMPILE_INDEX_SERVER,
COMPILE_INDEX_CLIENT,
};