-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
105 lines (88 loc) · 2.11 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
const {
name: short_name,
description: name,
version,
homepage,
} = require("./package.json");
const { BannerPlugin } = require("webpack");
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const TerserPlugin = require("terser-webpack-plugin");
const path = require("path");
module.exports = (env, { mode }) => {
const is_production = mode === "production";
const is_development = !is_production;
const config = {
watch: is_development,
entry: path.resolve(__dirname, "src/entry.ts"),
output: {
path: path.resolve(__dirname, "dist"),
filename: `${short_name}.js`,
},
resolve: {
alias: {
"@": path.resolve(__dirname, "src"),
},
},
externals: {
react: "React",
"react-dom": "ReactDOM",
"@wordpress/api-fetch": "wp.apiFetch",
"@wordpress/block-editor": "wp.blockEditor",
"@wordpress/blocks": "wp.blocks",
"@wordpress/components": "wp.components",
"@wordpress/data": "wp.data",
"@wordpress/element": "wp.element",
"@wordpress/i18n": "wp.i18n",
},
module: { rules: [] },
plugins: [],
};
config.module.rules.push({
test: /\.tsx?$/,
exclude: /node_modules/,
loader: "babel-loader",
resolve: {
extensions: [".ts", ".tsx", ".js", ".jsx"],
},
});
config.module.rules.push({
test: /\.(css|styl)$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: "css-loader",
options: {
modules: {
localIdentName: is_production
? "[hash:base64:5]"
: "[name]-[local]-[hash:base64:2]",
},
},
},
"stylus-loader",
],
});
config.plugins.push(
new MiniCssExtractPlugin({
filename: `${short_name}.css`,
})
);
if (is_production) {
config.plugins.push(
new BannerPlugin({
banner: `${name} v${version} | ${homepage}`,
include: /\.(css|js)/,
})
);
config.optimization = {
minimizer: [
new CssMinimizerPlugin(),
// As we are using a custom optimization, making use of
// CssMinimizerPlugin, we also need to specify TerserPlugin
new TerserPlugin({ extractComments: false }),
],
};
}
return config;
};