Skip to content

Commit 7c74749

Browse files
authoredApr 28, 2024··
Revert build changes (#95)
* Revert "tests: fix loading wasm when targeting external deps (#94)" This reverts commit bcc6ce4. * Revert "fix: cannot resolve path when build with EXTERNAL_PATH (#93)" This reverts commit 8ea767a. * Revert "Support building for externally shared js builtins (#91)" This reverts commit 1c5072c.
1 parent bcc6ce4 commit 7c74749

File tree

8 files changed

+865
-462
lines changed

8 files changed

+865
-462
lines changed
 

‎.babelrc

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"plugins": [
3+
[
4+
"@babel/plugin-transform-modules-commonjs",
5+
{
6+
"strict": true
7+
},
8+
]
9+
]
10+
}

‎.github/workflows/build.yml

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

33
on:
44
push:
5+
branches: main
56
pull_request:
67
branches: main
78

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+
815
jobs:
916
build:
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
17+
runs-on: ubuntu-18.04
1818
steps:
1919
- name: Checkout
20-
uses: actions/checkout@v4
21-
- name: Setup Node
22-
uses: actions/setup-node@v4
23-
with:
24-
cache: 'npm'
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";
2527
2628
- name: Install
27-
run: npm ci
29+
run: npm install
2830

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'
31+
- name: Install wasi-sdk
32+
env:
33+
PROJ_ROOT: ${{ steps.preparation.outputs.PROJ_ROOT }}
4934
run: |
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'
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 }}
6147
run: |
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
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/
6565
- name: Compile to Wasm & Test Wasm
66+
env:
67+
PROJ_ROOT: ${{ steps.preparation.outputs.PROJ_ROOT }}
6668
run: |
69+
cd $PROJ_ROOT;
6770
rm lib/lexer.wasm;
6871
npm run build-wasm;
6972
npm run build;
7073
# test
7174
npm run test;
7275
- name: Benchmark Wasm
73-
run: npm run bench;
76+
env:
77+
PROJ_ROOT: ${{ steps.preparation.outputs.PROJ_ROOT }}
78+
run: |
79+
cd $PROJ_ROOT;
80+
npm run bench;

‎.github/workflows/node.js.yml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3+
4+
name: Node.js CI
5+
6+
on:
7+
push:
8+
branches: [ master ]
9+
pull_request:
10+
branches: [ master ]
11+
12+
jobs:
13+
build:
14+
15+
runs-on: ubuntu-latest
16+
17+
strategy:
18+
matrix:
19+
node-version: [14.x]
20+
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
21+
22+
steps:
23+
- uses: actions/checkout@v2
24+
- name: Use Node.js ${{ matrix.node-version }}
25+
uses: actions/setup-node@v1
26+
with:
27+
node-version: ${{ matrix.node-version }}
28+
- run: npm ci
29+
- run: npm run build --if-present
30+
- run: npm test

‎Makefile

-16
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,10 @@
11
# These flags depend on the system and may be overridden
2-
3-
ifeq ($(WASI_SDK_PATH),)
42
WASM_CC := ../wasi-sdk-12.0/bin/clang
53
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-
114
WASM_LDFLAGS := -nostartfiles
125

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

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

‎build.js

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

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

86
try { fs.mkdirSync('./dist'); }
97
catch (e) {}
108

119
const wasmBuffer = fs.readFileSync('./lib/lexer.wasm');
10+
const jsSource = fs.readFileSync('./src/lexer.js').toString();
1211
const pjson = JSON.parse(fs.readFileSync('./package.json').toString());
1312

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-
})
13+
const jsSourceProcessed = jsSource.replace('WASM_BINARY', wasmBuffer.toString('base64'));
3214

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')));
15+
const minified = MINIFY && terser.minify(jsSourceProcessed, {
16+
module: true,
17+
output: {
18+
preamble: `/* cjs-module-lexer ${pjson.version} */`
4119
}
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-
}
20+
});
5221

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-
}
22+
if (minified.error)
23+
throw minified.error;
7324

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

‎package-lock.json

+748-316
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+7-4
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,21 @@
1616
"test-wasm": "cross-env WASM=1 mocha -b -u tdd test/*.js",
1717
"test": "npm run test-wasm && npm run test-js",
1818
"bench": "node --expose-gc bench/index.mjs",
19-
"build": "node build.js",
19+
"build": "node build.js ; babel dist/lexer.mjs -o dist/lexer.js ; terser dist/lexer.js -o dist/lexer.js",
2020
"build-wasm": "make lib/lexer.wasm && node build.js",
2121
"prepublishOnly": "make && npm run build",
2222
"footprint": "npm run build && cat dist/lexer.js | gzip -9f | wc -c"
2323
},
2424
"author": "Guy Bedford",
2525
"license": "MIT",
2626
"devDependencies": {
27+
"@babel/cli": "^7.5.5",
28+
"@babel/core": "^7.5.5",
29+
"@babel/plugin-transform-modules-commonjs": "^7.5.0",
2730
"cross-env": "^7.0.3",
28-
"esbuild": "^0.19.12",
2931
"kleur": "^2.0.2",
30-
"mocha": "^9.1.3"
32+
"mocha": "^9.1.3",
33+
"terser": "^4.1.4"
3134
},
3235
"files": [
3336
"dist",
@@ -41,4 +44,4 @@
4144
"url": "https://github.com/nodejs/cjs-module-lexer/issues"
4245
},
4346
"homepage": "https://github.com/nodejs/cjs-module-lexer#readme"
44-
}
47+
}

‎src/lexer.js

+2-17
Original file line numberDiff line numberDiff line change
@@ -90,29 +90,14 @@ function copyLE (src, outBuf16) {
9090
outBuf16[i] = src.charCodeAt(i++);
9191
}
9292

93-
const loadWasm = (typeof EXTERNAL_PATH === "string" && (async () => {
94-
return (await import("node:fs/promises"))
95-
.readFile(
96-
(await import("node:url")).fileURLToPath(
97-
import.meta.resolve("../lib/lexer.wasm")
98-
)
99-
);
100-
})) || (async () => {
101-
const binary = WASM_BINARY
102-
if (typeof window !== "undefined" && typeof atob === "function") {
103-
return Uint8Array.from(atob(binary), (x) => x.charCodeAt(0));
104-
} else {
105-
return Buffer.from(binary, "base64");
106-
}
107-
});
108-
10993
let initPromise;
11094
export function init () {
11195
if (initPromise)
11296
return initPromise;
11397
return initPromise = (async () => {
11498
const compiled = await WebAssembly.compile(
115-
await loadWasm()
99+
(binary => typeof window !== 'undefined' && typeof atob === 'function' ? Uint8Array.from(atob(binary), x => x.charCodeAt(0)) : Buffer.from(binary, 'base64'))
100+
('WASM_BINARY')
116101
)
117102
const { exports } = await WebAssembly.instantiate(compiled);
118103
wasm = exports;

0 commit comments

Comments
 (0)
Please sign in to comment.