Skip to content

Commit 5bcbf6e

Browse files
committed
Upgrade to webpack 5 and other build system improvements
1 parent 8555092 commit 5bcbf6e

13 files changed

+2357
-3169
lines changed

.babelrc

-5
This file was deleted.

.browserslistrc

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[garage]
2+
supports wasm
3+
4+
[worker]
5+
supports wasm
6+
7+
[site]
8+
> 0.25%
9+
not dead
10+
11+
[battle]
12+
> 0.25%
13+
not dead

package.json

+23-25
Original file line numberDiff line numberDiff line change
@@ -18,51 +18,49 @@
1818
"format": "prettier --write ."
1919
},
2020
"dependencies": {
21-
"@babel/polyfill": "^7.2.5",
2221
"@sentry/browser": "^6.2.5",
2322
"@sentry/tracing": "^6.2.5",
24-
"@wasmer/wasi": "^0.10.2",
25-
"@wasmer/wasm-transformer": "^0.10.2",
26-
"@wasmer/wasmfs": "^0.10.2",
23+
"@wasmer/wasi": "^0.12.0",
24+
"@wasmer/wasm-transformer": "^0.12.0",
25+
"@wasmer/wasmfs": "^0.12.0",
2726
"bootstrap": "^4.5.0",
2827
"codemirror": "^5.46.0",
2928
"comlink": "^4.3.0",
29+
"core-js": "^3.10.1",
3030
"line-by-line": "^0.1.6",
3131
"lodash": "^4.17.19",
3232
"readable-stream": "^3.6.0",
33+
"regenerator-runtime": "^0.13.7",
3334
"split.js": "^1.5.11",
3435
"wasm-feature-detect": "^1.2.3"
3536
},
3637
"devDependencies": {
37-
"@babel/core": "^7.7.5",
38-
"@babel/plugin-proposal-pipeline-operator": "^7.3.0",
39-
"@babel/plugin-syntax-dynamic-import": "^7.2.0",
40-
"@babel/preset-env": "^7.7.5",
41-
"babel-loader": "^8.0.5",
42-
"copy-webpack-plugin": "^5.1.1",
43-
"css-loader": "^3.2.1",
44-
"elm": "^0.19.1-3",
38+
"@babel/core": "^7.13.15",
39+
"@babel/preset-env": "^7.13.15",
40+
"babel-loader": "^8.2.2",
41+
"copy-webpack-plugin": "^8.1.1",
42+
"css-loader": "^5.2.0",
43+
"elm": "^0.19.1-5",
4544
"elm-format": "^0.8.3",
46-
"elm-hot-webpack-loader": "^1.1.6",
47-
"elm-webpack-loader": "^6.0.1",
45+
"elm-hot-webpack-loader": "^1.1.8",
46+
"elm-webpack-loader": "^7.0.0",
4847
"eslint": "^6.7.2",
4948
"eslint-config-standard": "^14.1.0",
5049
"eslint-plugin-import": "^2.18.2",
5150
"eslint-plugin-lodash": "^6.0.0",
5251
"eslint-plugin-node": "^10.0.0",
5352
"eslint-plugin-promise": "^4.2.1",
5453
"eslint-plugin-standard": "^4.0.1",
55-
"file-loader": "^6.0.0",
56-
"html-webpack-plugin": "^4.3.0",
57-
"mini-css-extract-plugin": "^0.8.0",
58-
"node-sass": "^4.10.0",
59-
"prettier": "^2.0.5",
60-
"sass-loader": "^8.0.0",
61-
"url-loader": "^4.1.0",
62-
"webpack": "4.41.2",
63-
"webpack-cli": "^3.3.2",
64-
"webpack-dev-server": "^3.4.1",
65-
"worker-loader": "^2.0.0"
54+
"file-loader": "^6.2.0",
55+
"html-webpack-plugin": "^5.3.1",
56+
"mini-css-extract-plugin": "^1.4.1",
57+
"node-sass": "^5.0.0",
58+
"prettier": "^2.2.1",
59+
"sass-loader": "^11.0.1",
60+
"url-loader": "^4.1.1",
61+
"webpack": "^5.31.0",
62+
"webpack-cli": "^4.6.0",
63+
"webpack-dev-server": "^3.11.2"
6664
},
6765
"version": "0.0.0"
6866
}

src/garage/app.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@ import Split from 'split.js'
99
import * as Sentry from '@sentry/browser'
1010
import { Integrations } from '@sentry/tracing'
1111

12-
Sentry.init({
13-
dsn: process.env.SENTRY_DSN,
14-
integrations: [new Integrations.BrowserTracing()],
15-
16-
// Set tracesSampleRate to 1.0 to capture 100%
17-
// of transactions for performance monitoring.
18-
// We recommend adjusting this value in production
19-
tracesSampleRate: 1.0,
20-
})
12+
if (process.env.SENTRY_DSN) {
13+
Sentry.init({
14+
dsn: process.env.SENTRY_DSN,
15+
integrations: [new Integrations.BrowserTracing()],
16+
17+
// Set tracesSampleRate to 1.0 to capture 100%
18+
// of transactions for performance monitoring.
19+
// We recommend adjusting this value in production
20+
tracesSampleRate: 1.0,
21+
})
22+
}
2123

2224
function loadSettings() {
2325
let settings
@@ -139,7 +141,7 @@ function init(node, flags, workerUrl, lang, assetsPath) {
139141

140142
window.onbeforeunload = () => {
141143
if (window.code && window.code !== window.savedCode) {
142-
return 'You\'ve made unsaved changes.'
144+
return "You've made unsaved changes."
143145
}
144146
}
145147
}

src/garage/match.worker.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import RawWasiWorker from './wasi.worker.js'
21
import * as Comlink from 'comlink'
32

43
import fetchRunner from './fetchRunner'
@@ -17,7 +16,9 @@ class MatchWorker {
1716

1817
const makeRunner = async ({ code, lang }) => {
1918
const langRunner = await fetchRunner(assetsPath, lang, () => {})
20-
const rawWorker = new RawWasiWorker()
19+
const rawWorker = new Worker(
20+
new URL('./wasi.worker.js', import.meta.url),
21+
)
2122
const WasiWorker = Comlink.wrap(rawWorker)
2223
const runner = await new WasiWorker(langRunner)
2324
await runner.setup()
@@ -44,8 +45,6 @@ class MatchWorker {
4445
worker1.terminate()
4546
worker2.terminate()
4647

47-
throw new Error('in worker')
48-
4948
console.log(`Time taken: ${(Date.now() - startTime) / 1000}s`)
5049
cb({ type: 'getOutput', data: finalState })
5150
} catch (e) {

src/garage/wasi.worker.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { WASI } from '@wasmer/wasi'
1+
import '../polyfill.js'
2+
import { WASI } from '@wasmer/wasi/lib/index.esm.js'
23
import { WasmFs } from '@wasmer/wasmfs'
3-
import wasiBindings from '@wasmer/wasi/lib/bindings/browser'
44
import * as Comlink from 'comlink'
55

66
class WasiRunner {
@@ -16,7 +16,7 @@ class WasiRunner {
1616
const wasi = (this.wasi = new WASI({
1717
preopens: { '/': '/' },
1818
bindings: {
19-
...wasiBindings,
19+
...WASI.defaultBindings,
2020
fs: wasmFs.fs,
2121
},
2222
}))
@@ -49,6 +49,8 @@ class WasiRunner {
4949
get_output(len) {
5050
const ptr = this.wasmExports.__rr_io_addr()
5151
const output = new Uint8Array(this.wasmExports.memory.buffer, ptr, len)
52+
// output is a view into the wasm memory buffer, we just want to return a
53+
// standalone uint8array buffer, so we copy it with slice()
5254
return output.slice()
5355
}
5456
init(input) {
@@ -59,9 +61,9 @@ class WasiRunner {
5961
} catch (e) {
6062
console.error('error while initializing', e, e && e.stack)
6163
console.error(this.wasmFs.fs.readFileSync('/dev/stderr', 'utf8'))
62-
return {
63-
output: new TextEncoder().encode('{"Err":{"InternalError":null}}'),
64-
}
64+
this._initResult = new TextEncoder().encode(
65+
'{"Err":{"InternalError":null}}',
66+
)
6567
}
6668
}
6769
run_turn(input) {
@@ -77,9 +79,7 @@ class WasiRunner {
7779
console.error('error while running turn', e, e && e.stack)
7880
console.error(fs.readFileSync('/dev/stderr', 'utf8'))
7981
return {
80-
output: new TextEncoder().encode(
81-
'{"Err":{"InternalError":null}}',
82-
),
82+
output: new TextEncoder().encode('{"Err":{"InternalError":null}}'),
8383
logs,
8484
}
8585
}

src/polyfill.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import 'core-js/stable'
2+
import 'regenerator-runtime/runtime'

src/site/app.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import * as Sentry from '@sentry/browser'
22
import { Integrations } from '@sentry/tracing'
33

4-
Sentry.init({
5-
dsn: process.env.SENTRY_DSN,
6-
integrations: [new Integrations.BrowserTracing()],
4+
if (process.env.SENTRY_DSN) {
5+
Sentry.init({
6+
dsn: process.env.SENTRY_DSN,
7+
integrations: [new Integrations.BrowserTracing()],
78

8-
// Set tracesSampleRate to 1.0 to capture 100%
9-
// of transactions for performance monitoring.
10-
// We recommend adjusting this value in production
11-
tracesSampleRate: 1.0,
12-
})
9+
// Set tracesSampleRate to 1.0 to capture 100%
10+
// of transactions for performance monitoring.
11+
// We recommend adjusting this value in production
12+
tracesSampleRate: 1.0,
13+
})
14+
}

webpack.common.js

+22-20
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,24 @@ const dist =
1010
: path.join(__dirname, '../backend/public/dist')
1111

1212
const loaders = {
13-
js: {
13+
js: (browserslistEnv) => ({
1414
test: /\.js$/,
1515
exclude: /node_modules/,
16-
use: 'babel-loader',
17-
},
16+
use: {
17+
loader: 'babel-loader',
18+
options: {
19+
presets: [
20+
[
21+
'@babel/preset-env',
22+
{ useBuiltIns: 'entry', corejs: 3, browserslistEnv },
23+
],
24+
],
25+
},
26+
},
27+
}),
1828
css: {
1929
test: /\.(sa|sc|c)ss$/,
20-
use: [
21-
{
22-
loader: MiniCssExtractPlugin.loader,
23-
options: {
24-
hmr: process.env.HOT === '1',
25-
},
26-
},
27-
'css-loader',
28-
'sass-loader',
29-
],
30+
use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader'],
3031
},
3132
elm: (module) => ({
3233
test: /\.elm$/,
@@ -49,10 +50,6 @@ const loaders = {
4950
test: /\.svg$/,
5051
use: 'url-loader',
5152
},
52-
worker: {
53-
test: /wasi\.worker\.js$/,
54-
use: 'worker-loader',
55-
},
5653
}
5754

5855
function mergeCustomizer(objValue, srcValue) {
@@ -62,10 +59,11 @@ function mergeCustomizer(objValue, srcValue) {
6259
}
6360

6461
function createConfigBase(dist, additional) {
65-
return _.mergeWith({
62+
const common = {
6663
mode: process.env.NODE_ENV || 'development',
6764
stats: 'minimal',
6865
output: {
66+
publicPath: 'auto',
6967
path: dist,
7068
},
7169
devtool: 'source-map',
@@ -74,10 +72,14 @@ function createConfigBase(dist, additional) {
7472
new webpack.EnvironmentPlugin({
7573
NODE_ENV: 'development',
7674
BOT_LANG: process.env.BOT_LANG || 'Python',
77-
SENTRY_DSN: process.env.NODE_ENV,
75+
SENTRY_DSN: process.env.SENTRY_DSN || '',
7876
}),
7977
],
80-
}, additional, mergeCustomizer)
78+
experiments: {
79+
asyncWebAssembly: true,
80+
},
81+
}
82+
return _.mergeWith(common, additional, mergeCustomizer)
8183
}
8284

8385
function createDevServerConfig(base) {

webpack.config.cli.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const path = require('path')
22

3-
43
const HtmlWebpackPlugin = require('html-webpack-plugin')
54
const CopyWebpackPlugin = require('copy-webpack-plugin')
65

@@ -15,7 +14,7 @@ module.exports = createConfigBase(dist, {
1514
entry: './src/cli/app.js',
1615
module: {
1716
rules: [
18-
loaders.js,
17+
loaders.js('cli'),
1918
loaders.css,
2019
loaders.elm('cli'),
2120
loaders.file,
@@ -24,13 +23,16 @@ module.exports = createConfigBase(dist, {
2423
},
2524
plugins: [
2625
new HtmlWebpackPlugin({ template: 'src/cli/index.html' }),
27-
new CopyWebpackPlugin([
28-
{
29-
from: process.env.NODE_ENV === 'production'
30-
? path.join(__dirname, './images')
31-
: path.join(__dirname, '../backend/public/images'),
32-
to: path.join(dist, 'images'),
33-
},
34-
]),
26+
new CopyWebpackPlugin({
27+
patterns: [
28+
{
29+
from:
30+
process.env.NODE_ENV === 'production'
31+
? path.join(__dirname, './images')
32+
: path.join(__dirname, '../backend/public/images'),
33+
to: path.join(dist, 'images'),
34+
},
35+
],
36+
}),
3537
],
3638
})

webpack.config.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
const path = require('path')
2+
const _ = require('lodash')
3+
const CopyPlugin = require('copy-webpack-plugin')
24

35
const {
46
createConfigBase,
@@ -11,12 +13,12 @@ function createConfig(module) {
1113
return createConfigBase(dist, {
1214
name: module,
1315
entry: {
14-
[`${module}_js`]: ['@babel/polyfill', `./src/${module}/app.js`],
16+
[`${module}_js`]: ['./src/polyfill.js', `./src/${module}/app.js`],
1517
[`${module}_css`]: `./src/${module}/main.scss`,
1618
},
1719
module: {
1820
rules: [
19-
loaders.js,
21+
loaders.js(module),
2022
loaders.css,
2123
loaders.elm(module),
2224
loaders.file,
@@ -34,7 +36,7 @@ const siteConfig = createConfigBase(dist, {
3436
site_css: './src/site/main.scss',
3537
},
3638
module: {
37-
rules: [loaders.js, loaders.css, loaders.file],
39+
rules: [loaders.js('site'), loaders.css, loaders.file],
3840
},
3941
})
4042

0 commit comments

Comments
 (0)