-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathwebpack.config.js
200 lines (185 loc) · 4.78 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
const path = require('path');
const webpack = require('webpack');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const ESLintPlugin = require('eslint-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const StyleLintPlugin = require('stylelint-webpack-plugin');
const { EsbuildPlugin } = require('esbuild-loader');
// Project paths.
const SRC_PATH = path.resolve(__dirname, './src/');
const OUT_PATH = path.resolve(__dirname, '../wp-content/themes/humanity-theme/assets/');
/**
* Filter entry points if specified in cli
* @param {Object} env cli env vars
* @returns {Object}
*/
const getEntries = (env) => {
const entries = {
bundle: path.resolve(__dirname, `${SRC_PATH}/scripts/App.js`),
blocks: path.resolve(__dirname, `${SRC_PATH}/scripts/blocks.js`),
editor: path.resolve(__dirname, `${SRC_PATH}/scripts/editor.js`),
admin: path.resolve(__dirname, `${SRC_PATH}/scripts/admin.js`),
};
if (!env?.entry) {
return entries;
}
const entry = {};
env.entry.split(',').forEach((name) => {
if (Object.hasOwnProperty.call(entries, name)) {
entry[name] = entries[name];
}
});
return entry;
};
/**
* Filter list of loaded plugins based on cli args
* @param {Object} argv raw cli args
* @param {Object} env cli env vars
* @returns {Array}
*/
const getPlugins = (argv, env) => {
const staticPlugins = [
new CopyWebpackPlugin({
patterns: [
{
context: SRC_PATH,
from: 'static/**/*',
to({ absoluteFilename }) {
return absoluteFilename.replace(`${SRC_PATH}/static`, OUT_PATH);
},
},
],
}),
];
// if we're only building static assets, skip CSS extraction
if (env?.entry === 'static') {
return [
new ESLintPlugin({ extensions: ['js', 'jsx', 'ts', 'tsx'] }),
// Sets mode so we can access it in `postcss.config.js`.
new webpack.LoaderOptionsPlugin({ options: { mode: argv.mode } }),
new StyleLintPlugin({ threads: true }),
...staticPlugins,
];
}
const plugins = [
new ESLintPlugin({ extensions: ['js', 'jsx', 'ts', 'tsx'] }),
// Sets mode so we can access it in `postcss.config.js`.
new webpack.LoaderOptionsPlugin({ options: { mode: argv.mode } }),
// Extract CSS to own bundle, filename relative to output.path.
new MiniCssExtractPlugin({ filename: '../styles/[name].css', chunkFilename: '[name].css' }),
new StyleLintPlugin({ threads: true }),
];
// we're specifying an entry point that isn't static, skip static processing
if (env?.entry) {
return plugins;
}
// do everything
return [...plugins, ...staticPlugins];
};
/**
* Get the cache configuration for the build mode
* @param {String} mode the build mode
* @returns {Object|False}
*/
const getCacheConf = (mode) => {
if (mode === 'production') {
return false;
}
return {
type: 'filesystem',
profile: false,
buildDependencies: {
config: [__filename],
},
};
};
const config = (env, argv) => ({
bail: argv.mode === 'production',
cache: getCacheConf(argv.mode),
target: 'web',
profile: false,
devtool: 'source-map',
entry: getEntries(env),
output: {
filename: '[name].js',
path: path.resolve(__dirname, `${OUT_PATH}/scripts`),
pathinfo: false,
},
externals: {
lodash: 'lodash',
react: 'React',
'react-dom': 'ReactDOM',
'@wordpress/i18n': 'wp.i18n',
},
watchOptions: {
ignored: /node_modules/,
},
performance: {
hints: false,
},
optimization: {
minimize: argv.mode === 'production',
minimizer: [
new EsbuildPlugin({
target: 'es2015',
css: true,
}),
],
},
resolve: {
symlinks: false,
},
stats: {
all: false,
assets: true,
errors: true,
errorDetails: true,
excludeAssets: [/\.(eot|ttf|woff2?|jpg|png|svg)$/],
},
plugins: getPlugins(argv, env),
module: {
rules: [
{
test: /App\.js$/,
loader: 'expose-loader',
options: {
exposes: 'App',
},
},
{
test: /\.s?(a|c)?ss$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
sourceMap: true,
url: false,
},
},
{
loader: 'postcss-loader',
options: {
sourceMap: true,
},
},
{
loader: 'sass-loader',
options: {
sourceMap: true,
},
},
],
},
{
test: /\.jsx?$/,
exclude: /(lodash|node_modules|react(-dom)?)/,
loader: 'esbuild-loader',
options: {
target: 'es2015',
},
},
],
},
});
module.exports = (env, argv) => config(env, argv);