Skip to content

Commit 7057b55

Browse files
committed
fix: insert hashbang above createRequire
fixes #111
1 parent 6a93e79 commit 7057b55

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/rollup/configs/pkg.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@ export const getPkgConfig = (
7272
dynamicImportVars({
7373
warnOnError: true,
7474
}),
75+
esmInjectCreateRequire(),
7576
...(
7677
options.minify
7778
? [esbuildMinify(esbuildConfig)]
7879
: []
7980
),
8081
patchBinary(executablePaths),
81-
esmInjectCreateRequire(),
8282
],
8383
output: [] as unknown as Output,
8484
external: [] as (string | RegExp)[],

tests/specs/builds/bin.ts

+20
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,25 @@ export default testSuite(({ describe }, nodePath: string) => {
5959
expect(await fixture.exists('dist/index.mjs')).toBe(true);
6060
expect(await fixture.exists('dist/index.js')).toBe(true);
6161
});
62+
63+
test('hashbang gets inserted at the top (despite other injections e.g. createRequire)', async () => {
64+
await using fixture = await createFixture({
65+
'src/dynamic-require.ts': 'require((() => \'fs\')());',
66+
'package.json': createPackageJson({
67+
bin: './dist/dynamic-require.mjs',
68+
}),
69+
});
70+
71+
const pkgrollProcess = await pkgroll([], {
72+
cwd: fixture.path,
73+
nodePath,
74+
});
75+
76+
expect(pkgrollProcess.exitCode).toBe(0);
77+
expect(pkgrollProcess.stderr).toBe('');
78+
79+
const content = await fixture.readFile('dist/dynamic-require.mjs', 'utf8');
80+
expect(content.startsWith('#!/usr/bin/env node')).toBeTruthy();
81+
});
6282
});
6383
});

0 commit comments

Comments
 (0)