-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathwebpack.config.js
98 lines (88 loc) · 2.67 KB
/
webpack.config.js
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
/**
* External dependencies
*/
const fs = require( 'fs' );
const path = require( 'path' );
const webpack = require( 'webpack' );
const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
const DependencyExtractionWebpackPlugin = require( '@wordpress/dependency-extraction-webpack-plugin' );
const pkg = require( './package.json' );
const TerserPlugin = require( 'terser-webpack-plugin' );
const MiniCSSExtractPlugin = require( 'mini-css-extract-plugin' );
const WebpackShellPluginNext = require( 'webpack-shell-plugin-next' );
const crypto = require( 'crypto' );
const versionHeader = md5 => `<?php
define( 'SEARCHREGEX_VERSION', '${ pkg.version }' );
define( 'SEARCHREGEX_BUILD', '${ md5 }' );
define( 'SEARCHREGEX_MIN_WP', '${ pkg.wordpress.supported }' );
`;
function generateVersion() {
fs.readFile( path.resolve( __dirname, 'build/search-regex.js' ), ( error, data ) => {
const md5 = crypto
.createHash( 'md5' )
.update( data, 'utf8' )
.digest( 'hex' );
fs.writeFileSync( path.resolve( __dirname, 'build/search-regex-version.php' ), versionHeader( md5 ) );
} );
}
process.env.WP_NO_EXTERNALS = true;
const modified = {
...defaultConfig,
output: {
...defaultConfig.output,
filename: 'search-regex.js',
},
externals: {
'@wordpress/i18n': 'wp.i18n'
},
plugins: [
// Replace the default MiniCSSExtractPlugin with a custom one that doesn't externalise React
...defaultConfig.plugins.filter( ( plugin ) => !( plugin instanceof MiniCSSExtractPlugin ) && !( plugin instanceof DependencyExtractionWebpackPlugin ) ),
new MiniCSSExtractPlugin( { filename: 'search-regex.css' } ),
new webpack.DefinePlugin( {
'process.env': { NODE_ENV: JSON.stringify( process.env.NODE_ENV || 'development' ) },
SEARCHREGEX_VERSION: "'" + pkg.version + "'",
} ),
new WebpackShellPluginNext( {
onBuildEnd: {
scripts: [ generateVersion ],
blocking: true,
parallel: false
},
} )
],
resolve: {
...defaultConfig.resolve,
alias: {
...defaultConfig.resolve.alias,
'@wp-plugin-components': path.resolve( __dirname, 'src/wp-plugin-components' ),
'@wp-plugin-lib': path.resolve( __dirname, 'src/wp-plugin-lib/' )
}
},
optimization: {
...defaultConfig.optimization,
minimizer: [
new TerserPlugin( {
parallel: true,
terserOptions: {
output: {
comments: /translators:/i,
},
compress: {
passes: 2,
},
mangle: {
reserved: [ '__', '_n', '_nx', '_x' ],
},
},
extractComments: {
condition: true,
banner: () => {
return 'Search Regex v' + pkg.version + ' - please refer to license.txt for license information';
},
},
} ),
]
}
};
module.exports = modified;