-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
158 lines (151 loc) · 4.53 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
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const HtmlMinimizerPlugin = require("html-minimizer-webpack-plugin");
const HtmlReplaceWebpackPlugin = require("html-replace-webpack-plugin");
const CopyPlugin = require("copy-webpack-plugin");
const JsonMinimizerPlugin = require("json-minimizer-webpack-plugin");
const { EnvironmentPlugin } = require("webpack");
module.exports = async (_env, argv) => {
const isDevelopment = argv.mode === "development";
const gtmResource = getGtmResource();
const devtool = isDevelopment ? { devtool: "inline-source-map" } : {};
const sourceMapsLoader = isDevelopment
? [
{
test: /\.js$/,
enforce: "pre",
use: ["source-map-loader"],
},
]
: [];
return [
{
entry: {
index: "./src/index.tsx",
},
...devtool,
performance: {
maxEntrypointSize: 1024 * 1024 * 2,
maxAssetSize: 1024 * 1024 * 2,
},
output: {
path: path.resolve("./dist"),
filename: "[name].js",
chunkFilename: "[name].bundle.js",
},
stats: {
excludeModules: true,
},
optimization: {
minimizer: [`...`, new JsonMinimizerPlugin(), new HtmlMinimizerPlugin()],
},
module: {
rules: [
...sourceMapsLoader,
{
test: /\.tsx?$/,
use: [
{
loader: "ts-loader",
options: {
compilerOptions: {
sourceMap: false,
},
},
},
],
},
{
test: /\.css$/,
use: ["style-loader", "css-loader"],
},
],
},
resolve: {
extensions: [".tsx", ".ts", ".js", ".jsx"],
modules: [path.resolve("./node_modules"), path.resolve("./src")],
},
plugins: [
new HtmlWebpackPlugin({
template: "./static/index.html",
inject: false,
minify: false,
}),
new HtmlReplaceWebpackPlugin([
{
pattern: /(<!-- gtm):([\w-/]+)(\s*-->)?/g,
replacement: (match, _gtm, type) => {
if (gtmResource) {
return gtmResource[type] ?? `${match}`;
}
return `${match}`;
},
},
]),
new EnvironmentPlugin({
WEBPACK_REPLACE__contextPath: getContextPath(),
WEBPACK_REPLACE__version: getVersion(),
}),
new CopyPlugin({
patterns: [
{ from: "./static/locales", to: "./static/locales" },
{ from: "./static/images", to: "./static/images" },
{ from: "./static/favicon", to: "./static/favicon" },
{ from: "./static/slides", to: "./static/slides" },
{ from: "./static/robots.txt", to: "./robots.txt" },
{ from: "./static/site.webmanifest", to: "./site.webmanifest" },
],
}),
],
devServer: {
open: true,
server: "https",
host: "0.0.0.0",
port: 9001,
compress: true,
historyApiFallback: true,
static: [path.join(__dirname, "./dist"), path.join(__dirname, "./static")],
},
},
];
};
function getVersion() {
const version = process.env.VERSION;
console.info(`Version :: ${version}`);
return version ?? process.env.USER ?? "local";
}
function getContextPath() {
const contextPath = process.env.CONTEXT_PATH;
console.info(`Context Path :: ${contextPath}`);
return contextPath ?? "";
}
function getGtmResource() {
const gtmId = process.env.GTM_ID;
console.info(`Google Tag Manager ID :: ${gtmId}`);
if (!gtmId) {
return undefined;
}
return {
id: gtmId,
header: `<!-- Google Tag Manager -->
<script>
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','${gtmId}');
</script>
<!-- End Google Tag Manager -->`,
body: `<!-- Google Tag Manager (noscript) -->
<noscript>
<iframe
src="https://www.googletagmanager.com/ns.html?id=${gtmId}"
height="0"
width="0"
style="display:none;visibility:hidden"
>
</iframe>
</noscript>
<!-- End Google Tag Manager (noscript) -->`,
};
}