Skip to content

Commit

Permalink
Add ESM build for browsers (#342)
Browse files Browse the repository at this point in the history
  • Loading branch information
calebeby authored Oct 18, 2020
1 parent 40c1e14 commit cfaedae
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ import * as common from "./common"

declare let WEB_WORKER_SOURCE_CODE: string

let build: typeof types.build = options => {
export const build: typeof types.build = options => {
throw new Error(`The "build" API only works in node`);
};

let transform: typeof types.transform = (input, options) => {
export const transform: typeof types.transform = (input, options) => {
throw new Error(`The "transform" API only works in node`);
};

let buildSync: typeof types.buildSync = options => {
export const buildSync: typeof types.buildSync = options => {
throw new Error(`The "buildSync" API only works in node`);
};

let transformSync: typeof types.transformSync = (input, options) => {
export const transformSync: typeof types.transformSync = (input, options) => {
throw new Error(`The "transformSync" API only works in node`);
};

let startService: typeof types.startService = options => {
export const startService: typeof types.startService = options => {
if (!options) throw new Error('Must provide an options object to "startService"');
if (!options.wasmURL) throw new Error('Must provide the "wasmURL" option');
return fetch(options.wasmURL).then(r => r.arrayBuffer()).then(wasm => {
Expand Down Expand Up @@ -87,4 +87,4 @@ let api: typeof types = {
startService,
};

module.exports = api;
export default api
11 changes: 11 additions & 0 deletions npm/esbuild-wasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@
},
"main": "lib/main.js",
"browser": "lib/browser.js",
"module": "lib/browser.mjs",
"exports": {
".": {
"browser": {
"module": "./lib/browser.mjs",
"script": "./lib/browser.js"
},
"node": "./lib/main.js"
},
"./": "./"
},
"types": "lib/main.d.ts",
"directories": {
"bin": "bin"
Expand Down
10 changes: 10 additions & 0 deletions scripts/esbuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ function buildWasmLib(esbuildPath) {
'--define:WEB_WORKER_SOURCE_CODE=' + JSON.stringify(wasmExecMinCode + workerMinCode),
], { cwd: repoDir }).toString()
fs.writeFileSync(path.join(libDir, 'browser.js'), umdPrefix + browserJs.trim() + umdSuffix)

// Generate "npm/esbuild-wasm/browser.mjs"
const browserMjs = childProcess.execFileSync(esbuildPath, [
path.join(repoDir, 'lib', 'browser.ts'),
'--bundle',
'--target=es2020',
'--format=esm',
'--define:WEB_WORKER_SOURCE_CODE=' + JSON.stringify(wasmExecMinCode + workerMinCode),
], { cwd: repoDir }).toString()
fs.writeFileSync(path.join(libDir, 'browser.mjs'), browserMjs.trim())
}

exports.buildBinary = () => {
Expand Down

0 comments on commit cfaedae

Please sign in to comment.