-
Notifications
You must be signed in to change notification settings - Fork 394
Expand file tree
/
Copy pathconfigureWebpack.js
More file actions
301 lines (273 loc) · 8.94 KB
/
configureWebpack.js
File metadata and controls
301 lines (273 loc) · 8.94 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
const path = require("path");
const CopyPlugin = require("copy-webpack-plugin");
const ForkTsCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
const ForkTsCheckerNotifierWebpackPlugin = require("fork-ts-checker-notifier-webpack-plugin");
const webpack = require("webpack");
/**
* Supplements the given webpack config with options required to build TerriaJS
*
* @param [options.terriaJSBasePath] The TerriaJS source directory
* @param [options.jsExtraPaths] Additional include paths for js/ts files.
* @param [options.config] Base webpack configuration
* @param [options.devMode] Set to `true` to generate for development build, default is `false`.
* @param [options.MiniCssExtractPlugin]
* @param [options.babelLoader] Optional babelLoader config, defaults to ./defaultBabelLoader.js
*/
function configureWebpack({
terriaJSBasePath,
jsExtraPaths,
config,
devMode,
MiniCssExtractPlugin,
babelLoader
}) {
const cesiumDir = path.dirname(
require.resolve("terriajs-cesium/package.json")
);
config.node = config.node || {};
config.resolve = config.resolve || {};
// Use empty polyfills for nodejs modules
config.resolve.fallback = {
fs: false, // geojson-merge, assimp
stream: false, // geojson-merge -> geojson-stream
crypto: false, // assismp.js
path: false // assimp.js
};
config.resolve.extensions = config.resolve.extensions || [
".js",
".ts",
".tsx",
".jsx"
];
config.resolve.alias = {
// @cesium/widgets will import from @cesium/engine. We need to make sure it ends up with
// the terriajs-cesium fork instead of upstream cesium.
"@cesium/engine": path.resolve(
require.resolve("terriajs-cesium/package.json"),
".."
),
...config.resolve.alias
};
config.resolve.modules = config.resolve.modules || [];
config.resolve.modules.push(path.resolve(terriaJSBasePath, "wwwroot"));
config.module = config.module || {};
config.module.rules = config.module.rules || [];
babelLoader = babelLoader || defaultBabelLoader({ devMode });
// Use Babel to compile our JavaScript and TypeScript files.
config.module.rules.push({
test: /\.(ts|js)x?$/,
include: [
path.resolve(terriaJSBasePath, "node_modules", "commander"),
path.resolve(terriaJSBasePath, "lib"),
path.resolve(terriaJSBasePath, "buildprocess", "generateDocs.ts"),
path.resolve(terriaJSBasePath, "buildprocess", "generateCatalogIndex.ts"),
path.resolve(terriaJSBasePath, "buildprocess", "patchNetworkRequests.ts"),
...jsExtraPaths
],
use: [babelLoader]
});
// Allow XML in the Models directory to be required-in as raw text.
config.module.rules.push({
test: /\.xml$/,
include: path.resolve(terriaJSBasePath, "lib", "Models"),
type: "asset/source" // inlines xml as raw text
});
// Allow .DAC file to be imported from MouseCoords.ts as URL
config.module.rules.push({
test: /\.(DAC)$/i,
type: "asset/resource",
include: [path.resolve(terriaJSBasePath, "wwwroot", "data")]
});
// Allow import of .css files from tinymce package
config.module.rules.push({
test: /\.(css)$/i,
type: "asset/source",
include: [path.dirname(require.resolve("tinymce/package.json"))]
});
// Remove Cesium debug mode checks in production. This might slightly improve performance.
// TODO: It will be good to have it enabled in devMode though, to discover issues with code
// however doing so currently breaks a few specs.
config.module.rules.push({
test: /\.js$/,
include: path.resolve(cesiumDir, "Source"),
use: [babelLoader, require.resolve("./removeCesiumDebugPragmas")]
});
// handle image imports
config.module.rules.push({
test: /\.(png|jpg|svg|gif)$/,
include: [
path.resolve(terriaJSBasePath) + path.sep,
path.resolve(cesiumDir)
],
exclude: [path.resolve(terriaJSBasePath, "wwwroot", "images", "icons")],
type: "asset" // inlines if file size < 8KB
});
// Convert imported svg icons into a sprite
// TODO: svg-sprite-loader has several security warnings - we need to find an alternative
config.module.rules.push({
test: /\.svg$/,
include: path.resolve(terriaJSBasePath, "wwwroot", "images", "icons"),
loader: require.resolve("svg-sprite-loader"),
options: {
esModule: false
}
});
config.devServer = config.devServer || {
stats: "minimal",
port: 3003,
open: true,
contentBase: "wwwroot/",
proxy: {
"*": {
target: "http://localhost:3001",
bypass: function (req, res, proxyOptions) {
if (
req.url.indexOf("/proxy") < 0 &&
req.url.indexOf("/proj4lookup") < 0 &&
req.url.indexOf("/convert") < 0 &&
req.url.indexOf("/proxyabledomains") < 0 &&
req.url.indexOf("/errorpage") < 0 &&
req.url.indexOf("/init") < 0 &&
req.url.indexOf("/serverconfig") < 0
) {
return req.originalUrl;
}
}
}
}
};
config.plugins = config.plugins || [];
// Do not import momentjs locale files
// Saves ~500kb (unzipped)
config.plugins.push(
new webpack.IgnorePlugin({
resourceRegExp: /^\.\/locale$/,
contextRegExp: /moment$/
})
);
// Copy assimpjs.wasm file - this is used by AssImpCatalogItem (see AssImpCatalogItem.forceLoadMapItems())
config.plugins.push(
new CopyPlugin({
patterns: [
{
from: require.resolve("assimpjs/dist/assimpjs.wasm"),
to: "assimpjs.wasm"
}
]
})
);
// Handle reference to Buffer in geojson-merge
config.plugins.push(
new webpack.ProvidePlugin({
Buffer: ["buffer", "Buffer"]
})
);
// Define NODE_ENV from mode setting - used in a few places in Terria
config.plugins.push(
new webpack.DefinePlugin({
"process.env.NODE_ENV": JSON.stringify(
devMode ? "development" : "production"
)
})
);
// Fork TypeScript type checking to a separate thread
config.plugins.push(
new ForkTsCheckerWebpackPlugin({
typescript: {
memoryLimit: 4096,
configFile: path.resolve(__dirname, "..", "tsconfig.json"),
diagnosticOptions: {
semantic: true,
syntactic: true
}
}
})
);
config.plugins.push(
new ForkTsCheckerNotifierWebpackPlugin({
excludeWarnings: true,
// probably don't need to know first check worked as well - disable it
skipFirstNotification: true
})
);
// Sass settings
if (MiniCssExtractPlugin) {
config.module.rules.push({
exclude: [
path.resolve(terriaJSBasePath, "lib", "Sass", "common"),
path.resolve(terriaJSBasePath, "lib", "Sass", "global")
],
include: path.resolve(terriaJSBasePath, "lib"),
test: /\.scss$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: { defaultExport: true }
},
{ loader: "terriajs-typings-for-css-modules-loader" },
{
loader: require.resolve("css-loader"),
options: {
sourceMap: true,
modules: {
localIdentName: "tjs-[name]__[local]",
exportLocalsConvention: "camelCase"
},
importLoaders: 2
}
},
{ loader: "resolve-url-loader", options: { sourceMap: true } },
{
loader: "sass-loader",
options: {
api: "modern",
sassOptions: {
sourceMap: true
}
}
}
]
});
}
// Alias react and react-dom to the one used by the building folder - apparently we can rely on the dir always being
// called node_modules https://github.com/npm/npm/issues/2734
config.resolve.alias["react"] = path.dirname(require.resolve("react"));
config.resolve.alias["react-dom"] = path.dirname(
require.resolve("react-dom")
);
// Make a terriajs-variables alias so it's really easy to override our sass variables by aliasing over the top of this.
config.resolve.alias["terriajs-variables"] =
config.resolve.alias["terriajs-variables"] ||
require.resolve("../lib/Sass/common/_default_variables.scss");
// Alias all lodash imports (including from our dependencies) to lodash-es
// This saves close to ~600KB unzipped
// Maybe we can remove this when lodash 5 comes out (?)
config.resolve.alias["lodash"] = "lodash-es";
return config;
}
const defaultBabelLoader = ({ devMode }) => ({
loader: "babel-loader",
options: {
cacheDirectory: true,
sourceMaps: !!devMode,
presets: [
[
"@babel/preset-env",
{
corejs: 3,
useBuiltIns: "usage"
}
],
["@babel/preset-react", { runtime: "automatic" }],
["@babel/preset-typescript", { allowNamespaces: true }]
],
plugins: [
["@babel/plugin-proposal-decorators", { legacy: true }],
"babel-plugin-styled-components"
],
assumptions: {
setPublicClassFields: false
}
}
});
module.exports = configureWebpack;