-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
base-webpack.config.ts
97 lines (92 loc) · 2.96 KB
/
base-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
import { logger } from '@storybook/node-logger';
import type { Options, CoreConfig, Webpack5BuilderConfig } from '@storybook/core-common';
import type { Configuration } from 'webpack';
export async function createDefaultWebpackConfig(
storybookBaseConfig: Configuration,
options: Options
): Promise<Configuration> {
if (
options.presetsList.some((preset) =>
/@storybook(\/|\\)preset-create-react-app/.test(
typeof preset === 'string' ? preset : preset.name
)
)
) {
return storybookBaseConfig;
}
const hasPostcssAddon = options.presetsList.some((preset) =>
/@storybook(\/|\\)addon-postcss/.test(typeof preset === 'string' ? preset : preset.name)
);
let cssLoaders = {};
if (!hasPostcssAddon) {
logger.info(`=> Using implicit CSS loaders`);
cssLoaders = {
test: /\.css$/,
sideEffects: true,
use: [
// TODO: Decide if we want to keep style-loader & css-loader in core
// Trying to apply style-loader or css-loader to files that already have been
// processed by them causes webpack to crash, so no one else can add similar
// loader configurations to the `.css` extension.
require.resolve('style-loader'),
{
loader: require.resolve('css-loader'),
options: {
importLoaders: 1,
},
},
],
};
}
const isProd = storybookBaseConfig.mode !== 'development';
const coreOptions = await options.presets.apply<CoreConfig>('core');
const builderOptions = (coreOptions.builder as Webpack5BuilderConfig).options;
const cacheConfig = builderOptions?.fsCache
? { cache: { type: 'filesystem' as 'filesystem' } }
: {};
const lazyCompilationConfig =
builderOptions?.lazyCompilation && !isProd ? { lazyCompilation: { entries: false } } : {};
return {
...storybookBaseConfig,
module: {
...storybookBaseConfig.module,
rules: [
...storybookBaseConfig.module.rules,
cssLoaders,
{
test: /\.(svg|ico|jpg|jpeg|png|apng|gif|eot|otf|webp|ttf|woff|woff2|cur|ani|pdf)(\?.*)?$/,
type: 'asset/resource',
generator: {
filename: isProd
? 'static/media/[name].[contenthash:8][ext]'
: 'static/media/[path][name][ext]',
},
},
{
test: /\.(mp4|webm|wav|mp3|m4a|aac|oga)(\?.*)?$/,
type: 'asset',
parser: {
dataUrlCondition: {
maxSize: 10000,
},
},
generator: {
filename: isProd
? 'static/media/[name].[contenthash:8][ext]'
: 'static/media/[path][name][ext]',
},
},
],
},
resolve: {
...storybookBaseConfig.resolve,
fallback: {
...storybookBaseConfig.resolve?.fallback,
crypto: false,
assert: false,
},
},
...cacheConfig,
experiments: { ...storybookBaseConfig.experiments, ...lazyCompilationConfig },
};
}