-
Notifications
You must be signed in to change notification settings - Fork 6
/
webpack.config.js
176 lines (167 loc) · 5.68 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
//@ts-check
"use strict";
const glob = require("glob");
const path = require("path");
const TerserPlugin = require("terser-webpack-plugin");
const webpack = require("webpack");
const merge = require("webpack-merge").merge;
const { mockTmcLocalMooc, mockBackend, productionApi } = require("./config");
/**@type {() => import("webpack").Configuration} */
const config = () => {
const isDevelopmentMode = process.env.NODE_ENV && process.env.NODE_ENV === "development";
const apiConfig = (() => {
switch (process.env.BACKEND) {
case "mockTmcLocalMooc":
return mockTmcLocalMooc;
case "mockBackend":
return mockBackend;
case "production":
return productionApi;
default:
console.warn("No backend set, defaulting to `production`");
return productionApi;
}
})();
/**@type {import('webpack').Configuration}*/
const commonConfig = {
target: "node",
entry: {
extension: "./src/extension.ts",
"testBundle.test": glob.globSync("./src/test/**/*.test.ts"),
"integration.spec": glob.globSync("./src/test-integration/**/*.spec.ts"),
},
output: {
path: path.resolve(__dirname, "dist"),
filename: "[name].js",
libraryTarget: "commonjs2",
devtoolModuleFilenameTemplate: "../[resource-path]",
},
externals: {
chai: "commonjs chai",
mocha: "commonjs mocha",
vscode: "commonjs vscode",
"vscode-test": "commonjs vscode-test",
},
resolve: {
extensions: [".ts", ".js"],
alias: {
handlebars: "handlebars/dist/handlebars.min.js",
// systeminformation issue https://github.com/sebhildebrandt/systeminformation/issues/701
"osx-temperature-sensor": false,
},
// solves an issue with ts-loader with ttypescript
// using the incorrect import paths for .d.ts files:
// https://github.com/TypeStrong/ts-loader/issues/1501
preferRelative: true,
fallback: {
async_hooks: false,
},
},
node: {
__dirname: false,
},
infrastructureLogging: {
level: "log",
},
optimization: {
splitChunks: {
chunks: "all",
name(module, chunks) {
return chunks.find((x) => x.name === "extension") ? "lib" : "testlib";
},
},
},
module: {
rules: [
{
test: /\.css$/i,
exclude: /node_modules/,
use: [
{
loader: "raw-loader",
},
],
},
{
test: /\.jsx$/,
exclude: /node_modules/,
use: [
{
loader: "babel-loader",
options: {
presets: ["@babel/preset-env"],
plugins: [
[
"@babel/plugin-transform-react-jsx",
{ pragma: "createElement" },
],
],
},
},
],
},
{
test: /\.md$/i,
exclude: /node_modules/,
use: [
{
loader: "raw-loader",
},
],
},
{
test: /\.ts$/,
exclude: /node_modules/,
use: [
{
loader: "ts-loader",
options: {
compiler: "ts-patch/compiler",
configFile: "tsconfig.json",
},
},
],
},
],
},
plugins: [
new webpack.DefinePlugin({
__DEBUG_MODE__: JSON.stringify(isDevelopmentMode),
...apiConfig,
}),
],
watchOptions: {
ignored: /node_modules/,
},
};
/**@returns {import('webpack').Configuration}*/
const devConfig = () => ({
mode: "development",
devtool: "inline-source-map",
});
// Type definition broken for the Nth time
// /**@returns {import('webpack').Configuration}*/
/**@returns {any} */
const prodConfig = () => ({
mode: "production",
optimization: {
minimizer: [
new TerserPlugin({
terserOptions: {
keep_fnames: /createElement/,
mangle: {
reserved: ["createElement"],
},
},
}),
],
},
});
console.log(
`Webpack building in ${isDevelopmentMode ? "development" : "production"} configuration.`,
);
console.log(`Configured TMC backend: ${apiConfig.__TMC_BACKEND_URL__}`);
console.log(`Configured MOOC backend: ${apiConfig.__MOOC_BACKEND_URL__}`);
return merge(commonConfig, isDevelopmentMode ? devConfig() : prodConfig());
};
module.exports = config;