1
1
/**
2
2
* External dependencies
3
3
*/
4
- const { sync : readPkgUp } = require ( "read-pkg-up" ) ;
5
- const LiveReloadPlugin = require ( "webpack-livereload-plugin" ) ;
6
- const MiniCssExtractPlugin = require ( "mini-css-extract-plugin" ) ;
7
- const path = require ( "path" ) ;
8
- const { existsSync, lstatSync, readdirSync, realpathSync } = require ( "fs" ) ;
4
+ const { existsSync, lstatSync, readdirSync, realpathSync } = require ( 'fs' ) ;
5
+ const { sync : readPkgUp } = require ( 'read-pkg-up' ) ;
6
+ const globImporter = require ( 'node-sass-glob-importer' ) ;
7
+ const LiveReloadPlugin = require ( 'webpack-livereload-plugin' ) ;
8
+ const MiniCssExtractPlugin = require ( 'mini-css-extract-plugin' ) ;
9
+ const path = require ( 'path' ) ;
9
10
10
11
/**
11
12
* WordPress dependencies
12
13
*/
13
- const DependencyExtractionWebpackPlugin = require ( " @wordpress/dependency-extraction-webpack-plugin" ) ;
14
+ const DependencyExtractionWebpackPlugin = require ( ' @wordpress/dependency-extraction-webpack-plugin' ) ;
14
15
15
16
/**
16
17
* Internal dependencies
17
18
*/
18
- const RemoveSuprefluousAssetsPlugin = require ( " ../plugins/remove-superfluous-assets" ) ;
19
+ const RemoveSuprefluousAssetsPlugin = require ( ' ../plugins/remove-superfluous-assets' ) ;
19
20
const {
20
21
getArg,
21
22
getScriptsConfig,
22
23
hasArg,
23
24
hasBabelConfig,
24
25
hasFileArg,
25
- hasPostCSSConfig
26
- } = require ( " ../utils" ) ;
26
+ hasPostCSSConfig,
27
+ } = require ( ' ../utils' ) ;
27
28
28
29
const { path : pkgPath } = readPkgUp ( {
29
- cwd : realpathSync ( process . cwd ( ) )
30
+ cwd : realpathSync ( process . cwd ( ) ) ,
30
31
} ) ;
31
32
32
33
const config = getScriptsConfig ( ) ;
33
34
34
35
const paths = Object . assign (
35
36
{
36
- src : "src/assets" ,
37
- output : "dist" ,
38
- scripts : "js" ,
39
- styles : "scss" ,
40
- images : "images"
37
+ src : 'src/assets' ,
38
+ output : 'dist' ,
39
+ scripts : 'js' ,
40
+ styles : 'scss' ,
41
+ images : 'images' ,
42
+ fonts : 'fonts' ,
41
43
} ,
42
44
config && config . paths ? config . paths : { }
43
45
) ;
44
46
45
47
for ( const pathKey in paths ) {
46
48
let pathValue = getArg ( `--${ pathKey } -path` , paths [ pathKey ] ) ;
47
49
48
- if ( [ " src" , " output" ] . includes ( pathKey ) && "/" !== pathValue . charAt ( 0 ) ) {
50
+ if ( [ ' src' , ' output' ] . includes ( pathKey ) && '/' !== pathValue . charAt ( 0 ) ) {
49
51
pathValue = path . join ( path . dirname ( pkgPath ) , pathValue ) ;
50
52
}
51
53
@@ -58,7 +60,7 @@ const entry = {};
58
60
if ( ! hasFileArg ( ) ) {
59
61
if ( ! existsSync ( paths . src ) ) {
60
62
/* eslint-disable-next-line no-console */
61
- console . log ( " Source directory does not exist." ) ;
63
+ console . log ( ' Source directory does not exist.' ) ;
62
64
process . exit ( 1 ) ;
63
65
}
64
66
@@ -68,9 +70,9 @@ if (!hasFileArg()) {
68
70
const ext = path . extname ( file ) ;
69
71
const name = path . basename ( file , ext ) ;
70
72
71
- if ( "_" !== name . charAt ( 0 ) ) {
73
+ if ( '_' !== name . charAt ( 0 ) ) {
72
74
const filePath = path . join ( srcPath , file ) ;
73
- outputPath = outputPath . replace ( " scss" , " css" ) ;
75
+ outputPath = outputPath . replace ( ' scss' , ' css' ) ;
74
76
75
77
if ( lstatSync ( filePath ) . isFile ( ) ) {
76
78
entry [ `${ outputPath } /${ name } ` ] = filePath ;
@@ -81,7 +83,7 @@ if (!hasFileArg()) {
81
83
const directoryErrors = [ ] ;
82
84
let hasDirectory = false ;
83
85
84
- for ( const assetType of [ " scripts" , " styles" ] ) {
86
+ for ( const assetType of [ ' scripts' , ' styles' ] ) {
85
87
if ( false === paths [ assetType ] ) {
86
88
continue ;
87
89
}
@@ -100,7 +102,7 @@ if (!hasFileArg()) {
100
102
101
103
hasDirectory = true ;
102
104
103
- readdirSync ( srcPath ) . forEach ( file =>
105
+ readdirSync ( srcPath ) . forEach ( ( file ) =>
104
106
addEntry ( file , srcPath , paths [ assetType ] )
105
107
) ;
106
108
}
@@ -118,114 +120,162 @@ if (!hasFileArg()) {
118
120
119
121
if ( ! entry ) {
120
122
/* eslint-disable-next-line no-console */
121
- console . log ( " No entry files available." ) ;
123
+ console . log ( ' No entry files available.' ) ;
122
124
process . exit ( 1 ) ;
123
125
}
124
126
}
125
127
126
128
const mode = getArg (
127
- " --mode" ,
128
- process . env . NODE_ENV === " production" ? " production" : " development"
129
+ ' --mode' ,
130
+ process . env . NODE_ENV === ' production' ? ' production' : ' development'
129
131
) ;
130
- const isProduction = "production" === mode ;
132
+ const isProduction = 'production' === mode ;
133
+
134
+ const imageWebpackLoaderConfig = {
135
+ loader : 'image-webpack-loader' ,
136
+ options : {
137
+ disable : false === config . imagemin ,
138
+ ...( 'object' === typeof config . imagemin && config . imagemin ) ,
139
+ } ,
140
+ } ;
131
141
132
142
module . exports = {
133
143
mode,
134
144
context,
135
145
entry,
136
146
output : {
137
147
path : paths . output ,
138
- filename : " [name].js" ,
139
- publicPath : " ../"
148
+ filename : ' [name].js' ,
149
+ publicPath : ' ../' ,
140
150
} ,
141
- devtool : isProduction ? false : " source-map" ,
151
+ devtool : isProduction ? false : ' source-map' ,
142
152
module : {
143
153
rules : [
144
154
{
145
155
test : / \. j s $ / ,
146
156
exclude : / n o d e _ m o d u l e s / ,
147
157
use : [
148
- require . resolve ( " thread-loader" ) ,
158
+ require . resolve ( ' thread-loader' ) ,
149
159
{
150
- loader : require . resolve ( " babel-loader" ) ,
160
+ loader : require . resolve ( ' babel-loader' ) ,
151
161
options : {
152
162
// Babel uses a directory within local node_modules
153
163
// by default. Use the environment variable option
154
164
// to enable more persistent caching.
155
- cacheDirectory : process . env . BABEL_CACHE_DIRECTORY || true ,
165
+ cacheDirectory :
166
+ process . env . BABEL_CACHE_DIRECTORY || true ,
156
167
...( ! hasBabelConfig ( ) && {
157
168
babelrc : false ,
158
169
configFile : false ,
159
- presets : [ require . resolve ( "@wordpress/babel-preset-default" ) ]
160
- } )
161
- }
162
- }
163
- ]
170
+ presets : [
171
+ require . resolve (
172
+ '@wordpress/babel-preset-default'
173
+ ) ,
174
+ ] ,
175
+ } ) ,
176
+ } ,
177
+ } ,
178
+ ] ,
164
179
} ,
165
180
{
166
181
test : / \. s c s s $ / ,
167
182
use : [
168
183
MiniCssExtractPlugin . loader ,
169
184
{
170
- loader : " css-loader" ,
185
+ loader : ' css-loader' ,
171
186
options : {
172
187
importLoaders : 2 ,
173
- sourceMap : true
174
- }
188
+ sourceMap : true ,
189
+ } ,
175
190
} ,
176
191
{
177
- loader : " postcss-loader" ,
192
+ loader : ' postcss-loader' ,
178
193
options : {
179
194
sourceMap : true ,
180
195
...( ! hasPostCSSConfig ( ) && {
181
- plugins : ( ) => [ require ( "autoprefixer" ) ]
182
- } )
183
- }
196
+ postcssOptions : {
197
+ plugins : ( ) => [ require ( 'autoprefixer' ) ] ,
198
+ } ,
199
+ } ) ,
200
+ } ,
184
201
} ,
185
202
{
186
- loader : " sass-loader" ,
203
+ loader : ' sass-loader' ,
187
204
options : {
188
- sourceMap : true
189
- }
190
- }
191
- ]
192
- } ,
193
- {
194
- test : / \. s v g $ / ,
195
- issuer : {
196
- test : / \. j s x ? $ /
197
- } ,
198
- use : [ "@svgr/webpack" ]
205
+ sassOptions : {
206
+ importer : [ globImporter ( ) ] ,
207
+ } ,
208
+ sourceMap : true ,
209
+ } ,
210
+ } ,
211
+ ] ,
199
212
} ,
200
213
{
201
- test : / \. ( p n g | s v g | j p g | g i f ) $ / ,
202
- use : [
214
+ test : / \. ( p n g | s v g | j p e ? g | g i f ) $ / ,
215
+ oneOf : [
203
216
{
204
- loader : "url-loader" ,
205
- options : {
206
- limit : config . urlLoader ? config . urlLoader : false ,
207
- name : "[name].[ext]" ,
208
- outputPath : paths . images
209
- }
217
+ issuer : / \. ( t s | t s x | j s | j s x ) $ / ,
218
+ resourceQuery : { not : / i n l i n e / } ,
219
+ test : / \. s v g $ / ,
220
+ type : 'javascript/auto' ,
221
+ use : [
222
+ '@svgr/webpack' ,
223
+ {
224
+ loader : 'url-loader' ,
225
+ options : {
226
+ limit : config . inlineAssets
227
+ ? config . inlineAssets
228
+ : false ,
229
+ name : '[hash].[ext]' ,
230
+ outputPath : paths . images ,
231
+ } ,
232
+ } ,
233
+ imageWebpackLoaderConfig ,
234
+ ] ,
210
235
} ,
211
236
{
212
- loader : "image-webpack-loader" ,
213
- options : {
214
- disable : false === config . imagemin ,
215
- ...( "object" === typeof config . imagemin && config . imagemin )
216
- }
217
- }
218
- ]
219
- }
220
- ]
237
+ resourceQuery : { not : / i n l i n e / } ,
238
+ generator : {
239
+ filename : `${ paths . images } /[hash][ext][query]` ,
240
+ } ,
241
+ type :
242
+ config . inlineAssets === false
243
+ ? 'asset'
244
+ : 'asset/resource' ,
245
+ use : [ imageWebpackLoaderConfig ] ,
246
+ ...( Number . isInteger ( config . inlineAssets ) && {
247
+ parser : {
248
+ dataUrlCondition : {
249
+ maxSize : config . inlineAssets ,
250
+ } ,
251
+ } ,
252
+ } ) ,
253
+ } ,
254
+ {
255
+ resourceQuery : / i n l i n e / ,
256
+ type : 'asset/inline' ,
257
+ use : [ imageWebpackLoaderConfig ] ,
258
+ } ,
259
+ ] ,
260
+ } ,
261
+ {
262
+ test : / \. ( w o f f | w o f f 2 | e o t | t t f | o t f ) $ / i,
263
+ type : 'asset/resource' ,
264
+ generator : {
265
+ filename : `${ paths . fonts } /[hash][ext][query]` ,
266
+ } ,
267
+ } ,
268
+ ] ,
221
269
} ,
222
270
plugins : [
223
271
new MiniCssExtractPlugin ( {
224
- filename : " [name].css"
272
+ filename : ' [name].css' ,
225
273
} ) ,
226
- ! hasArg ( "--no-deps" ) &&
227
- new DependencyExtractionWebpackPlugin ( { injectPolyfill : true } ) ,
274
+ ! hasArg ( '--no-deps' ) &&
275
+ new DependencyExtractionWebpackPlugin ( {
276
+ injectPolyfill : true ,
277
+ } ) ,
228
278
new RemoveSuprefluousAssetsPlugin ( ) ,
229
- ! isProduction && new LiveReloadPlugin ( )
230
- ] . filter ( Boolean )
279
+ ! isProduction && new LiveReloadPlugin ( ) ,
280
+ ] . filter ( Boolean ) ,
231
281
} ;
0 commit comments