Skip to content

Commit

Permalink
feat: support CJS output for wasm build (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirjavascript authored Jan 15, 2024
1 parent a67d5a7 commit 21eea6f
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ All of the APIs in `@parcel/watcher` support the following options, which are pa

## WASM

The `@parcel/watcher-wasm` package can be used in place of `@parcel/watcher` on unsupported platforms. It relies on the Node `fs` module, so in non-Node environments such as browsers, an `fs` polyfill will be needed. The `@parcel/watcher-wasm` package is published as an ESM-only module.
The `@parcel/watcher-wasm` package can be used in place of `@parcel/watcher` on unsupported platforms. It relies on the Node `fs` module, so in non-Node environments such as browsers, an `fs` polyfill will be needed.

**Note**: the WASM implementation is significantly less efficient than the native implementations because it must crawl the file system to watch each directory individually. Use the native `@parcel/watcher` package wherever possible.

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"node-addon-api": "^7.0.0"
},
"devDependencies": {
"esbuild": "^0.19.8",
"fs-extra": "^10.0.0",
"husky": "^7.0.2",
"lint-staged": "^11.1.2",
Expand Down
25 changes: 21 additions & 4 deletions scripts/build-wasm.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const esbuild = require('esbuild');
const exec = require('child_process').execSync;
const fs = require('fs');
const pkg = require('../package.json');
Expand All @@ -15,7 +16,6 @@ export default function init(input?: string | URL | Request): Promise<void>;
fs.writeFileSync(`${dir}/npm/wasm/index.d.ts`, dts);

let readme = fs.readFileSync(`${dir}/README.md`, 'utf8');
readme = readme.replace('# ⚡️ Lightning CSS', '# ⚡️ lightningcss-wasm');
fs.writeFileSync(`${dir}/npm/wasm/README.md`, readme);

let js = fs.readFileSync(`${dir}/wasm/index.mjs`, 'utf8');
Expand All @@ -27,20 +27,37 @@ fs.copyFileSync(`${dir}/wrapper.js`, `${dir}/npm/wasm/wrapper.js`);
fs.copyFileSync(`${dir}/wasm/watcher.wasm`, `${dir}/npm/wasm/watcher.wasm`);
fs.cpSync(`${dir}/node_modules/napi-wasm`, `${dir}/npm/wasm/node_modules/napi-wasm`, {recursive: true});

let wasmPkg = { ...pkg };
const cjsBuild = {
entryPoints: [`${dir}/npm/wasm/index.mjs`],
bundle: true,
format: 'cjs',
platform: 'node',
packages: 'external',
outdir: `${dir}/npm/wasm`,
outExtension: { '.js': '.cjs' },
inject: [`${dir}/wasm/import.meta.url-polyfill.js`],
define: { 'import.meta.url': 'import_meta_url' },
};
esbuild.build(cjsBuild).catch(console.error);

const wasmPkg = { ...pkg };
wasmPkg.name = '@parcel/watcher-wasm';
wasmPkg.main = 'index.mjs';
wasmPkg.module = 'index.mjs';
wasmPkg.types = 'index.d.ts';
wasmPkg.sideEffects = false;
wasmPkg.files = ['*.js', '*.mjs', '*.d.ts', '*.wasm'];
wasmPkg.files = ['*.js', '*.cjs', '*.mjs', '*.d.ts', '*.wasm'];
wasmPkg.dependencies = {
'napi-wasm': pkg.devDependencies['napi-wasm'],
'is-glob': pkg.dependencies['is-glob'],
'micromatch': pkg.dependencies['micromatch']
};
wasmPkg.exports = {
types: './index.d.ts',
import: './index.mjs',
require: './index.cjs'
};
wasmPkg.bundledDependencies = ['napi-wasm']; // for stackblitz
delete wasmPkg.exports;
delete wasmPkg.binary;
delete wasmPkg['lint-staged'];
delete wasmPkg.husky;
Expand Down
6 changes: 6 additions & 0 deletions wasm/import.meta.url-polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* @see https://github.com/evanw/esbuild/issues/1633
*/
export const import_meta_url =
typeof document === 'undefined' ? new (require('url'.replace('', '')).URL)('file:' + __filename).href :
(document.currentScript && document.currentScript.src || new URL('main.js', document.baseURI).href)
138 changes: 138 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,116 @@
chalk "^2.0.0"
js-tokens "^4.0.0"

"@esbuild/android-arm64@0.19.8":
version "0.19.8"
resolved "https://registry.yarnpkg.com/@esbuild/android-arm64/-/android-arm64-0.19.8.tgz#fb7130103835b6d43ea499c3f30cfb2b2ed58456"
integrity sha512-B8JbS61bEunhfx8kasogFENgQfr/dIp+ggYXwTqdbMAgGDhRa3AaPpQMuQU0rNxDLECj6FhDzk1cF9WHMVwrtA==

"@esbuild/android-arm@0.19.8":
version "0.19.8"
resolved "https://registry.yarnpkg.com/@esbuild/android-arm/-/android-arm-0.19.8.tgz#b46e4d9e984e6d6db6c4224d72c86b7757e35bcb"
integrity sha512-31E2lxlGM1KEfivQl8Yf5aYU/mflz9g06H6S15ITUFQueMFtFjESRMoDSkvMo8thYvLBax+VKTPlpnx+sPicOA==

"@esbuild/android-x64@0.19.8":
version "0.19.8"
resolved "https://registry.yarnpkg.com/@esbuild/android-x64/-/android-x64-0.19.8.tgz#a13db9441b5a4f4e4fec4a6f8ffacfea07888db7"
integrity sha512-rdqqYfRIn4jWOp+lzQttYMa2Xar3OK9Yt2fhOhzFXqg0rVWEfSclJvZq5fZslnz6ypHvVf3CT7qyf0A5pM682A==

"@esbuild/darwin-arm64@0.19.8":
version "0.19.8"
resolved "https://registry.yarnpkg.com/@esbuild/darwin-arm64/-/darwin-arm64-0.19.8.tgz#49f5718d36541f40dd62bfdf84da9c65168a0fc2"
integrity sha512-RQw9DemMbIq35Bprbboyf8SmOr4UXsRVxJ97LgB55VKKeJOOdvsIPy0nFyF2l8U+h4PtBx/1kRf0BelOYCiQcw==

"@esbuild/darwin-x64@0.19.8":
version "0.19.8"
resolved "https://registry.yarnpkg.com/@esbuild/darwin-x64/-/darwin-x64-0.19.8.tgz#75c5c88371eea4bfc1f9ecfd0e75104c74a481ac"
integrity sha512-3sur80OT9YdeZwIVgERAysAbwncom7b4bCI2XKLjMfPymTud7e/oY4y+ci1XVp5TfQp/bppn7xLw1n/oSQY3/Q==

"@esbuild/freebsd-arm64@0.19.8":
version "0.19.8"
resolved "https://registry.yarnpkg.com/@esbuild/freebsd-arm64/-/freebsd-arm64-0.19.8.tgz#9d7259fea4fd2b5f7437b52b542816e89d7c8575"
integrity sha512-WAnPJSDattvS/XtPCTj1tPoTxERjcTpH6HsMr6ujTT+X6rylVe8ggxk8pVxzf5U1wh5sPODpawNicF5ta/9Tmw==

"@esbuild/freebsd-x64@0.19.8":
version "0.19.8"
resolved "https://registry.yarnpkg.com/@esbuild/freebsd-x64/-/freebsd-x64-0.19.8.tgz#abac03e1c4c7c75ee8add6d76ec592f46dbb39e3"
integrity sha512-ICvZyOplIjmmhjd6mxi+zxSdpPTKFfyPPQMQTK/w+8eNK6WV01AjIztJALDtwNNfFhfZLux0tZLC+U9nSyA5Zg==

"@esbuild/linux-arm64@0.19.8":
version "0.19.8"
resolved "https://registry.yarnpkg.com/@esbuild/linux-arm64/-/linux-arm64-0.19.8.tgz#c577932cf4feeaa43cb9cec27b89cbe0df7d9098"
integrity sha512-z1zMZivxDLHWnyGOctT9JP70h0beY54xDDDJt4VpTX+iwA77IFsE1vCXWmprajJGa+ZYSqkSbRQ4eyLCpCmiCQ==

"@esbuild/linux-arm@0.19.8":
version "0.19.8"
resolved "https://registry.yarnpkg.com/@esbuild/linux-arm/-/linux-arm-0.19.8.tgz#d6014d8b98b5cbc96b95dad3d14d75bb364fdc0f"
integrity sha512-H4vmI5PYqSvosPaTJuEppU9oz1dq2A7Mr2vyg5TF9Ga+3+MGgBdGzcyBP7qK9MrwFQZlvNyJrvz6GuCaj3OukQ==

"@esbuild/linux-ia32@0.19.8":
version "0.19.8"
resolved "https://registry.yarnpkg.com/@esbuild/linux-ia32/-/linux-ia32-0.19.8.tgz#2379a0554307d19ac4a6cdc15b08f0ea28e7a40d"
integrity sha512-1a8suQiFJmZz1khm/rDglOc8lavtzEMRo0v6WhPgxkrjcU0LkHj+TwBrALwoz/OtMExvsqbbMI0ChyelKabSvQ==

"@esbuild/linux-loong64@0.19.8":
version "0.19.8"
resolved "https://registry.yarnpkg.com/@esbuild/linux-loong64/-/linux-loong64-0.19.8.tgz#e2a5bbffe15748b49356a6cd7b2d5bf60c5a7123"
integrity sha512-fHZWS2JJxnXt1uYJsDv9+b60WCc2RlvVAy1F76qOLtXRO+H4mjt3Tr6MJ5l7Q78X8KgCFudnTuiQRBhULUyBKQ==

"@esbuild/linux-mips64el@0.19.8":
version "0.19.8"
resolved "https://registry.yarnpkg.com/@esbuild/linux-mips64el/-/linux-mips64el-0.19.8.tgz#1359331e6f6214f26f4b08db9b9df661c57cfa24"
integrity sha512-Wy/z0EL5qZYLX66dVnEg9riiwls5IYnziwuju2oUiuxVc+/edvqXa04qNtbrs0Ukatg5HEzqT94Zs7J207dN5Q==

"@esbuild/linux-ppc64@0.19.8":
version "0.19.8"
resolved "https://registry.yarnpkg.com/@esbuild/linux-ppc64/-/linux-ppc64-0.19.8.tgz#9ba436addc1646dc89dae48c62d3e951ffe70951"
integrity sha512-ETaW6245wK23YIEufhMQ3HSeHO7NgsLx8gygBVldRHKhOlD1oNeNy/P67mIh1zPn2Hr2HLieQrt6tWrVwuqrxg==

"@esbuild/linux-riscv64@0.19.8":
version "0.19.8"
resolved "https://registry.yarnpkg.com/@esbuild/linux-riscv64/-/linux-riscv64-0.19.8.tgz#fbcf0c3a0b20f40b5fc31c3b7695f0769f9de66b"
integrity sha512-T2DRQk55SgoleTP+DtPlMrxi/5r9AeFgkhkZ/B0ap99zmxtxdOixOMI570VjdRCs9pE4Wdkz7JYrsPvsl7eESg==

"@esbuild/linux-s390x@0.19.8":
version "0.19.8"
resolved "https://registry.yarnpkg.com/@esbuild/linux-s390x/-/linux-s390x-0.19.8.tgz#989e8a05f7792d139d5564ffa7ff898ac6f20a4a"
integrity sha512-NPxbdmmo3Bk7mbNeHmcCd7R7fptJaczPYBaELk6NcXxy7HLNyWwCyDJ/Xx+/YcNH7Im5dHdx9gZ5xIwyliQCbg==

"@esbuild/linux-x64@0.19.8":
version "0.19.8"
resolved "https://registry.yarnpkg.com/@esbuild/linux-x64/-/linux-x64-0.19.8.tgz#b187295393a59323397fe5ff51e769ec4e72212b"
integrity sha512-lytMAVOM3b1gPypL2TRmZ5rnXl7+6IIk8uB3eLsV1JwcizuolblXRrc5ShPrO9ls/b+RTp+E6gbsuLWHWi2zGg==

"@esbuild/netbsd-x64@0.19.8":
version "0.19.8"
resolved "https://registry.yarnpkg.com/@esbuild/netbsd-x64/-/netbsd-x64-0.19.8.tgz#c1ec0e24ea82313cb1c7bae176bd5acd5bde7137"
integrity sha512-hvWVo2VsXz/8NVt1UhLzxwAfo5sioj92uo0bCfLibB0xlOmimU/DeAEsQILlBQvkhrGjamP0/el5HU76HAitGw==

"@esbuild/openbsd-x64@0.19.8":
version "0.19.8"
resolved "https://registry.yarnpkg.com/@esbuild/openbsd-x64/-/openbsd-x64-0.19.8.tgz#0c5b696ac66c6d70cf9ee17073a581a28af9e18d"
integrity sha512-/7Y7u77rdvmGTxR83PgaSvSBJCC2L3Kb1M/+dmSIvRvQPXXCuC97QAwMugBNG0yGcbEGfFBH7ojPzAOxfGNkwQ==

"@esbuild/sunos-x64@0.19.8":
version "0.19.8"
resolved "https://registry.yarnpkg.com/@esbuild/sunos-x64/-/sunos-x64-0.19.8.tgz#2a697e1f77926ff09fcc457d8f29916d6cd48fb1"
integrity sha512-9Lc4s7Oi98GqFA4HzA/W2JHIYfnXbUYgekUP/Sm4BG9sfLjyv6GKKHKKVs83SMicBF2JwAX6A1PuOLMqpD001w==

"@esbuild/win32-arm64@0.19.8":
version "0.19.8"
resolved "https://registry.yarnpkg.com/@esbuild/win32-arm64/-/win32-arm64-0.19.8.tgz#ec029e62a2fca8c071842ecb1bc5c2dd20b066f1"
integrity sha512-rq6WzBGjSzihI9deW3fC2Gqiak68+b7qo5/3kmB6Gvbh/NYPA0sJhrnp7wgV4bNwjqM+R2AApXGxMO7ZoGhIJg==

"@esbuild/win32-ia32@0.19.8":
version "0.19.8"
resolved "https://registry.yarnpkg.com/@esbuild/win32-ia32/-/win32-ia32-0.19.8.tgz#cbb9a3146bde64dc15543e48afe418c7a3214851"
integrity sha512-AIAbverbg5jMvJznYiGhrd3sumfwWs8572mIJL5NQjJa06P8KfCPWZQ0NwZbPQnbQi9OWSZhFVSUWjjIrn4hSw==

"@esbuild/win32-x64@0.19.8":
version "0.19.8"
resolved "https://registry.yarnpkg.com/@esbuild/win32-x64/-/win32-x64-0.19.8.tgz#c8285183dbdb17008578dbacb6e22748709b4822"
integrity sha512-bfZ0cQ1uZs2PqpulNL5j/3w+GDhP36k1K5c38QdQg+Swy51jFZWWeIkteNsufkQxp986wnqRRsb/bHbY1WQ7TA==

"@types/color-name@^1.1.1":
version "1.1.1"
resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0"
Expand Down Expand Up @@ -351,6 +461,34 @@ error-ex@^1.3.1:
dependencies:
is-arrayish "^0.2.1"

esbuild@^0.19.8:
version "0.19.8"
resolved "https://registry.yarnpkg.com/esbuild/-/esbuild-0.19.8.tgz#ad05b72281d84483fa6b5345bd246c27a207b8f1"
integrity sha512-l7iffQpT2OrZfH2rXIp7/FkmaeZM0vxbxN9KfiCwGYuZqzMg/JdvX26R31Zxn/Pxvsrg3Y9N6XTcnknqDyyv4w==
optionalDependencies:
"@esbuild/android-arm" "0.19.8"
"@esbuild/android-arm64" "0.19.8"
"@esbuild/android-x64" "0.19.8"
"@esbuild/darwin-arm64" "0.19.8"
"@esbuild/darwin-x64" "0.19.8"
"@esbuild/freebsd-arm64" "0.19.8"
"@esbuild/freebsd-x64" "0.19.8"
"@esbuild/linux-arm" "0.19.8"
"@esbuild/linux-arm64" "0.19.8"
"@esbuild/linux-ia32" "0.19.8"
"@esbuild/linux-loong64" "0.19.8"
"@esbuild/linux-mips64el" "0.19.8"
"@esbuild/linux-ppc64" "0.19.8"
"@esbuild/linux-riscv64" "0.19.8"
"@esbuild/linux-s390x" "0.19.8"
"@esbuild/linux-x64" "0.19.8"
"@esbuild/netbsd-x64" "0.19.8"
"@esbuild/openbsd-x64" "0.19.8"
"@esbuild/sunos-x64" "0.19.8"
"@esbuild/win32-arm64" "0.19.8"
"@esbuild/win32-ia32" "0.19.8"
"@esbuild/win32-x64" "0.19.8"

escalade@^3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/escalade/-/escalade-3.1.1.tgz#d8cfdc7000965c5a0174b4a82eaa5c0552742e40"
Expand Down

0 comments on commit 21eea6f

Please sign in to comment.