/
base-webpack.config.ts
140 lines (126 loc) · 4.24 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
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
import autoprefixer from 'autoprefixer';
import findUp from 'find-up';
import path from 'path';
import { logger } from '@storybook/node-logger';
import deprecate from 'util-deprecate';
import dedent from 'ts-dedent';
import type { BuilderOptions, LoadedPreset, Options } from '@storybook/core-common';
const warnImplicitPostcssPlugins = deprecate(
() => ({
// Additional config is merged with config, so we have it disabled currently
config: false,
plugins: [
require('postcss-flexbugs-fixes'), // eslint-disable-line global-require
autoprefixer({
flexbox: 'no-2009',
}),
],
}),
dedent`
Default PostCSS plugins are deprecated. When switching to '@storybook/addon-postcss',
you will need to add your own plugins, such as 'postcss-flexbugs-fixes' and 'autoprefixer'.
See https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#deprecated-default-postcss-plugins for details.
`
);
const warnGetPostcssOptions = deprecate(
() => {},
dedent`
Relying on the implicit PostCSS loader is deprecated and will be removed in Storybook 7.0.
If you need PostCSS, include '@storybook/addon-postcss' in your '.storybook/main.js' file.
See https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#deprecated-implicit-postcss-loader for details.
`
);
const getPostcssOptions = async () => {
const postcssConfigFiles = [
'.postcssrc',
'.postcssrc.json',
'.postcssrc.yml',
'.postcssrc.js',
'postcss.config.js',
];
// This is done naturally by newer postcss-loader (through cosmiconfig)
const customPostcssConfig = await findUp(postcssConfigFiles);
if (customPostcssConfig) {
logger.info(`=> Using custom ${path.basename(customPostcssConfig)}`);
warnGetPostcssOptions();
return {
config: customPostcssConfig,
};
}
return warnImplicitPostcssPlugins();
};
const presetName = (preset: LoadedPreset | string): string =>
typeof preset === 'string' ? preset : preset.name;
export async function createDefaultWebpackConfig(storybookBaseConfig: any, options: Options) {
if (
options.presetsList.some((preset) =>
/@storybook(\/|\\)preset-create-react-app/.test(presetName(preset))
)
) {
return storybookBaseConfig;
}
const hasPostcssAddon = options.presetsList.some((preset) =>
/@storybook(\/|\\)addon-postcss/.test(presetName(preset))
);
const features = await options.presets.apply<{ postcss?: boolean }>('features');
let cssLoaders = {};
if (!hasPostcssAddon) {
logger.info(`=> Using implicit CSS loaders`);
const use = [
// TODO(blaine): 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,
},
},
features?.postcss !== false
? {
loader: require.resolve('postcss-loader'),
options: {
postcssOptions: await getPostcssOptions(),
},
}
: null,
];
cssLoaders = {
test: /\.css$/,
sideEffects: true,
use: use.filter(Boolean),
};
}
const isProd = storybookBaseConfig.mode !== 'development';
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)(\?.*)?$/,
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]',
},
},
],
},
};
}