11/**
22 * External dependencies
33 */
4- const { existsSync, lstatSync, readdirSync, realpathSync } = require ( 'fs' ) ;
5- const { sync : readPkgUp } = require ( 'read-pkg-up' ) ;
4+ const { existsSync, lstatSync, readdirSync } = require ( 'fs' ) ;
65const globImporter = require ( 'node-sass-glob-importer' ) ;
76const LiveReloadPlugin = require ( 'webpack-livereload-plugin' ) ;
87const MiniCssExtractPlugin = require ( 'mini-css-extract-plugin' ) ;
98const path = require ( 'path' ) ;
9+ const TerserPlugin = require ( 'terser-webpack-plugin' ) ;
1010
1111/**
1212 * WordPress dependencies
@@ -19,16 +19,15 @@ const DependencyExtractionWebpackPlugin = require('@wordpress/dependency-extract
1919const RemoveSuprefluousAssetsPlugin = require ( '../plugins/remove-superfluous-assets' ) ;
2020const {
2121 getArg,
22+ getPackagePath,
2223 getScriptsConfig,
2324 hasArg,
2425 hasBabelConfig,
2526 hasFileArg,
2627 hasPostCSSConfig,
2728} = require ( '../utils' ) ;
2829
29- const { path : pkgPath } = readPkgUp ( {
30- cwd : realpathSync ( process . cwd ( ) ) ,
31- } ) ;
30+ const pkgPath = getPackagePath ( ) ;
3231
3332const config = getScriptsConfig ( ) ;
3433
@@ -125,6 +124,14 @@ if (!hasFileArg()) {
125124 }
126125}
127126
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+
128135const mode = getArg (
129136 '--mode' ,
130137 process . env . NODE_ENV === 'production' ? 'production' : 'development'
@@ -148,11 +155,16 @@ module.exports = {
148155 filename : '[name].js' ,
149156 publicPath : '../' ,
150157 } ,
158+ resolve : {
159+ alias,
160+ extensions : [ '.ts' , '.tsx' , '...' ] ,
161+ roots : [ path . resolve ( pkgPath ) ] ,
162+ } ,
151163 devtool : isProduction ? false : 'source-map' ,
152164 module : {
153165 rules : [
154166 {
155- test : / \. j s $ / ,
167+ test : / \. ( t | j ) s x ? $ / ,
156168 exclude : / n o d e _ m o d u l e s / ,
157169 use : [
158170 require . resolve ( 'thread-loader' ) ,
@@ -267,6 +279,16 @@ module.exports = {
267279 } ,
268280 ] ,
269281 } ,
282+ optimization : {
283+ minimize : true ,
284+ minimizer : [
285+ new TerserPlugin ( {
286+ terserOptions : {
287+ keep_classnames : true ,
288+ } ,
289+ } ) ,
290+ ] ,
291+ } ,
270292 plugins : [
271293 new MiniCssExtractPlugin ( {
272294 filename : '[name].css' ,
0 commit comments