-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
150 lines (148 loc) · 3.62 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
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
const EslintPlugin = require( 'eslint-webpack-plugin' );
const WebpackBar = require( 'webpackbar' );
const DependencyExtractionWebpackPlugin = require( '@wordpress/dependency-extraction-webpack-plugin' );
const { resolve } = require( 'path' );
const MiniCSSExtractPlugin = require( 'mini-css-extract-plugin' );
const RemoveEmptyScriptsPlugin = require( 'webpack-remove-empty-scripts' );
const CompressionPlugin = require( 'compression-webpack-plugin' );
const { WebpackManifestPlugin } = require( 'webpack-manifest-plugin' );
// eslint-disable-next-line no-unused-vars
module.exports = ( _, args ) => ( {
entry: {
customizer: resolve( process.cwd(), 'assets/js/customizer', 'index.ts' ),
'customizer-preview': resolve( process.cwd(), 'assets/js/customizer/preview', 'index.ts' ),
frontend: resolve( process.cwd(), 'assets/js/frontend', 'index.ts' ),
// style: resolve( process.cwd(), 'assets/scss', 'style.scss' ),
'editor-style': resolve( process.cwd(), 'assets/scss', 'editor-style.scss' ),
},
output: {
filename: '[name].[contenthash:8].js',
path: resolve( process.cwd(), 'assets/dist' ),
clean: true,
},
resolve: {
extensions: [ '.js', '.jsx', '.ts', '.tsx' ],
},
module: {
rules: [
{
test: /\.(js|jsx|svg|ts|tsx)$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
},
},
{
test: /\.css$/i,
use: [
{
loader: MiniCSSExtractPlugin.loader,
},
{
loader: 'css-loader',
},
],
},
{
test: /\.less$/i,
use: [
{
loader: MiniCSSExtractPlugin.loader,
},
{
loader: 'css-loader',
},
{
loader: 'less-loader',
},
],
},
{
test: /\.scss$/i,
use: [
{
loader: MiniCSSExtractPlugin.loader,
},
{
loader: 'css-loader',
},
{
loader: 'postcss-loader',
},
{
loader: 'sass-loader',
},
],
},
],
},
optimization: {
// splitChunks: {
// cacheGroups: {
// style: {
// type: 'css/mini-extract',
// test: /[\\/]style(\.module)?\.(sc|sa|c)ss$/,
// chunks: 'all',
// enforce: true,
// name( _, chunks, cacheGroupKey ) {
// const chunkName = chunks[ 0 ].name;
// return `${ dirname(
// chunkName
// ) }/${ cacheGroupKey }-${ basename( chunkName ) }`;
// },
// },
// default: false,
// },
// },
},
plugins: [
new RemoveEmptyScriptsPlugin(),
new MiniCSSExtractPlugin( { filename: '[name].[contenthash:8].css' } ),
new DependencyExtractionWebpackPlugin(),
new WebpackBar(),
...( args.mode === 'development' ? [
new EslintPlugin( { extensions: [ 'js', 'jsx', 'ts', 'tsx' ] } ),
] : [
new CompressionPlugin(),
] ),
new WebpackManifestPlugin( {
basePath: '',
fileName: 'manifest.json',
removeKeyHash: true,
generate: ( seed, files ) => {
return files.reduce( ( manifest, file ) => {
if ( ! file.chunk ) {
return manifest;
}
const entries = {};
file.chunk.files.forEach( ( path ) => {
let ext = path.split( '.' ).pop();
switch ( ext ) {
case 'js':
ext = 'script';
break;
case 'css':
ext = 'style';
break;
case 'php':
ext = 'asset';
break;
}
// else {
// if ( ! entries[ ext ] ) {
// entries[ ext ] = [];
// }
// entries[ ext ].push( path );
// }
entries[ ext ] = path;
} );
if ( file.chunk.name ) {
manifest[ file.chunk.name ] = entries;
}
return manifest;
}, seed );
},
} ),
].filter( Boolean ),
devtool: args.mode === 'development' ? 'source-map' : false,
} );