Skip to content

Commit 4cdb01a

Browse files
committed
Revert "Revert build changes (nodejs#95)"
This reverts commit 7c74749.
1 parent 7c74749 commit 4cdb01a

File tree

8 files changed

+462
-865
lines changed

8 files changed

+462
-865
lines changed

.babelrc

-10
This file was deleted.

.github/workflows/build.yml

+49-56
Original file line numberDiff line numberDiff line change
@@ -2,79 +2,72 @@ name: Test
22

33
on:
44
push:
5-
branches: main
65
pull_request:
76
branches: main
87

9-
env:
10-
WASI_VERSION: 12
11-
WASI_VERSION_FULL: "12.0"
12-
WABT_VERSION: "1.0.24"
13-
EMCC_VERSION: "1.40.1-fastcomp"
14-
158
jobs:
169
build:
17-
runs-on: ubuntu-18.04
10+
runs-on: ubuntu-latest
11+
env:
12+
WASI_SDK_VERSION: "21"
13+
WASI_SDK_PATH: /opt/wasi-sdk
14+
WABT_VERSION: "1.0.34"
15+
WABT_PATH: /opt/wabt
16+
BINARYEN_VERSION: "117"
17+
BINARYEN_PATH: /opt/binaryen
1818
steps:
1919
- name: Checkout
20-
uses: actions/checkout@v2
21-
22-
- name: Prepare
23-
id: preparation
24-
run: |
25-
export PWD=$(pwd);
26-
echo "::set-output name=PROJ_ROOT::$PWD";
20+
uses: actions/checkout@v4
21+
- name: Setup Node
22+
uses: actions/setup-node@v4
23+
with:
24+
cache: 'npm'
2725

2826
- name: Install
29-
run: npm install
27+
run: npm ci
3028

31-
- name: Install wasi-sdk
32-
env:
33-
PROJ_ROOT: ${{ steps.preparation.outputs.PROJ_ROOT }}
29+
- name: "Restore WASI SDK"
30+
id: cache-wasi-sdk
31+
uses: actions/cache@v4
32+
with:
33+
path: ${{ env.WASI_SDK_PATH }}
34+
key: ${{ runner.os }}-wasi-sdk-${{ env.WASI_SDK_VERSION }}
35+
- name: "Install WASI SDK"
36+
if: steps.cache-wasi-sdk.outputs.cache-hit != 'true'
37+
run: |
38+
mkdir ${{ env.WASI_SDK_PATH }} && \
39+
curl -s -S --location https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${{ env.WASI_SDK_VERSION }}/wasi-sdk-${{ env.WASI_SDK_VERSION }}.0-linux.tar.gz | \
40+
tar --strip-components 1 --directory ${{ env.WASI_SDK_PATH }} --extract --gunzip
41+
- name: "Restore WABT"
42+
id: cache-wabt
43+
uses: actions/cache@v4
44+
with:
45+
path: ${{ env.WABT_PATH }}
46+
key: ${{ runner.os }}-wabt-${{ env.WABT_VERSION }}
47+
- name: "Install WABT"
48+
if: steps.cache-wabt.outputs.cache-hit != 'true'
3449
run: |
35-
cd $PROJ_ROOT;
36-
cd ../;
37-
export WASI_OS="linux"
38-
curl -sL https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${WASI_VERSION}/wasi-sdk-${WASI_VERSION_FULL}-${WASI_OS}.tar.gz -O
39-
# check if package downloaded
40-
ls -la
41-
tar xvf wasi-sdk-${WASI_VERSION_FULL}-${WASI_OS}.tar.gz
42-
# print clang version
43-
./wasi-sdk-${WASI_VERSION_FULL}/bin/clang --version
44-
- name: Install wabt
45-
env:
46-
PROJ_ROOT: ${{ steps.preparation.outputs.PROJ_ROOT }}
50+
mkdir ${{ env.WABT_PATH }} && \
51+
curl -s -S --location https://github.com/WebAssembly/wabt/releases/download/${{ env.WABT_VERSION }}/wabt-${{ env.WABT_VERSION }}-ubuntu.tar.gz | \
52+
tar --strip-components 1 --directory ${{ env.WABT_PATH }} --extract --gunzip
53+
- name: "Restore Binaryen"
54+
id: cache-binaryen
55+
uses: actions/cache@v4
56+
with:
57+
path: ${{ env.BINARYEN_PATH }}
58+
key: ${{ runner.os }}-binaryen-${{ env.BINARYEN_VERSION }}
59+
- name: "Install Binaryen"
60+
if: steps.cache-binaryen.outputs.cache-hit != 'true'
4761
run: |
48-
cd $PROJ_ROOT;
49-
cd ../;
50-
if [[ "$RUNNER_OS" == "Linux" ]]; then
51-
export WABT_OS="ubuntu";
52-
fi
53-
if [[ "$RUNNER_OS" == "macOS" ]]; then
54-
export WABT_OS="macos";
55-
fi
56-
if [[ "$RUNNER_OS" == "Windows" ]]; then
57-
export WABT_OS="windows";
58-
fi
59-
curl -sL https://github.com/WebAssembly/wabt/releases/download/${WABT_VERSION}/wabt-${WABT_VERSION}-${WABT_OS}.tar.gz -O
60-
# check if package downloaded
61-
ls -la
62-
tar xvf wabt-${WABT_VERSION}-${WABT_OS}.tar.gz
63-
# check if wabt binaries installed
64-
ls -la ./wabt-${WABT_VERSION}/bin/
62+
mkdir ${{ env.BINARYEN_PATH }} && \
63+
curl -s -S --location https://github.com/WebAssembly/binaryen/releases/download/version_${{ env.BINARYEN_VERSION }}/binaryen-version_${{ env.BINARYEN_VERSION }}-x86_64-linux.tar.gz | \
64+
tar --strip-components 1 --directory ${{ env.WABT_PATH }} --extract --gunzip
6565
- name: Compile to Wasm & Test Wasm
66-
env:
67-
PROJ_ROOT: ${{ steps.preparation.outputs.PROJ_ROOT }}
6866
run: |
69-
cd $PROJ_ROOT;
7067
rm lib/lexer.wasm;
7168
npm run build-wasm;
7269
npm run build;
7370
# test
7471
npm run test;
7572
- name: Benchmark Wasm
76-
env:
77-
PROJ_ROOT: ${{ steps.preparation.outputs.PROJ_ROOT }}
78-
run: |
79-
cd $PROJ_ROOT;
80-
npm run bench;
73+
run: npm run bench;

.github/workflows/node.js.yml

-30
This file was deleted.

Makefile

+16
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
11
# These flags depend on the system and may be overridden
2+
3+
ifeq ($(WASI_SDK_PATH),)
24
WASM_CC := ../wasi-sdk-12.0/bin/clang
35
WASM_CFLAGS := --sysroot=../wasi-sdk-12.0/share/wasi-sysroot
6+
else
7+
WASM_CC := $(WASI_SDK_PATH)/bin/clang
8+
WASM_CFLAGS := --sysroot=$(WASI_SDK_PATH)/share/wasi-sysroot
9+
endif
10+
411
WASM_LDFLAGS := -nostartfiles
512

13+
ifeq ($(WABT_PATH),)
614
WASM2WAT := ../wabt/bin/wasm2wat
15+
else
16+
WASM2WAT := $(WABT_PATH)/bin/wasm2wat
17+
endif
18+
19+
ifeq ($(BINARYEN_PATH),)
720
WASM_OPT := ../binaryen/bin/wasm-opt
21+
else
22+
WASM_OPT := $(BINARYEN_PATH)/bin/wasm-opt
23+
endif
824

925
# These are project-specific and are expected to be kept intact
1026
WASM_EXTRA_CFLAGS := -I include-wasm/ -Wno-logical-op-parentheses -Wno-parentheses -Oz

build.js

+60-12
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,73 @@
11
const fs = require('fs');
2-
const terser = require('terser');
2+
const { buildSync } = require('esbuild');
3+
const path = require('path/posix')
34

4-
const MINIFY = true;
5+
const { EXTERNAL_PATH } = process.env;
6+
const MINIFY = !EXTERNAL_PATH;
57

68
try { fs.mkdirSync('./dist'); }
79
catch (e) {}
810

911
const wasmBuffer = fs.readFileSync('./lib/lexer.wasm');
10-
const jsSource = fs.readFileSync('./src/lexer.js').toString();
1112
const pjson = JSON.parse(fs.readFileSync('./package.json').toString());
1213

13-
const jsSourceProcessed = jsSource.replace('WASM_BINARY', wasmBuffer.toString('base64'));
14+
buildSync({
15+
entryPoints: ['./src/lexer.js'],
16+
outfile: './dist/lexer.mjs',
17+
bundle: true,
18+
minify: MINIFY,
19+
platform: 'node',
20+
format: 'esm',
21+
banner: {
22+
js: `/* cjs-module-lexer ${pjson.version} */`
23+
},
24+
define: EXTERNAL_PATH ? {
25+
WASM_BINARY: 'undefined',
26+
EXTERNAL_PATH: `'${path.join(EXTERNAL_PATH, 'lib/lexer.wasm')}'`,
27+
} : {
28+
WASM_BINARY: `'${wasmBuffer.toString('base64')}'`,
29+
EXTERNAL_PATH: 'undefined'
30+
}
31+
})
1432

15-
const minified = MINIFY && terser.minify(jsSourceProcessed, {
16-
module: true,
17-
output: {
18-
preamble: `/* cjs-module-lexer ${pjson.version} */`
33+
if (EXTERNAL_PATH) {
34+
buildSync({
35+
stdin: {
36+
contents: `'use strict';
37+
let lazy;
38+
async function init () {
39+
if (!lazy) {
40+
lazy = await import(require('node:url').pathToFileURL(require('node:module').createRequire('${EXTERNAL_PATH}/dist/lexer.js').resolve('./lexer.mjs')));
1941
}
20-
});
42+
module.exports = lazy;
43+
return lazy.init();
44+
}
45+
46+
function parse (source, name = '@') {
47+
if (!lazy)
48+
throw new Error('Not initialized');
49+
50+
return lazy.parse(source, name);
51+
}
2152
22-
if (minified.error)
23-
throw minified.error;
53+
module.exports = { init, parse };`,
54+
loader: 'js',
55+
},
56+
outfile: './dist/lexer.js',
57+
minify: MINIFY,
58+
platform: 'node',
59+
format: 'cjs',
60+
});
61+
} else {
62+
buildSync({
63+
entryPoints: ['./dist/lexer.mjs'],
64+
outfile: './dist/lexer.js',
65+
minify: MINIFY,
66+
platform: 'node',
67+
format: 'cjs',
68+
banner: {
69+
js: `/* cjs-module-lexer ${pjson.version} */`
70+
}
71+
})
72+
}
2473

25-
fs.writeFileSync('./dist/lexer.mjs', minified ? minified.code : jsSourceProcessed);

0 commit comments

Comments
 (0)