Repository navigation
Expand file tree
/
Copy pathwebpack.config.js
More file actions
203 lines (194 loc) · 5.57 KB
/
Copy pathwebpack.config.js
File metadata and controls
203 lines (194 loc) · 5.57 KB
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
require("es6-promise").polyfill();
const path = require("path");
const webpack = require("webpack");
const bourbon = require("node-bourbon").includePaths;
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const ForkTsCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
const globImporter = require("node-sass-glob-importer");
const DEV_SOURCE_MAPS = "eval-source-map";
let plugins = [
new ForkTsCheckerWebpackPlugin(),
new HtmlWebpackPlugin({
filename: "../frontend/templates/react.tmpl",
inject: false,
templateParameters: {
isProduction: process.env.NODE_ENV === "production",
},
template: "frontend/templates/react.ejs",
}),
new webpack.DefinePlugin({
featureFlags: {},
}),
];
if (process.env.NODE_ENV === "production") {
plugins = plugins.concat([
new webpack.DefinePlugin({
"process.env": { NODE_ENV: JSON.stringify("production") },
}),
new MiniCssExtractPlugin({
filename: "bundle-[contenthash].css",
// Async chunks need this too, or their CSS is emitted unhashed — which
// the Go asset handler then serves with no-cache (see hashedAssetRe in
// server/service/frontend.go). Keeping the bundle- prefix also keeps
// these covered by the assets/bundle*.* gitignore rule.
chunkFilename: "bundle-[contenthash].css",
}),
]);
} else {
// development
plugins = plugins.concat([
new MiniCssExtractPlugin({
filename: "bundle.css",
chunkFilename: "bundle-[name].css",
}),
]);
}
const repo = __dirname;
const config = {
mode: process.env.NODE_ENV,
entry: {
bundle: path.join(repo, "frontend/index.jsx"),
},
output: {
path: path.join(repo, "assets/"),
publicPath: "/assets/",
filename: "[name].js",
},
devtool: process.env.NODE_ENV === "development" ? DEV_SOURCE_MAPS : false,
plugins,
optimization: {
minimize: process.env.NODE_ENV === "production",
},
module: {
rules: [
{
// Fleet-maintained app icons: always emitted as separate files, never
// inlined as data URLs. There are thousands of them and a page renders at
// most a screenful, so inlining any of them would put bytes in the
// bundle that almost no page load needs.
test: /\.png$/,
include: path.join(
repo,
"frontend/pages/SoftwarePage/components/icons/png"
),
type: "asset/resource",
generator: {
filename: "icons/[name]@[hash][ext]",
},
},
{
test: /\.(pdf|png|gif|ico|jpg|svg|eot|otf|woff|woff2|ttf|mp4|webm)$/,
exclude: path.join(
repo,
"frontend/pages/SoftwarePage/components/icons/png"
),
type: "asset",
generator: {
filename: "[name]@[hash][ext]",
},
},
{
test: /\.(sh|ps1)$/,
type: "asset/source",
},
{
test: /(\.tsx?|\.jsx?)$/,
exclude: /node_modules/,
use: {
loader: "esbuild-loader",
options: {
target: "es2016",
},
},
},
{
test: /\.scss$/,
exclude: /node_modules/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: "./",
},
},
{ loader: "css-loader" },
{ loader: "postcss-loader" },
{
loader: "sass-loader",
options: {
sourceMap: true,
sassOptions: {
includePaths: bourbon,
importer: globImporter(),
silenceDeprecations: [
"import",
"global-builtin",
"slash-div",
"color-functions",
"mixed-decls",
"legacy-js-api",
],
},
},
},
],
},
{
test: /\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {},
},
"css-loader",
"postcss-loader",
],
},
{
test: /\.jsx?$/,
include: path.join(repo, "frontend"),
use: { loader: "babel-loader", options: { cacheDirectory: true } },
},
],
},
performance: {
hints: false,
},
resolve: {
extensions: [".tsx", ".ts", ".js", ".jsx", ".json"],
modules: [path.resolve(path.join(repo, "./frontend")), "node_modules"],
fallback: { path: require.resolve("path-browserify") },
},
};
if (process.env.NODE_ENV !== "production") {
// Default splitChunks names shared chunks after their contents, so the names
// shift as the sharing topology does — 404ing against go-bindata -debug's
// asset list, which is frozen when generate-dev runs.
config.optimization.splitChunks = {
cacheGroups: {
defaultVendors: {
test: /[\\/]node_modules[\\/]/,
name: "vendors",
chunks: "async",
priority: -10,
reuseExistingChunk: true,
},
default: {
name: "common",
chunks: "async",
minChunks: 2,
priority: -20,
reuseExistingChunk: true,
},
},
};
}
if (process.env.NODE_ENV === "production") {
config.output.filename = "[name]-[contenthash].js";
// Route chunks need a content hash too, so the Go asset handler serves them
// with a long-lived immutable Cache-Control (see hashedAssetRe in
// server/service/frontend.go).
config.output.chunkFilename = "[name]-[contenthash].js";
}
module.exports = config;