diff --git a/lib/npm/browser.ts b/lib/npm/browser.ts index 0196f3329bb..8dbdc24b1ef 100644 --- a/lib/npm/browser.ts +++ b/lib/npm/browser.ts @@ -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; @@ -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 }), diff --git a/scripts/esbuild.js b/scripts/esbuild.js index 56e8650b49c..e6287d194e3 100644 --- a/scripts/esbuild.js +++ b/scripts/esbuild.js @@ -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.*" @@ -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', @@ -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)