Skip to content

Commit

Permalink
fix #926: use "--target=node10" for node code in npm
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Mar 6, 2021
1 parent 8ca9f46 commit 59e6b64
Showing 1 changed file with 31 additions and 23 deletions.
54 changes: 31 additions & 23 deletions scripts/esbuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ const os = require('os')
const repoDir = path.dirname(__dirname)
const npmDir = path.join(repoDir, 'npm', 'esbuild')
const version = fs.readFileSync(path.join(repoDir, 'version.txt'), 'utf8').trim()
const nodeTarget = 'node10'; // See: https://nodejs.org/en/about/releases/
const umdBrowserTarget = 'es2015'; // Transpiles "async"
const esmBrowserTarget = 'es2017'; // Preserves "async"

exports.buildNativeLib = (esbuildPath) => {
const libDir = path.join(npmDir, 'lib')
Expand All @@ -17,7 +20,7 @@ exports.buildNativeLib = (esbuildPath) => {
childProcess.execFileSync(esbuildPath, [
path.join(repoDir, 'lib', 'install.ts'),
'--outfile=' + path.join(npmDir, 'install.js'),
'--target=es2015',
'--target=' + nodeTarget,
'--define:ESBUILD_VERSION=' + JSON.stringify(version),
'--platform=node',
], { cwd: repoDir })
Expand All @@ -27,7 +30,7 @@ exports.buildNativeLib = (esbuildPath) => {
path.join(repoDir, 'lib', 'node.ts'),
'--outfile=' + path.join(libDir, 'main.js'),
'--bundle',
'--target=es2015',
'--target=' + nodeTarget,
'--format=cjs',
'--define:WASM=false',
'--define:ESBUILD_VERSION=' + JSON.stringify(version),
Expand Down Expand Up @@ -96,7 +99,7 @@ exports.buildWasmLib = async (esbuildPath) => {
path.join(repoDir, 'lib', 'node.ts'),
'--outfile=' + path.join(libDir, 'main.js'),
'--bundle',
'--target=es2015',
'--target=' + nodeTarget,
'--format=cjs',
'--define:WASM=true',
'--define:ESBUILD_VERSION=' + JSON.stringify(version),
Expand All @@ -111,35 +114,40 @@ exports.buildWasmLib = async (esbuildPath) => {

for (const minify of [false, true]) {
const minifyFlags = minify ? ['--minify'] : []
let wasmWorkerCode = {}

// Process "npm/esbuild-wasm/wasm_exec.js"
let wasmExecCode = wasm_exec_js;
if (minify) {
const wasmExecMin = childProcess.execFileSync(esbuildPath, [
'--target=es2015',
].concat(minifyFlags), { cwd: repoDir, input: wasmExecCode }).toString()
const commentLines = wasmExecCode.split('\n')
const firstNonComment = commentLines.findIndex(line => !line.startsWith('//'))
wasmExecCode = '\n' + commentLines.slice(0, firstNonComment).concat(wasmExecMin).join('\n')
}
for (const [format, target] of Object.entries({ umd: umdBrowserTarget, esm: esmBrowserTarget })) {
// Process "npm/esbuild-wasm/wasm_exec.js"
let wasmExecCode = wasm_exec_js;
if (minify) {
const wasmExecMin = childProcess.execFileSync(esbuildPath, [
'--target=' + target,
].concat(minifyFlags), { cwd: repoDir, input: wasmExecCode }).toString()
const commentLines = wasmExecCode.split('\n')
const firstNonComment = commentLines.findIndex(line => !line.startsWith('//'))
wasmExecCode = '\n' + commentLines.slice(0, firstNonComment).concat(wasmExecMin).join('\n')
}

// Process "lib/worker.ts"
const workerCode = childProcess.execFileSync(esbuildPath, [
path.join(repoDir, 'lib', 'worker.ts'),
'--target=es2015',
'--define:ESBUILD_VERSION=' + JSON.stringify(version),
].concat(minifyFlags), { cwd: repoDir }).toString().trim()
// Process "lib/worker.ts"
const workerCode = childProcess.execFileSync(esbuildPath, [
path.join(repoDir, 'lib', 'worker.ts'),
'--target=' + target,
'--define:ESBUILD_VERSION=' + JSON.stringify(version),
].concat(minifyFlags), { cwd: repoDir }).toString().trim()

wasmWorkerCode[format] = wasmExecCode + workerCode
}

// Generate "npm/esbuild-wasm/lib/browser.*"
const umdPrefix = `(exports=>{`
const umdSuffix = `})(typeof exports==="object"?exports:(typeof self!=="undefined"?self:this).esbuild={});`
const browserCJS = childProcess.execFileSync(esbuildPath, [
path.join(repoDir, 'lib', 'browser.ts'),
'--bundle',
'--target=es2015',
'--target=' + umdBrowserTarget,
'--format=cjs',
'--define:ESBUILD_VERSION=' + JSON.stringify(version),
'--define:WEB_WORKER_SOURCE_CODE=' + JSON.stringify(wasmExecCode + workerCode),
'--define:WEB_WORKER_SOURCE_CODE=' + JSON.stringify(wasmWorkerCode.umd),
'--banner=' + umdPrefix,
'--footer=' + umdSuffix,
].concat(minifyFlags), { cwd: repoDir }).toString()
Expand All @@ -149,10 +157,10 @@ exports.buildWasmLib = async (esbuildPath) => {
const browserESM = childProcess.execFileSync(esbuildPath, [
path.join(repoDir, 'lib', 'browser.ts'),
'--bundle',
'--target=es2017',
'--target=' + esmBrowserTarget,
'--format=esm',
'--define:ESBUILD_VERSION=' + JSON.stringify(version),
'--define:WEB_WORKER_SOURCE_CODE=' + JSON.stringify(wasmExecCode + workerCode),
'--define:WEB_WORKER_SOURCE_CODE=' + JSON.stringify(wasmWorkerCode.esm),
].concat(minifyFlags), { cwd: repoDir }).toString()
fs.writeFileSync(path.join(esmDir, minify ? 'browser.min.js' : 'browser.js'), browserESM)
}
Expand Down

0 comments on commit 59e6b64

Please sign in to comment.