-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwebpack.config.js
124 lines (105 loc) · 2.74 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
/* eslint-disable @typescript-eslint/no-var-requires */
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");
const {
name: short_name,
description: name,
version,
homepage,
} = require("./package.json");
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"),
"@/types": path.resolve(__dirname, "types"),
},
},
externals: {
lodash: "lodash",
react: "React",
"react-dom": "ReactDOM",
"@wordpress/block-editor": "wp.blockEditor",
"@wordpress/blocks": "wp.blocks",
"@wordpress/components": "wp.components",
"@wordpress/data": "wp.data",
"@wordpress/edit-post": "wp.editPost",
"@wordpress/element": "wp.element",
"@wordpress/hooks": "wp.hooks",
"@wordpress/i18n": "wp.i18n",
"@wordpress/plugins": "wp.plugins",
"@wordpress/rich-text": "wp.richText",
},
module: { rules: [] },
plugins: [],
};
config.module.rules.push({
test: /\.tsx?$/,
exclude: /node_modules/,
loader: "babel-loader",
resolve: {
extensions: [".ts", ".tsx", ".js"],
},
});
config.module.rules.push({
test: /\.(css|styl)$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: "css-loader",
options: {
modules: {
localIdentContext: short_name,
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/,
})
);
config.plugins.push(
new BannerPlugin({
banner: [
`${name} v${version} | ${homepage}`,
`react-tiny-popover | https://github.com/alexkatz/react-tiny-popover | Alex Katz | MIT License`,
`copy-text-to-clipboard | https://github.com/sindresorhus/copy-text-to-clipboard | Sindre Sorhus | MIT License`,
].join("\n"),
include: /\.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;
};