-
Notifications
You must be signed in to change notification settings - Fork 47
/
webpack.parts.js
169 lines (152 loc) · 3.8 KB
/
webpack.parts.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
const { WebpackPluginServe } = require("webpack-plugin-serve");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const path = require("path");
const glob = require("glob");
const PurgeCSSPlugin = require("purgecss-webpack-plugin");
const webpack = require("webpack");
const { GitRevisionPlugin } = require("git-revision-webpack-plugin");
const TerserPlugin = require("terser-webpack-plugin");
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
const { MiniHtmlWebpackPlugin } = require("mini-html-webpack-plugin");
const { ModuleFederationPlugin } = require("webpack").container;
const ALL_FILES = glob.sync(path.join(__dirname, "src/*.js"));
const APP_SOURCE = path.join(__dirname, "src");
exports.federateModule = ({ name, filename, exposes, remotes, shared }) => ({
plugins: [
new ModuleFederationPlugin({ name, filename, exposes, remotes, shared }),
],
});
exports.page = ({ path = "", template, title, chunks } = {}) => ({
plugins: [
new MiniHtmlWebpackPlugin({
chunks,
filename: `${path && path + "/"}index.html`,
context: { title },
template,
}),
],
});
exports.setFreeVariable = (key, value) => {
const env = {};
env[key] = JSON.stringify(value);
return {
plugins: [new webpack.DefinePlugin(env)],
};
};
exports.minifyCSS = ({ options }) => ({
optimization: {
minimizer: [new CssMinimizerPlugin({ minimizerOptions: options })],
},
});
exports.minifyJavaScript = () => ({
optimization: {
minimizer: [new TerserPlugin()],
},
});
exports.attachRevision = () => ({
plugins: [
new webpack.BannerPlugin({
banner: new GitRevisionPlugin().version(),
}),
],
});
exports.clean = () => ({
output: {
clean: true,
},
});
exports.loadJavaScript = () => ({
module: {
rules: [
{
test: /\.js$/,
include: APP_SOURCE, // Consider extracting as a parameter
use: "babel-loader",
},
],
},
});
exports.eliminateUnusedCSS = () => ({
plugins: [
new PurgeCSSPlugin({
paths: ALL_FILES, // Consider extracting as a parameter
extractors: [
{
extractor: (content) =>
content.match(/[^<>"'`\s]*[^<>"'`\s:]/g) || [],
extensions: ["html"],
},
],
}),
],
});
exports.extractCSS = ({ options = {}, loaders = [] } = {}) => {
return {
module: {
rules: [
{
test: /\.css$/,
use: [
{ loader: MiniCssExtractPlugin.loader, options },
"css-loader",
].concat(loaders),
// If you distribute your code as a package and want to
// use _Tree Shaking_, then you should mark CSS extraction
// to emit side effects. For most use cases, you don't
// have to worry about setting flag.
sideEffects: true,
},
],
},
plugins: [
new MiniCssExtractPlugin({
filename: "[name].[contenthash].css",
}),
],
};
};
exports.devServer = () => ({
watch: true,
plugins: [
new WebpackPluginServe({
port: parseInt(process.env.PORT, 10) || 8080,
static: "./dist", // Expose if output.path changes
liveReload: true,
waitForBuild: true,
}),
],
});
exports.tailwind = () => ({
loader: "postcss-loader",
options: {
postcssOptions: {
plugins: [require("tailwindcss")()],
},
},
});
exports.autoprefix = () => ({
loader: "postcss-loader",
options: {
postcssOptions: {
plugins: [require("autoprefixer")()],
},
},
});
exports.loadImages = ({ limit } = {}) => ({
module: {
rules: [
{
test: /\.(png|jpg)$/,
type: "asset",
parser: {
dataUrlCondition: {
maxSize: limit,
},
},
},
],
},
});
exports.generateSourceMaps = ({ type }) => ({
devtool: type,
});