-
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathwebpack.config.js
More file actions
220 lines (201 loc) · 5.45 KB
/
webpack.config.js
File metadata and controls
220 lines (201 loc) · 5.45 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
const path = require("path");
const CopyPlugin = require("copy-webpack-plugin");
const { ProvidePlugin } = require("webpack");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
const config = require("./config.js");
module.exports = (env) => {
let { publicPath, skin } = { ...config, ...env };
const { analyzeBundle, example } = { ...config, ...env };
// output filename: deployment config
let outputFilename = "[name].[contenthash].bundle.js";
// HtmlWebpackPlugin: deployment config
let htmlConfig = {
inject: false,
filename: "headstart.php",
templateContent: ({ htmlWebpackPlugin }) =>
`${htmlWebpackPlugin.tags.headTags}
${htmlWebpackPlugin.tags.bodyTags}`,
};
// MiniCssExtractPlugin: deployment config
const miniCssConfig = {
filename: "[name].[contenthash].css",
};
// local examples config overrides
const exampleConfig = getExampleConfig(example);
if (exampleConfig) {
publicPath = "http://localhost:8080/dist/";
skin = exampleConfig.skin;
outputFilename = "[name].bundle.js";
// HtmlWebpackPlugin
htmlConfig = {
template: exampleConfig.template,
};
// MiniCssExtractPlugin
miniCssConfig.filename = "[name].css";
}
return {
devtool: "eval-source-map",
entry: {
headstart: "./vis/index.ts",
},
output: {
filename: outputFilename,
path: path.resolve(__dirname, "dist"),
publicPath: publicPath,
library: {
name: "headstart",
type: "var",
},
clean: true,
},
devServer: {
open: true,
static: [
path.resolve(__dirname, "dist/"),
path.resolve(__dirname, "examples/public/"),
],
devMiddleware: { publicPath: "/dist/", writeToDisk: true },
},
resolve: {
extensions: [".js", ".jsx", ".ts", ".tsx"],
alias: {
// Aliases for the root frontend folder (vis) and the folder where
// code of components is mostly located (vis/js)
"@": path.resolve(__dirname, "vis/"),
"@js": path.resolve(__dirname, "vis/js/"),
// Aliases for hypher and markjs are created for more convenient
// import of them from the node_modules/ folder
hypher: "hypher/dist/jquery.hypher.js",
markjs: "mark.js/dist/jquery.mark.js",
},
},
plugins: [
new HtmlWebpackPlugin(htmlConfig),
new ProvidePlugin({
process: "process/browser",
jQuery: "jquery",
}),
new MiniCssExtractPlugin(miniCssConfig),
...(analyzeBundle ? [new BundleAnalyzerPlugin()] : []),
new CopyPlugin({
patterns: [
{
from: path.resolve(__dirname, "node_modules/leaflet/dist/images"),
to: "images",
},
],
}),
],
module: {
rules: [
{
test: /\.tsx?$/,
use: "ts-loader",
exclude: /node_modules/,
},
{
test: /\.[j]sx?$/,
exclude: /node_modules/,
use: "babel-loader",
},
{
test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
type: "asset/resource",
generator: {
filename: "fonts/[name][ext]",
},
},
{
test: /\.(png|jpe?g|gif)$/i,
type: "asset/resource",
generator: {
filename: "images/[name][ext]",
},
},
{
test: /\.(sa|sc)ss$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: "css-loader",
options: {
url: true,
esModule: false,
},
},
{
loader: "sass-loader",
options: {
additionalData: `$skin: "${process.env.SKIN || ""}";`,
sassOptions: {
includePaths: ["node_modules"],
},
},
},
],
},
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: "css-loader",
options: { url: true, esModule: false },
},
],
},
{ test: /\.csl$/, type: "asset/source" },
],
},
optimization: {
// deterministic = stable hashes between builds
moduleIds: "deterministic",
// single = optimized for import into same page
runtimeChunk: "single",
splitChunks: {
// max chunk size in B
maxSize: 500000,
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: "vendors",
chunks: "all",
},
},
},
},
};
};
const getExampleConfig = (example) => {
switch (example) {
case "base":
return {
skin: "",
template: "examples/templates/base.html",
};
case "pubmed":
return {
skin: "",
template: "examples/templates/pubmed.html",
};
case "triple":
return {
skin: "triple",
template: "examples/templates/triple.html",
};
case "viper":
return {
skin: "viper",
template: "examples/templates/viper.html",
};
case "covis":
return {
skin: "covis",
template: "examples/templates/covis.html",
};
default:
return null;
}
};