1
1
// webpack.config.js
2
- const CleanWebpackPlugin = require ( 'clean-webpack-plugin' ) ;
3
- const HardSourceWebpackPlugin = require ( 'hard-source-webpack-plugin-patch' ) ;
2
+ const { CleanWebpackPlugin } = require ( 'clean-webpack-plugin' ) ;
4
3
const TerserPlugin = require ( 'terser-webpack-plugin' ) ;
5
- const autoprefixer = require ( 'autoprefixer' ) ;
6
4
const HtmlWebpackPlugin = require ( 'html-webpack-plugin' ) ;
7
5
const MiniCssExtractPlugin = require ( 'mini-css-extract-plugin' ) ;
8
- const webpack = require ( 'webpack' ) ;
9
6
const CopyPlugin = require ( 'copy-webpack-plugin' ) ;
10
7
const path = require ( 'path' ) ;
11
8
const argv = require ( 'yargs' ) . argv ;
12
- const merge = require ( 'webpack-merge' ) ;
9
+ const { merge } = require ( 'webpack-merge' ) ;
13
10
const WebpackBar = require ( 'webpackbar' ) ;
11
+ const fs = require ( 'node:fs' ) ;
14
12
15
13
const cosmiconfigSync = require ( 'cosmiconfig' ) . cosmiconfigSync ;
16
14
const explorerSync = cosmiconfigSync ( 'patternlab' ) ;
@@ -23,10 +21,27 @@ const defaultConfig = {
23
21
sourceMaps : true ,
24
22
watch : argv . watch ? true : false ,
25
23
publicPath : './styleguide/' ,
26
- copy : [ { from : './src/images/**' , to : 'images' , flatten : true } ] ,
24
+ copy : {
25
+ patterns : [
26
+ { from : '../uikit-workshop/src/images/**' , to : 'images/[name][ext]' } ,
27
+ ] ,
28
+ } ,
27
29
noViewAll : false ,
28
30
} ;
29
31
32
+ // Requiring partials
33
+ // adapted from https://github.com/webpack-contrib/html-loader/issues/291#issuecomment-721909576
34
+ const INCLUDE_PATTERN = / \< i n c l u d e s r c = \" ( .+ ) \" \/ ? \> (?: \< \/ i n c l u d e \> ) ? / gi;
35
+ const processNestedHtml = ( content , loaderContext ) =>
36
+ ! INCLUDE_PATTERN . test ( content )
37
+ ? content
38
+ : content . replace ( INCLUDE_PATTERN , ( m , src ) =>
39
+ processNestedHtml (
40
+ fs . readFileSync ( path . resolve ( loaderContext . context , src ) , 'utf8' ) ,
41
+ loaderContext
42
+ )
43
+ ) ;
44
+
30
45
module . exports = function ( apiConfig ) {
31
46
return new Promise ( async ( resolve ) => {
32
47
let customConfig = defaultConfig ;
@@ -109,7 +124,9 @@ module.exports = function (apiConfig) {
109
124
loader : 'postcss-loader' ,
110
125
options : {
111
126
sourceMap : config . sourceMaps ,
112
- plugins : ( ) => [ autoprefixer ( ) ] ,
127
+ postcssOptions : {
128
+ plugins : [ [ 'autoprefixer' , { } ] ] ,
129
+ } ,
113
130
} ,
114
131
} ,
115
132
{
@@ -147,7 +164,6 @@ module.exports = function (apiConfig) {
147
164
output : {
148
165
path : path . resolve ( config . rootDir , `${ config . buildDir } /styleguide` ) ,
149
166
publicPath : `${ config . publicPath } ` ,
150
- filename : '[name].js' ,
151
167
chunkFilename : `js/[name]-chunk-[chunkhash].js` ,
152
168
} ,
153
169
module : {
@@ -170,12 +186,11 @@ module.exports = function (apiConfig) {
170
186
{
171
187
loader : 'html-loader' ,
172
188
options : {
173
- interpolate : true ,
174
- minimize : config . prod ? true : false ,
175
- minifyCSS : false ,
176
- minifyJS : config . prod ? true : false ,
177
- // super important -- this prevents the embedded iframe srcdoc HTML from breaking!
178
- preventAttributesEscaping : true ,
189
+ minimize : {
190
+ minifyCSS : false ,
191
+ minifyJS : config . prod ? true : false ,
192
+ } ,
193
+ preprocessor : processNestedHtml ,
179
194
} ,
180
195
} ,
181
196
] ,
@@ -225,8 +240,8 @@ module.exports = function (apiConfig) {
225
240
mode : config . prod ? 'production' : 'development' ,
226
241
optimization : {
227
242
minimize : config . prod ,
228
- occurrenceOrder : true ,
229
- namedChunks : true ,
243
+ chunkIds : 'total-size' ,
244
+ moduleIds : 'size' ,
230
245
removeAvailableModules : true ,
231
246
removeEmptyChunks : true ,
232
247
nodeEnv : 'production' ,
@@ -235,9 +250,9 @@ module.exports = function (apiConfig) {
235
250
splitChunks : {
236
251
chunks : 'async' ,
237
252
cacheGroups : {
238
- vendors : {
253
+ defaultVendors : {
239
254
test : / [ \\ / ] n o d e _ m o d u l e s [ \\ / ] / ,
240
- name : 'vendors' ,
255
+ idHint : 'vendors' ,
241
256
chunks : 'async' ,
242
257
reuseExistingChunk : true ,
243
258
} ,
@@ -247,7 +262,6 @@ module.exports = function (apiConfig) {
247
262
? [
248
263
new TerserPlugin ( {
249
264
test : / \. m ? j s ( \? .* ) ? $ / i,
250
- sourceMap : config . prod ? false : config . sourceMaps ,
251
265
terserOptions : {
252
266
safari10 : true ,
253
267
} ,
@@ -258,23 +272,6 @@ module.exports = function (apiConfig) {
258
272
plugins : [ new WebpackBar ( ) , new CopyPlugin ( config . copy ) ] ,
259
273
} ;
260
274
261
- webpackConfig . plugins . push (
262
- new HardSourceWebpackPlugin ( {
263
- info : {
264
- level : 'warn' ,
265
- } ,
266
- // Clean up large, old caches automatically.
267
- cachePrune : {
268
- // Caches younger than `maxAge` are not considered for deletion. They must
269
- // be at least this (default: 2 days) old in milliseconds.
270
- maxAge : 2 * 24 * 60 * 60 * 1000 ,
271
- // All caches together must be larger than `sizeThreshold` before any
272
- // caches will be deleted. Together they must be at least 300MB in size
273
- sizeThreshold : 300 * 1024 * 1024 ,
274
- } ,
275
- } )
276
- ) ;
277
-
278
275
const legacyConfig = merge ( webpackConfig , {
279
276
entry : {
280
277
'js/patternlab-pattern' : path . join (
@@ -303,7 +300,6 @@ module.exports = function (apiConfig) {
303
300
new MiniCssExtractPlugin ( {
304
301
filename : `[name].css` ,
305
302
chunkFilename : `[id].css` ,
306
- allChunks : true ,
307
303
} ) ,
308
304
] ,
309
305
} ) ;
@@ -343,22 +339,19 @@ module.exports = function (apiConfig) {
343
339
} ,
344
340
plugins : [
345
341
// clear out the buildDir on every fresh Webpack build
346
- new CleanWebpackPlugin (
347
- config . watch
342
+ new CleanWebpackPlugin ( {
343
+ verbose : false ,
344
+ cleanOnceBeforeBuildPatterns : config . watch
348
345
? [ ]
349
346
: [
350
347
`${ config . buildDir } /index.html` ,
351
348
`${ config . buildDir } /styleguide/css` ,
352
349
`${ config . buildDir } /styleguide/js` ,
353
350
] ,
354
- {
355
- allowExternal : true ,
356
- verbose : false ,
357
351
358
- // perform clean just before files are emitted to the output dir
359
- beforeEmit : false ,
360
- }
361
- ) ,
352
+ // perform clean just before files are emitted to the output dir
353
+ beforeEmit : false ,
354
+ } ) ,
362
355
new HtmlWebpackPlugin ( {
363
356
filename : '../index.html' ,
364
357
template : path . resolve ( __dirname , 'src/html/index.html' ) ,
@@ -367,7 +360,6 @@ module.exports = function (apiConfig) {
367
360
new MiniCssExtractPlugin ( {
368
361
filename : `[name].css` ,
369
362
chunkFilename : `[id].css` ,
370
- allChunks : true ,
371
363
} ) ,
372
364
] ,
373
365
} ) ;
0 commit comments