Skip to content

Commit f4682e9

Browse files
committed
build: add esbuild target for shadow npm inject script
Creates dist/shadow-npm-inject.js from src/shadow/npm/inject.mts. This inject script is loaded via --require flag when spawning npm with shadow arborist.
1 parent 92cd77d commit f4682e9

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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

packages/cli/scripts/build.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ async function main() {
117117
command: 'node',
118118
args: ['.config/esbuild.index.config.mjs'],
119119
},
120+
{
121+
name: 'Build Shadow NPM Inject',
122+
command: 'node',
123+
args: ['.config/esbuild.inject.config.mjs'],
124+
},
120125
{
121126
name: 'Compress CLI',
122127
command: 'node',

0 commit comments

Comments
 (0)