forked from johngodley/redirection
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.js
More file actions
185 lines (172 loc) · 5.18 KB
/
webpack.config.js
File metadata and controls
185 lines (172 loc) · 5.18 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const fs = require( 'fs' );
const path = require( 'path' );
const webpack = require( 'webpack' );
const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
const pkg = require( './package.json' );
const TerserPlugin = require( 'terser-webpack-plugin' );
const MiniCSSExtractPlugin = require( 'mini-css-extract-plugin' );
const RtlCssPlugin = require( '@wordpress/scripts/plugins/rtlcss-webpack-plugin' );
const WebpackShellPluginNext = require( 'webpack-shell-plugin-next' );
const crypto = require( 'crypto' );
const { BundleAnalyzerPlugin } = require( 'webpack-bundle-analyzer' );
const versionHeader = ( md5 ) => `<?php
define( 'REDIRECTION_VERSION', '${ pkg.version }' );
define( 'REDIRECTION_BUILD', '${ md5 }' );
define( 'REDIRECTION_MIN_WP', '${ pkg.wordpress.supported }' );
`;
function generateVersion() {
fs.readFile( path.resolve( __dirname, 'build/redirection.js' ), ( error, data ) => {
const md5 = crypto.createHash( 'md5' ).update( data, 'utf8' ).digest( 'hex' );
fs.writeFileSync( path.resolve( __dirname, 'build/redirection-version.php' ), versionHeader( md5 ) );
} );
}
// Custom RTL CSS Plugin to use redirection naming
class CustomRtlCssPlugin extends RtlCssPlugin {
processAssets = ( compilation, callback ) => {
const rtlcss = require( 'rtlcss' );
const chunks = Array.from( compilation.chunks );
chunks.forEach( ( chunk ) => {
const files = Array.from( chunk.files );
files
.filter( ( filename ) => path.extname( filename ) === '.css' )
.forEach( ( filename ) => {
const src = compilation.assets[ filename ].source();
const dst = rtlcss.process( src );
// Use custom naming: redirection.css -> redirection-rtl.css
const dstFileName = filename.replace( /\.css$/, '-rtl.css' );
compilation.assets[ dstFileName ] = new webpack.sources.RawSource( dst );
chunk.files.add( dstFileName );
} );
} );
callback();
};
}
const modified = {
...defaultConfig,
output: {
...defaultConfig.output,
filename: 'redirection.js',
},
module: {
...defaultConfig.module,
rules: defaultConfig.module.rules.map( ( rule ) => {
// Find the sass-loader and configure it to suppress deprecation warnings
if (
rule &&
typeof rule === 'object' &&
rule.test &&
rule.test.toString().includes( 'scss' ) &&
Array.isArray( rule.use )
) {
return {
...rule,
use: rule.use.map( ( loader ) => {
// Handle both string and object loaders
if ( typeof loader === 'string' && loader.includes( 'sass-loader' ) ) {
return {
loader,
options: {
sassOptions: {
quietDeps: true,
silenceDeprecations: [ 'import' ],
},
},
};
}
if (
typeof loader === 'object' &&
loader &&
loader.loader &&
loader.loader.includes( 'sass-loader' )
) {
return {
...loader,
options: {
...( loader.options || {} ),
sassOptions: {
...( loader.options?.sassOptions || {} ),
quietDeps: true,
silenceDeprecations: [ 'import' ],
},
},
};
}
return loader;
} ),
};
}
return rule;
} ),
},
ignoreWarnings: [
{
module: /\.scss$/,
message: /Sass @import rules are deprecated/,
},
],
plugins: [
// Replace the default MiniCSSExtractPlugin and RtlCssPlugin with custom ones
...defaultConfig.plugins.filter(
( plugin ) => ! ( plugin instanceof MiniCSSExtractPlugin ) && ! ( plugin instanceof RtlCssPlugin )
),
new MiniCSSExtractPlugin( { filename: 'redirection.css' } ),
new CustomRtlCssPlugin(),
new webpack.DefinePlugin( {
'process.env': { NODE_ENV: JSON.stringify( process.env.NODE_ENV || 'development' ) },
REDIRECTION_VERSION: "'" + pkg.version + "'",
} ),
new WebpackShellPluginNext( {
onBuildEnd: {
scripts: [ generateVersion ],
blocking: true,
parallel: false,
},
} ),
// Add bundle analyzer when ANALYZE env var is set
...( process.env.ANALYZE ? [ new BundleAnalyzerPlugin() ] : [] ),
],
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/' ),
lib: path.resolve( __dirname, 'src/lib/' ),
component: path.resolve( __dirname, 'src/component/' ),
state: path.resolve( __dirname, 'src/state/' ),
page: path.resolve( __dirname, 'src/page/' ),
app: path.resolve( __dirname, 'src/app/' ),
types: path.resolve( __dirname, 'src/types/' ),
stores: path.resolve( __dirname, 'src/stores/' ),
},
},
optimization: {
...defaultConfig.optimization,
usedExports: true,
sideEffects: true,
minimize: true,
minimizer: [
new TerserPlugin( {
parallel: true,
terserOptions: {
output: {
comments: /translators:/i,
},
compress: {
passes: 2,
},
mangle: {
reserved: [ '__', '_n', '_nx', '_x' ],
},
},
extractComments: {
condition: true,
banner: () => {
return 'Redirection v' + pkg.version + ' - please refer to license.txt for license information';
},
},
} ),
],
},
};
module.exports = modified;