Skip to content

Commit 3a4a76e

Browse files
authored
Merge pull request #718 from TerriaJS/webpack5
Webpack 5 upgrade
2 parents 6a380b2 + 926332c commit 3a4a76e

File tree

17 files changed

+1832
-2296
lines changed

17 files changed

+1832
-2296
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111

1212
strategy:
1313
matrix:
14-
node-version: [16.x, 18.x, 20.x, 22.x]
14+
node-version: [20.x, 22.x, 23.x]
1515

1616
steps:
1717
- uses: actions/checkout@v2

CHANGES.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
# Change Log
22

3+
#### 0.3.0
4+
5+
**2025-02-18**
6+
7+
- **Breaking changes:**
8+
- Updated TerriaJS to `8.8.0`
9+
- Upgrades Webpack to version 5
10+
- Upgraded sass to version 1.80+
11+
- [Full list of TerriaJS changes](https://github.com/TerriaJS/terriajs/blob/main/CHANGES.md#880---2025-02-18)
12+
- Converted files from CJS to ESM style
13+
- This includes `entry.js`, `index.js` - if you have changed these files it is likely that you will get a merge conflict when upgrading. Please [reach out to us](https://github.com/terriajs/terriajs/discussions) if you need help.
14+
- Replaced webpack aliases `~terriajs-variables` and `~terriajs` in css imports with relative paths. This simplifies the webpack configuration.
15+
316
#### 0.2.4
417

518
**2024-12-19**

Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# develop container
2-
FROM node:16 AS develop
2+
FROM node:20 AS develop
33

44
# build container
5-
FROM node:16 AS build
5+
FROM node:20 AS build
66
USER node
77

88
COPY --chown=node:node . /app
@@ -13,7 +13,7 @@ RUN yarn install --network-timeout 1000000
1313
RUN yarn gulp release
1414

1515
# deploy container
16-
FROM node:16-slim AS deploy
16+
FROM node:20-slim AS deploy
1717

1818
USER node
1919

buildprocess/configureWebpackForPlugins.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const fs = require("fs");
99
* "terriajs-plugin-sample", "terriajs-sample-plugin" as well as
1010
* "terriajs-some-plugin-for-something".
1111
*/
12-
const PluginPackagePattern = /^terriajs-.*plugin/;
12+
const PluginPackagePattern = /(^@terriajs\/plugin-*|^terriajs-.*plugin)/;
1313

1414
function configureWebpackForPlugins(config) {
1515
config.module.rules.push(createPluginIconsRule());
@@ -71,9 +71,10 @@ function readPackageName(packageFile) {
7171
? packageJson.name
7272
: undefined;
7373
return packageJsonNames[packageFile];
74-
} catch {}
74+
} catch {
75+
return;
76+
}
7577
}
76-
return undefined;
7778
}
7879

7980
module.exports = configureWebpackForPlugins;

buildprocess/webpack.config.hot.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

buildprocess/webpack.config.js

Lines changed: 75 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
1-
"use strict";
1+
const configureWebpackForTerriaJS = require("terriajs/buildprocess/configureWebpack");
2+
const configureWebpackForPlugins = require("./configureWebpackForPlugins");
3+
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
4+
const path = require("path");
25

3-
/*global require*/
4-
var configureWebpackForTerriaJS = require("terriajs/buildprocess/configureWebpack");
5-
var configureWebpackForPlugins = require("./configureWebpackForPlugins");
6-
var MiniCssExtractPlugin = require("mini-css-extract-plugin");
7-
var path = require("path");
8-
9-
module.exports = function (devMode, hot) {
10-
var config = {
6+
/**
7+
* Webpack config for building terriamap
8+
*/
9+
module.exports = function (devMode) {
10+
// Base configuration
11+
const config = {
1112
mode: devMode ? "development" : "production",
1213
entry: "./entry.js",
1314
output: {
1415
path: path.resolve(__dirname, "..", "wwwroot", "build"),
1516
filename: "TerriaMap.js",
16-
// work around chrome needing the full URL when using sourcemaps (http://stackoverflow.com/questions/34133808/webpack-ots-parsing-error-loading-fonts/34133809#34133809)
17-
publicPath: hot ? "http://localhost:3003/build/" : "build/",
17+
publicPath: "build/",
1818
sourcePrefix: "", // to avoid breaking multi-line string literals by inserting extra tabs.
1919
globalObject: "(self || window)" // to avoid breaking in web worker (https://github.com/webpack/webpack/issues/6642)
2020
},
2121
devtool: devMode ? "eval-cheap-module-source-map" : false,
22+
2223
module: {
24+
// following rules are for terriamap source files
25+
// rules for building terriajs are configured in configureWebpackForTerriaJS
2326
rules: [
24-
{
25-
test: /\.html$/,
26-
include: path.resolve(__dirname, "..", "lib", "Views"),
27-
loader: "raw-loader"
28-
},
27+
// build source files
2928
{
3029
test: /\.(ts|js)x?$/,
3130
include: [
@@ -51,119 +50,105 @@ module.exports = function (devMode, hot) {
5150
["@babel/typescript", { allowNamespaces: true }]
5251
],
5352
plugins: [
54-
"@babel/plugin-transform-modules-commonjs",
5553
["@babel/plugin-proposal-decorators", { legacy: true }],
56-
"@babel/proposal-class-properties",
57-
"@babel/proposal-object-rest-spread",
58-
"babel-plugin-styled-components",
59-
require.resolve("@babel/plugin-syntax-dynamic-import")
54+
"babel-plugin-styled-components"
6055
]
6156
}
6257
}
63-
// Re-enable this if we need to observe any differences in the
64-
// transpilation via ts-loader, & babel's stripping of types,
65-
// or if TypeScript has newer features that babel hasn't
66-
// caught up with
67-
// {
68-
// loader: require.resolve('ts-loader'),
69-
// options: {
70-
// transpileOnly: true
71-
// // configFile: path.resolve(__dirname, '..', 'node_modules', 'terriajs', 'tsconfig.json')
72-
// }
73-
// }
7458
]
7559
},
60+
// import html file as string
61+
{
62+
test: /\.html$/,
63+
include: path.resolve(__dirname, "..", "lib", "Views"),
64+
type: "asset/source"
65+
},
66+
// import images
7667
{
7768
test: /\.(png|jpg|svg|gif)$/,
7869
include: path.resolve(__dirname, "..", "wwwroot", "images"),
79-
loader: "url-loader",
80-
options: {
81-
limit: 8192
82-
}
70+
type: "asset" // inlines as data url if size < 8kb
8371
},
72+
// import globe.gif
8473
{
8574
test: /globe\.gif$/,
8675
include: path.resolve(__dirname, "..", "lib", "Styles"),
87-
loader: "url-loader",
88-
options: {
89-
limit: 65536
76+
type: "asset",
77+
parser: {
78+
dataUrlCondition: {
79+
maxSize: 65536 // < inline as data url if size < 64k
80+
}
9081
}
9182
},
83+
// handle css files - inject in html tag
9284
{
9385
test: /loader\.css$/,
9486
include: [path.resolve(__dirname, "..", "lib", "Styles")],
95-
loader: ["style-loader", "css-loader"]
87+
use: ["style-loader", "css-loader"]
9688
},
89+
// handle scss files
9790
{
9891
test: /\.scss$/,
9992
include: [path.resolve(__dirname, "..", "lib")],
100-
loader: hot
101-
? [
102-
"style-loader",
103-
{
104-
loader: "css-loader",
105-
options: {
106-
sourceMap: true,
107-
modules: true,
108-
camelCase: true,
109-
localIdentName: "tm-[name]__[local]",
110-
importLoaders: 2
111-
}
112-
},
113-
{
114-
loader: "resolve-url-loader",
115-
options: {
116-
sourceMap: false
117-
}
118-
},
119-
"sass-loader?sourceMap"
120-
]
121-
: [
122-
MiniCssExtractPlugin.loader,
123-
{
124-
loader: "css-loader",
125-
options: {
126-
sourceMap: true,
127-
modules: true,
128-
camelCase: true,
129-
localIdentName: "tm-[name]__[local]",
130-
importLoaders: 2
131-
}
132-
},
133-
{
134-
loader: "resolve-url-loader",
135-
options: {
136-
sourceMap: false
137-
}
93+
use: [
94+
{
95+
loader: MiniCssExtractPlugin.loader,
96+
options: {
97+
// Use default export for css modules as opposed to the more
98+
// efficient named exports. This is required because most of
99+
// legacy stylesheets in TerriaJS assumes default export style.
100+
defaultExport: true
101+
}
102+
},
103+
{
104+
loader: "css-loader",
105+
options: {
106+
sourceMap: true,
107+
modules: {
108+
localIdentName: "tjs-[name]__[local]",
109+
exportLocalsConvention: "camelCase"
138110
},
139-
"sass-loader?sourceMap"
140-
]
111+
importLoaders: 2
112+
}
113+
},
114+
{
115+
loader: "resolve-url-loader",
116+
options: {
117+
sourceMap: false
118+
}
119+
},
120+
{
121+
loader: "sass-loader",
122+
options: {
123+
api: "modern",
124+
sassOptions: {
125+
sourceMap: true
126+
}
127+
}
128+
}
129+
]
141130
}
142131
]
143132
},
144133
plugins: [
134+
// Extract SASS styles into a seperate stylesheet
145135
new MiniCssExtractPlugin({
146136
filename: "TerriaMap.css",
147-
disable: hot,
148-
ignoreOrder: true,
149-
allChunks: true
137+
ignoreOrder: true
150138
})
151139
],
152140
resolve: {
153141
alias: {},
154142
modules: ["node_modules"]
155143
}
156144
};
157-
config.resolve.alias["terriajs-variables"] = require.resolve(
158-
"../lib/Styles/variables.scss"
159-
);
145+
160146
return configureWebpackForPlugins(
161-
configureWebpackForTerriaJS(
162-
path.dirname(require.resolve("terriajs/package.json")),
147+
configureWebpackForTerriaJS({
148+
terriaJSBasePath: path.dirname(require.resolve("terriajs/package.json")),
163149
config,
164150
devMode,
165-
hot,
166151
MiniCssExtractPlugin
167-
)
152+
})
168153
);
169154
};

deploy/docker/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Docker image for the primary terria map application server
2-
FROM node:16
2+
FROM node:20
33

44
RUN mkdir -p /usr/src/app && mkdir -p /etc/config/client
55
WORKDIR /usr/src/app/component

entry.js

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,10 @@
1-
const globeGif = require("./lib/Styles/globe.gif");
2-
const polyfill = require("terriajs/lib/Core/polyfill");
1+
import globeGif from "./lib/Styles/globe.gif";
2+
import "./lib/Styles/loader.css";
33

4-
require("./lib/Styles/loader.css");
5-
6-
function loadMainScript() {
7-
// load the main chunk
8-
return new Promise((resolve, reject) => {
9-
require.ensure(
10-
["terriajs/lib/Core/prerequisites"],
11-
function (require) {
12-
require("terriajs/lib/Core/prerequisites");
13-
require.ensure(
14-
["./index"],
15-
function (require) {
16-
resolve(require("./index"));
17-
},
18-
reject,
19-
"index"
20-
);
21-
},
22-
reject,
23-
"index"
24-
);
25-
});
4+
async function loadMainScript() {
5+
return import("terriajs/lib/Core/prerequisites")
6+
.then(() => import("./index"))
7+
.then(({ default: terriaPromise }) => terriaPromise);
268
}
279

2810
function createLoader() {
@@ -44,18 +26,16 @@ function createLoader() {
4426
loaderDiv.style.backgroundColor = "#383F4D";
4527
document.body.appendChild(loaderDiv);
4628

47-
polyfill(function () {
48-
loadMainScript()
49-
.catch(() => {
50-
// Ignore errors and try to show the map anyway
51-
})
52-
.then(() => {
53-
loaderDiv.classList.add("loader-ui-hide");
54-
setTimeout(() => {
55-
document.body.removeChild(loaderDiv);
56-
}, 2000);
57-
});
58-
});
29+
loadMainScript()
30+
.catch((_err) => {
31+
// Ignore errors and try to show the map anyway
32+
})
33+
.then(() => {
34+
loaderDiv.classList.add("loader-ui-hide");
35+
setTimeout(() => {
36+
document.body.removeChild(loaderDiv);
37+
}, 2000);
38+
});
5939
}
6040

6141
createLoader();

0 commit comments

Comments
 (0)