|
| 1 | +const esbuild = require('esbuild') |
| 2 | +const path = require('path') |
| 3 | +const fs = require('fs') |
| 4 | +const mri = require('mri') |
| 5 | + |
| 6 | +const resolve = (...args) => path.resolve(__dirname, ...args) |
| 7 | + |
| 8 | +const args = mri(process.argv.slice(2), { |
| 9 | + boolean: ['watch', 'minify'], |
| 10 | + string: ['outfile', 'outdir', 'external'], |
| 11 | +}) |
| 12 | + |
| 13 | +esbuild.build({ |
| 14 | + entryPoints: args._, |
| 15 | + bundle: true, |
| 16 | + platform: 'node', |
| 17 | + external: [].concat(args.external), |
| 18 | + format: 'cjs', |
| 19 | + outdir: args.outdir, |
| 20 | + outfile: args.outfile, |
| 21 | + watch: args.watch, |
| 22 | + minify: args.minify, |
| 23 | + plugins: [ |
| 24 | + { |
| 25 | + name: 'css', |
| 26 | + setup(build) { |
| 27 | + build.onResolve({ filter: /\.css$/, namespace: 'file' }, (args) => ({ |
| 28 | + path: require.resolve(args.path, { paths: [args.resolveDir] }), |
| 29 | + namespace: 'css', |
| 30 | + })) |
| 31 | + |
| 32 | + build.onLoad({ filter: /.*/, namespace: 'css' }, async (args) => ({ |
| 33 | + contents: ` |
| 34 | + export default ${JSON.stringify(await fs.promises.readFile(args.path, 'utf8'))} |
| 35 | + `, |
| 36 | + })) |
| 37 | + }, |
| 38 | + }, |
| 39 | + { |
| 40 | + // https://github.com/evanw/esbuild/issues/1051#issuecomment-806325487 |
| 41 | + name: 'native-node-modules', |
| 42 | + setup(build) { |
| 43 | + // If a ".node" file is imported within a module in the "file" namespace, resolve |
| 44 | + // it to an absolute path and put it into the "node-file" virtual namespace. |
| 45 | + build.onResolve({ filter: /\.node$/, namespace: 'file' }, (args) => ({ |
| 46 | + path: require.resolve(args.path, { paths: [args.resolveDir] }), |
| 47 | + namespace: 'node-file', |
| 48 | + })) |
| 49 | + |
| 50 | + // Files in the "node-file" virtual namespace call "require()" on the |
| 51 | + // path from esbuild of the ".node" file in the output directory. |
| 52 | + build.onLoad({ filter: /.*/, namespace: 'node-file' }, (args) => ({ |
| 53 | + contents: ` |
| 54 | + import path from ${JSON.stringify(args.path)} |
| 55 | + import { resolve } from 'path' |
| 56 | + module.exports = require(resolve(__dirname, path)) |
| 57 | + `, |
| 58 | + })) |
| 59 | + |
| 60 | + // If a ".node" file is imported within a module in the "node-file" namespace, put |
| 61 | + // it in the "file" namespace where esbuild's default loading behavior will handle |
| 62 | + // it. It is already an absolute path since we resolved it to one above. |
| 63 | + build.onResolve({ filter: /\.node$/, namespace: 'node-file' }, (args) => ({ |
| 64 | + path: args.path, |
| 65 | + namespace: 'file', |
| 66 | + })) |
| 67 | + |
| 68 | + // Tell esbuild's default loading behavior to use the "file" loader for |
| 69 | + // these ".node" files. |
| 70 | + let opts = build.initialOptions |
| 71 | + opts.loader = opts.loader || {} |
| 72 | + opts.loader['.node'] = 'file' |
| 73 | + }, |
| 74 | + }, |
| 75 | + ], |
| 76 | +}) |
0 commit comments