|
| 1 | +/** |
| 2 | + * esbuild configuration for building shadow npm inject script. |
| 3 | + * |
| 4 | + * This builds the inject script that is loaded via --require flag |
| 5 | + * when spawning npm with shadow arborist. |
| 6 | + */ |
| 7 | + |
| 8 | +import { writeFileSync } from 'node:fs' |
| 9 | +import path from 'node:path' |
| 10 | +import { fileURLToPath } from 'node:url' |
| 11 | + |
| 12 | +import { build } from 'esbuild' |
| 13 | + |
| 14 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)) |
| 15 | +const rootPath = path.join(__dirname, '..') |
| 16 | + |
| 17 | +const config = { |
| 18 | + entryPoints: [path.join(rootPath, 'src/shadow/npm/inject.mts')], |
| 19 | + bundle: true, |
| 20 | + outfile: path.join(rootPath, 'dist/shadow-npm-inject.js'), |
| 21 | + // Target Node.js environment (not browser). |
| 22 | + platform: 'node', |
| 23 | + // Target Node.js 18+ features. |
| 24 | + target: 'node18', |
| 25 | + format: 'cjs', |
| 26 | + |
| 27 | + // With platform: 'node', esbuild automatically externalizes all Node.js built-ins. |
| 28 | + external: [ |
| 29 | + 'node-gyp', // Required for require.resolve('node-gyp/package.json') |
| 30 | + ], |
| 31 | + |
| 32 | + // Suppress warnings for intentional CommonJS compatibility code. |
| 33 | + logOverride: { |
| 34 | + 'commonjs-variable-in-esm': 'silent', |
| 35 | + 'empty-import-meta': 'silent', |
| 36 | + 'require-resolve-not-external': 'silent', |
| 37 | + }, |
| 38 | + |
| 39 | + // Source maps off for production. |
| 40 | + sourcemap: false, |
| 41 | + |
| 42 | + // Don't minify (keep readable for debugging). |
| 43 | + minify: false, |
| 44 | + |
| 45 | + // Keep names for better stack traces. |
| 46 | + keepNames: true, |
| 47 | + |
| 48 | + // Write directly to disk. |
| 49 | + write: true, |
| 50 | + |
| 51 | + // Define environment variables and import.meta. |
| 52 | + define: { |
| 53 | + 'process.env.NODE_ENV': '"production"', |
| 54 | + 'import.meta.url': '__importMetaUrl', |
| 55 | + }, |
| 56 | + |
| 57 | + // Inject import.meta.url polyfill for CJS. |
| 58 | + inject: [path.join(__dirname, 'esbuild-inject-import-meta.mjs')], |
| 59 | +} |
| 60 | + |
| 61 | +// Run build if invoked directly. |
| 62 | +if (fileURLToPath(import.meta.url) === process.argv[1]) { |
| 63 | + build(config) |
| 64 | + .then(() => { |
| 65 | + console.log('Built shadow-npm-inject.js') |
| 66 | + }) |
| 67 | + .catch(error => { |
| 68 | + console.error('Build failed:', error) |
| 69 | + process.exitCode = 1 |
| 70 | + }) |
| 71 | +} |
| 72 | + |
| 73 | +export default config |
0 commit comments