/
manager-webpack.config.ts
189 lines (182 loc) · 5.56 KB
/
manager-webpack.config.ts
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
183
184
185
186
187
188
189
import path from 'path';
import fse from 'fs-extra';
import { DefinePlugin, Configuration, WebpackPluginInstance } from 'webpack';
import Dotenv from 'dotenv-webpack';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import CaseSensitivePathsPlugin from 'case-sensitive-paths-webpack-plugin';
import VirtualModulePlugin from 'webpack-virtual-modules';
import TerserWebpackPlugin from 'terser-webpack-plugin';
import themingPaths from '@storybook/theming/paths';
import uiPaths from '@storybook/ui/paths';
import readPackage from 'read-pkg-up';
import {
resolvePathInStorybookCache,
stringifyEnvs,
es6Transpiler,
getManagerHeadTemplate,
getManagerMainTemplate,
Options,
ManagerWebpackOptions,
hasDotenv,
} from '@storybook/core-common';
import { babelLoader } from './babel-loader-manager';
export default async ({
configDir,
configType,
docsMode,
entries,
refs,
outputDir,
previewUrl,
versionCheck,
releaseNotesData,
presets,
modern,
}: Options & ManagerWebpackOptions): Promise<Configuration> => {
const envs = await presets.apply<Record<string, string>>('env');
const logLevel = await presets.apply('logLevel', undefined);
const template = await presets.apply('managerMainTemplate', getManagerMainTemplate());
const headHtmlSnippet = await presets.apply(
'managerHead',
getManagerHeadTemplate(configDir, process.env)
);
const isProd = configType === 'PRODUCTION';
const refsTemplate = fse.readFileSync(path.join(__dirname, 'virtualModuleRef.template.js'), {
encoding: 'utf8',
});
const {
packageJson: { version },
} = await readPackage({ cwd: __dirname });
// @ts-ignore
// const { BundleAnalyzerPlugin } = await import('webpack-bundle-analyzer').catch(() => ({}));
return {
name: 'manager',
mode: isProd ? 'production' : 'development',
bail: isProd,
devtool: false,
entry: entries,
output: {
path: outputDir,
filename: isProd ? '[name].[contenthash].manager.bundle.js' : '[name].manager.bundle.js',
publicPath: '',
},
watchOptions: {
aggregateTimeout: 2000,
ignored: /node_modules/,
},
plugins: [
refs
? ((new VirtualModulePlugin({
[path.resolve(path.join(configDir, `generated-refs.js`))]: refsTemplate.replace(
`'{{refs}}'`,
JSON.stringify(refs)
),
}) as any) as WebpackPluginInstance)
: null,
(new HtmlWebpackPlugin({
filename: `index.html`,
// FIXME: `none` isn't a known option
chunksSortMode: 'none' as any,
alwaysWriteToDisk: true,
inject: false,
templateParameters: (compilation, files, options) => ({
compilation,
files,
options,
version,
globals: {
CONFIG_TYPE: configType,
LOGLEVEL: logLevel,
VERSIONCHECK: JSON.stringify(versionCheck),
RELEASE_NOTES_DATA: JSON.stringify(releaseNotesData),
DOCS_MODE: docsMode, // global docs mode
PREVIEW_URL: previewUrl, // global preview URL
},
headHtmlSnippet,
}),
template,
}) as any) as WebpackPluginInstance,
(new CaseSensitivePathsPlugin() as any) as WebpackPluginInstance,
hasDotenv() ? new Dotenv({ silent: true }) : null,
// graphql sources check process variable
new DefinePlugin({
'process.env': stringifyEnvs(envs),
NODE_ENV: JSON.stringify(envs.NODE_ENV),
}) as WebpackPluginInstance,
// isProd &&
// BundleAnalyzerPlugin &&
// new BundleAnalyzerPlugin({ analyzerMode: 'static', openAnalyzer: false }),
].filter(Boolean),
module: {
rules: [
babelLoader(),
es6Transpiler() as any,
{
test: /\.css$/,
use: [
require.resolve('style-loader'),
{
loader: require.resolve('css-loader'),
options: {
importLoaders: 1,
},
},
],
},
{
test: /\.(svg|ico|jpg|jpeg|png|apng|gif|eot|otf|webp|ttf|woff|woff2|cur|ani|pdf)(\?.*)?$/,
loader: require.resolve('file-loader'),
options: {
name: isProd
? 'static/media/[name].[contenthash:8].[ext]'
: 'static/media/[path][name].[ext]',
},
},
{
test: /\.(mp4|webm|wav|mp3|m4a|aac|oga)(\?.*)?$/,
loader: require.resolve('url-loader'),
options: {
limit: 10000,
name: isProd
? 'static/media/[name].[contenthash:8].[ext]'
: 'static/media/[path][name].[ext]',
},
},
],
},
resolve: {
extensions: ['.mjs', '.js', '.jsx', '.json', '.cjs', '.ts', '.tsx'],
modules: ['node_modules'].concat(envs.NODE_PATH || []),
mainFields: [modern ? 'sbmodern' : null, 'browser', 'module', 'main'].filter(Boolean),
alias: {
...themingPaths,
...uiPaths,
},
},
recordsPath: resolvePathInStorybookCache('public/records.json'),
performance: {
hints: false,
},
optimization: {
splitChunks: {
chunks: 'all',
},
runtimeChunk: true,
sideEffects: true,
usedExports: true,
concatenateModules: true,
minimizer: isProd
? [
new TerserWebpackPlugin({
parallel: true,
terserOptions: {
mangle: false,
sourceMap: true,
keep_fnames: true,
},
}),
]
: [],
},
};
};