-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwebpack.config.js
204 lines (198 loc) · 6.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
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
201
202
203
204
const path = require("path");
const webpack = require("webpack");
const Dotenv = require("dotenv-webpack");
const CopyPlugin = require("copy-webpack-plugin");
const StatoscopeWebpackPlugin = require("@statoscope/webpack-plugin").default;
const { DuplicatesPlugin } = require("inspectpack/plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CompressionPlugin = require("compression-webpack-plugin");
const bundleOutputDir = "./dist";
module.exports = (env) => {
console.log("ENVs", env);
let devtool = "inline-source-map";
const isDevBuild = env.TARGET_ENV === "development";
let plugins = [];
switch (env.TARGET_ENV) {
case "development":
plugins = [
new Dotenv({
path: path.join(__dirname, ".env.development"),
}),
new webpack.ProvidePlugin({
process: "process/browser",
Buffer: ["buffer", "Buffer"],
}),
new StatoscopeWebpackPlugin(),
new CopyPlugin([{ from: "html-dev/" }]),
new MiniCssExtractPlugin({
filename: "[name].css",
}),
new DuplicatesPlugin({
// Emit compilation warning or error? (Default: `false`)
emitErrors: false,
// Handle all messages with handler function (`(report: string)`)
// Overrides `emitErrors` output.
emitHandler: undefined,
// List of packages that can be ignored. (Default: `[]`)
// - If a string, then a prefix match of `{$name}/` for each module.
// - If a regex, then `.test(pattern)` which means you should add slashes
// where appropriate.
//
// **Note**: Uses posix paths for all matching (e.g., on windows `/` not `\`).
ignoredPackages: undefined,
// Display full duplicates information? (Default: `false`)
verbose: true,
}),
];
break;
case "production":
devtool = false;
plugins = [
new Dotenv({
path: path.join(__dirname, ".env.production"),
allowEmptyValues: false,
}),
new webpack.ProvidePlugin({
process: "process/browser",
Buffer: ["buffer", "Buffer"],
}),
new StatoscopeWebpackPlugin(),
new CopyPlugin([{ from: "html-production/" }]),
new MiniCssExtractPlugin({
filename: "[name].css",
}),
new CompressionPlugin({
test: /\.(js|css)$/, // Compress .js and .css files
algorithm: "gzip", // Default compression algorithm
threshold: 10240, // Only assets bigger than this size (10 Kilobytes) are processed
minRatio: 0.8 // Only assets that compress better than this ratio are processed
}),
];
break;
case "staging":
devtool = false;
plugins = [
new Dotenv({
path: path.join(__dirname, ".env.staging"),
allowEmptyValues: false,
}),
new webpack.ProvidePlugin({
process: "process/browser",
Buffer: ["buffer", "Buffer"],
}),
new StatoscopeWebpackPlugin(),
new CopyPlugin([{ from: "html-staging/" }]),
new MiniCssExtractPlugin({
filename: "[name].css",
}),
new CompressionPlugin({
test: /\.(js|css)$/, // Compress .js and .css files
algorithm: "gzip", // Default compression algorithm
threshold: 10240, // Only assets bigger than this size (10 Kilobytes) are processed
minRatio: 0.8 // Only assets that compress better than this ratio are processed
}),
];
break;
default:
throw new Error(`Unsupported TARGET_ENV: ${env.TARGET_ENV}`);
}
return [
{
entry: "./src/index.ts",
devtool: devtool,
output: {
filename: "widget.js",
path: path.resolve(bundleOutputDir),
},
devServer: {
static: bundleOutputDir,
},
plugins: plugins,
optimization: {
minimize: !isDevBuild,
},
mode: isDevBuild ? "development" : "production",
module: {
rules: [
// packs SVG's discovered in url() into bundle
{ test: /\.svg/, use: "svg-url-loader" },
{
test: /\.css$/i,
use: [
{ loader: MiniCssExtractPlugin.loader },
{
loader: "css-loader",
},
{
loader: "postcss-loader",
options: {
postcssOptions: {
plugins: [
require("autoprefixer")(),
require("tailwindcss")(),
],
},
},
},
],
sideEffects: true,
},
// use babel-loader for TS and JS modeles,
// starting v7 Babel babel-loader can transpile TS into JS,
// so no need for ts-loader
// note, that in dev we still use tsc for type checking
{
test: /\.(js|ts|tsx|jsx)$/,
exclude: [/node_modules/, /access-protocol/],
use: [
{
loader: "babel-loader",
options: {
presets: [
["@babel/preset-env"],
[
// enable transpiling ts => js
"@babel/typescript",
// tell babel to compile JSX using into Preact
{ jsxPragma: "h" },
],
],
plugins: [
// syntax sugar found in React components
"@babel/proposal-class-properties",
"@babel/proposal-object-rest-spread",
// transpile JSX/TSX to JS
[
"@babel/plugin-transform-react-jsx",
{
// we use Preact, which has `Preact.h` instead of `React.createElement`
pragma: "h",
pragmaFrag: "Fragment",
},
],
],
},
},
],
},
],
},
resolve: {
extensions: ["*", ".js", ".ts", ".tsx"],
alias: {
react: "preact/compat",
"react-dom/test-utils": "preact/test-utils",
"react-dom": "preact/compat", // Must be below test-utils
"react/jsx-runtime": "preact/jsx-runtime",
},
fallback: {
crypto: require.resolve("crypto-browserify"),
stream: require.resolve("stream-browserify"),
path: require.resolve("path-browserify"),
buffer: require.resolve("buffer"),
zlib: require.resolve("browserify-zlib"),
},
},
},
];
};