Skip to content

Commit

Permalink
#2081: avoid "Function.toString" in "esbuild-wasm"
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Mar 5, 2022
1 parent 536ebd3 commit 151499a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
6 changes: 3 additions & 3 deletions lib/npm/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import * as common from "../shared/common"
import * as ourselves from "./browser"

declare const ESBUILD_VERSION: string;
declare let WEB_WORKER_SOURCE_CODE: string
declare let WEB_WORKER_FUNCTION: (postMessage: (data: Uint8Array) => void) => (event: { data: Uint8Array | ArrayBuffer }) => void
let webWorkerFunction = WEB_WORKER_FUNCTION

export let version = ESBUILD_VERSION;

Expand Down Expand Up @@ -83,11 +83,11 @@ const startRunningService = async (wasmURL: string, useWorker: boolean): Promise

if (useWorker) {
// Run esbuild off the main thread
let blob = new Blob([`onmessage=(${webWorkerFunction})(postMessage)`], { type: 'text/javascript' })
let blob = new Blob([`onmessage=${WEB_WORKER_SOURCE_CODE}(postMessage)`], { type: 'text/javascript' })
worker = new Worker(URL.createObjectURL(blob))
} else {
// Run esbuild on the main thread
let onmessage = webWorkerFunction((data: Uint8Array) => worker.onmessage!({ data }))
let onmessage = WEB_WORKER_FUNCTION((data: Uint8Array) => worker.onmessage!({ data }))
worker = {
onmessage: null,
postMessage: data => onmessage({ data }),
Expand Down
6 changes: 4 additions & 2 deletions scripts/esbuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ exports.buildWasmLib = async (esbuildPath) => {
const firstNonComment = commentLines.findIndex(line => !line.startsWith('//'))
const commentPrefix = '\n' + commentLines.slice(0, firstNonComment).join('\n') + '\n'
wasmWorkerCode[format] = minify
? `postMessage=>{${commentPrefix}${wasmExecAndWorker}}`
: `(postMessage) => {${(commentPrefix + wasmExecAndWorker).replace(/\n/g, '\n ')}\n}`
? `(postMessage=>{${commentPrefix}${wasmExecAndWorker}})`
: `((postMessage) => {${(commentPrefix + wasmExecAndWorker).replace(/\n/g, '\n ')}\n })`
}

// Generate "npm/esbuild-wasm/lib/browser.*"
Expand All @@ -201,6 +201,7 @@ exports.buildWasmLib = async (esbuildPath) => {
'--target=' + umdBrowserTarget,
'--format=cjs',
'--define:ESBUILD_VERSION=' + JSON.stringify(version),
'--define:WEB_WORKER_SOURCE_CODE=' + JSON.stringify(wasmWorkerCode.umd),
'--banner:js=' + umdPrefix,
'--footer:js=' + umdSuffix,
'--log-level=warning',
Expand All @@ -214,6 +215,7 @@ exports.buildWasmLib = async (esbuildPath) => {
'--target=' + esmBrowserTarget,
'--format=esm',
'--define:ESBUILD_VERSION=' + JSON.stringify(version),
'--define:WEB_WORKER_SOURCE_CODE=' + JSON.stringify(wasmWorkerCode.esm),
'--log-level=warning',
].concat(minifyFlags), { cwd: repoDir }).toString().replace('WEB_WORKER_FUNCTION', wasmWorkerCode.esm)
fs.writeFileSync(path.join(esmDir, minify ? 'browser.min.js' : 'browser.js'), browserESM)
Expand Down

0 comments on commit 151499a

Please sign in to comment.