1
1
/**
2
2
* External dependencies
3
3
*/
4
- const { existsSync, lstatSync, readdirSync, realpathSync } = require ( 'fs' ) ;
5
- const { sync : readPkgUp } = require ( 'read-pkg-up' ) ;
4
+ const { existsSync, lstatSync, readdirSync } = require ( 'fs' ) ;
6
5
const globImporter = require ( 'node-sass-glob-importer' ) ;
7
6
const LiveReloadPlugin = require ( 'webpack-livereload-plugin' ) ;
8
7
const MiniCssExtractPlugin = require ( 'mini-css-extract-plugin' ) ;
9
8
const path = require ( 'path' ) ;
9
+ const TerserPlugin = require ( 'terser-webpack-plugin' ) ;
10
10
11
11
/**
12
12
* WordPress dependencies
@@ -19,16 +19,15 @@ const DependencyExtractionWebpackPlugin = require('@wordpress/dependency-extract
19
19
const RemoveSuprefluousAssetsPlugin = require ( '../plugins/remove-superfluous-assets' ) ;
20
20
const {
21
21
getArg,
22
+ getPackagePath,
22
23
getScriptsConfig,
23
24
hasArg,
24
25
hasBabelConfig,
25
26
hasFileArg,
26
27
hasPostCSSConfig,
27
28
} = require ( '../utils' ) ;
28
29
29
- const { path : pkgPath } = readPkgUp ( {
30
- cwd : realpathSync ( process . cwd ( ) ) ,
31
- } ) ;
30
+ const pkgPath = getPackagePath ( ) ;
32
31
33
32
const config = getScriptsConfig ( ) ;
34
33
@@ -125,6 +124,14 @@ if (!hasFileArg()) {
125
124
}
126
125
}
127
126
127
+ const alias = Object . fromEntries (
128
+ Object . entries ( paths )
129
+ . filter ( ( [ key ] ) =>
130
+ [ 'scripts' , 'styles' , 'images' , 'fonts' ] . includes ( key )
131
+ )
132
+ . map ( ( [ , value ] ) => [ value , `${ paths . src } /${ value } ` ] )
133
+ ) ;
134
+
128
135
const mode = getArg (
129
136
'--mode' ,
130
137
process . env . NODE_ENV === 'production' ? 'production' : 'development'
@@ -148,11 +155,16 @@ module.exports = {
148
155
filename : '[name].js' ,
149
156
publicPath : '../' ,
150
157
} ,
158
+ resolve : {
159
+ alias,
160
+ extensions : [ '.ts' , '.tsx' , '...' ] ,
161
+ roots : [ path . resolve ( pkgPath ) ] ,
162
+ } ,
151
163
devtool : isProduction ? false : 'source-map' ,
152
164
module : {
153
165
rules : [
154
166
{
155
- test : / \. j s $ / ,
167
+ test : / \. ( t | j ) s x ? $ / ,
156
168
exclude : / n o d e _ m o d u l e s / ,
157
169
use : [
158
170
require . resolve ( 'thread-loader' ) ,
@@ -267,6 +279,16 @@ module.exports = {
267
279
} ,
268
280
] ,
269
281
} ,
282
+ optimization : {
283
+ minimize : true ,
284
+ minimizer : [
285
+ new TerserPlugin ( {
286
+ terserOptions : {
287
+ keep_classnames : true ,
288
+ } ,
289
+ } ) ,
290
+ ] ,
291
+ } ,
270
292
plugins : [
271
293
new MiniCssExtractPlugin ( {
272
294
filename : '[name].css' ,
0 commit comments