-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
webpack.config.js
100 lines (87 loc) · 2.25 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
/**
* External Dependencies
*/
const { resolve } = require('path');
const glob = require('glob');
const defaultConfig = require('@wordpress/scripts/config/webpack.config');
const RemoveEmptyScriptsPlugin = require('webpack-remove-empty-scripts');
const RtlCssPlugin = require('rtlcss-webpack-plugin');
const isProduction = process.env.NODE_ENV === 'production';
// Prepare JS for controls.
const entryControls = glob.sync('./controls/**/script.js').reduce(function (
entries,
entry
) {
const matchForRename = /controls\/([\w\d_]+)\/script\.js$/g.exec(entry);
if (matchForRename !== null && typeof matchForRename[1] !== 'undefined') {
entries[`control-${matchForRename[1].replace('_', '-')}`] = resolve(
process.cwd(),
entry
);
}
return entries;
}, {});
const newConfig = {
...defaultConfig,
...{
entry: {
// JS.
'admin-templates': resolve(
process.cwd(),
'assets/admin/templates',
'index.js'
),
'admin-tools': resolve(
process.cwd(),
'assets/admin/tools',
'index.js'
),
editor: resolve(process.cwd(), 'assets/editor', 'index.js'),
'editor-translation': resolve(
process.cwd(),
'assets/editor',
'translation.js'
),
'block-builder': resolve(
process.cwd(),
'assets/block-builder',
'index.js'
),
// JS controls.
...entryControls,
// SCSS.
'admin-style': resolve(process.cwd(), 'assets/admin', 'index.scss'),
'block-builder-astra-style': resolve(
process.cwd(),
'assets/block-builder/3rd',
'astra.scss'
),
},
// Display minimum info in terminal.
stats: 'minimal',
},
plugins: [
...defaultConfig.plugins,
new RtlCssPlugin({
filename: `[name]-rtl.css`,
}),
],
};
// Production only.
if (isProduction) {
// Remove JS files created for styles
// to prevent enqueue it on production.
newConfig.plugins = [new RemoveEmptyScriptsPlugin(), ...newConfig.plugins];
}
// Development only.
if (!isProduction) {
newConfig.devServer = {
...newConfig.devServer,
// Support for dev server on all domains.
allowedHosts: 'all',
};
// Fix HMR is not working with multiple entries.
// @thanks https://github.com/webpack/webpack-dev-server/issues/2792#issuecomment-806983882
newConfig.optimization.runtimeChunk = 'single';
}
module.exports = newConfig;