diff --git a/build/fetch.js b/build/fetch.js new file mode 100644 index 00000000000..a3ab88704f8 --- /dev/null +++ b/build/fetch.js @@ -0,0 +1,36 @@ +const esbuild = require('esbuild') +const fs = require('fs') +const path = require('path') + +// Copied from: https://github.com/evanw/esbuild/issues/859#issuecomment-829154955 +const nodeModules = /^(?:.*[\\/])?node_modules(?:[\\/].*)?$/ + +const dirnamePlugin = { + name: 'dirname', + setup (build) { + build.onLoad({ filter: /.*/ }, ({ path: filePath }) => { + if (!filePath.match(nodeModules)) { + let contents = fs.readFileSync(filePath, 'utf8') + const loader = path.extname(filePath).substring(1) + const dirname = path.dirname(filePath) + contents = contents + .replace('__dirname', `"${dirname}"`) + .replace('__filename', `"${filePath}"`) + return { + contents, + loader + } + } + }) + } +} + +esbuild.build({ + platform: 'node', + entryPoints: [path.resolve('index-fetch.js')], + bundle: true, + outfile: path.resolve('undici-fetch.js'), + plugins: [dirnamePlugin] +}).then(() => { + console.log('Build complete.') +}) diff --git a/package.json b/package.json index a780bc10376..ab4c976aab4 100644 --- a/package.json +++ b/package.json @@ -70,7 +70,7 @@ "docs" ], "scripts": { - "build:node": "npx esbuild@0.14.38 index-fetch.js --bundle --platform=node --outfile=undici-fetch.js", + "build:node": "node build/fetch.js", "prebuild:wasm": "node build/wasm.js --prebuild", "build:wasm": "node build/wasm.js --docker", "lint": "standard | snazzy", @@ -109,6 +109,7 @@ "delay": "^5.0.0", "dns-packet": "^5.4.0", "docsify-cli": "^4.4.3", + "esbuild": "^0.19.4", "form-data": "^4.0.0", "formdata-node": "^4.3.1", "https-pem": "^3.0.0",