-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.js
More file actions
147 lines (138 loc) · 4.44 KB
/
Copy pathwebpack.config.js
File metadata and controls
147 lines (138 loc) · 4.44 KB
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
/**
* This single config file handles both the production and development/storybook builds.
* If the environment is production, a fully minified/production build,
* using custom plugins and module rules, is generated and output to `/dist`.
* If the environment is development/storybook, a custom set of module rules are created,
* and a subset of the resulting config (plugins, module.rules, and resolve.extensions) are consumed by
* `.storybook/main.js` to supplement Storybook's existing config.
*/
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
// TODO: find a replacement for this plugin.
// const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin")
const FixStyleOnlyEntriesPlugin = require('webpack-fix-style-only-entries');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const { generateScopedName } = require('./scripts/cssModulesNaming.cjs');
const rules = [];
// Process all SCSS modules which will be compiled inside the main JS bundle.
const injectCssModulesInJS = {
test: /\.scss$/,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
modules: {
getLocalIdent: (context, _localIdentName, localName) =>
generateScopedName(localName, context.resourcePath),
},
sourceMap: true,
},
},
'sass-loader',
'postcss-loader',
],
include: /\.module\.scss$/,
};
// Process all SCSS modules which will be compiled to an index.css
const extractCssModulesToCss = {
test: /\.scss$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
sourceMap: true,
modules: {
getLocalIdent: (context, _localIdentName, localName) =>
generateScopedName(localName, context.resourcePath),
},
},
},
'sass-loader',
'postcss-loader',
],
include: /\.module\.scss$/,
};
const processGlobalCss = {
test: /\.scss$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'sass-loader',
'postcss-loader',
],
exclude: /\.module\.scss$/,
};
const processFonts = {
test: /\.(woff(2)?|otf|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
type: 'asset/inline'
};
// When previewing or building storybook, global CSS is imported
// via the storybook preview.js, so only modules need to be processed.
// NOTE: this rule is merged with the rest of the storybook webpack config in
// .storybook/main.js
if (process.env.IS_STORYBOOK) {
rules.push(
{ ...injectCssModulesInJS },
);
}
// Rules for package publishing.
// All JS is built with dts/rollup but
// global CSS files are generated via webpack.
if (process.env.IS_PUBLISHING) {
rules.push(
...[
{ ...extractCssModulesToCss },
{ ...processGlobalCss },
{ ...processFonts },
],
);
}
module.exports = {
mode: process.env.NODE_ENV || 'development', // Should be set in the yarn script since there is no true ENV.
// Files to be bundled
entry: {
index: [path.join(__dirname, 'src/styles/component-modules.js')], // Component CSS modules extracted to index.css.
utilities: [path.join(__dirname, 'src/styles/utilities.scss')], // Utilities CSS only.
fonts: [path.join(__dirname, 'src/styles/fonts.scss')], // Fonts CSS only.
variables: [path.join(__dirname, 'src/styles/variables/index.scss')], // Variables CSS only.
reset: [path.join(__dirname, 'src/styles/reset.scss')], // CSS Reset only.
},
optimization: {
usedExports: true,
minimizer: [
// Minify CSS/SCSS
new CssMinimizerPlugin(),
],
},
// Final files based on entry files.
output: {
filename: '[name].js',
path: path.join(__dirname, 'dist'),
libraryTarget: 'umd',
globalObject: 'this',
},
plugins: [
// Extract css to its own .css file as opposed to a JS module.
new MiniCssExtractPlugin({ filename: 'css/[name].css' }),
// Clear out the css directory on every build.
new CleanWebpackPlugin({
cleanOnceBeforeBuildPatterns: [
'css/**',
],
}),
// This removes empty .js files generated for css/scss-only entries. Issue inherent to webpack, more details here:
// https://github.com/webpack-contrib/mini-css-extract-plugin/issues/151
new FixStyleOnlyEntriesPlugin(),
],
module: {
rules,
},
resolve: {
alias: {
'@emotion/react': path.resolve(__dirname, 'node_modules/@emotion/react'),
},
},
};