Skip to content

Commit

Permalink
fix(commonjs): Keep the shebang at the top of the file content
Browse files Browse the repository at this point in the history
  • Loading branch information
TrickyPi committed Oct 13, 2023
1 parent 1caee89 commit c8bbe4f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 6 deletions.
11 changes: 10 additions & 1 deletion packages/commonjs/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,16 @@ export default function commonjs(options = {}) {
// entry suffix is just appended to not mess up relative external resolution
if (id.endsWith(ENTRY_SUFFIX)) {
const acutalId = id.slice(0, -ENTRY_SUFFIX.length);
return getEntryProxy(acutalId, getDefaultIsModuleExports(acutalId), this.getModuleInfo);
const {
meta: { commonjs: commonjsMeta }
} = this.getModuleInfo(acutalId);
const shebang = commonjsMeta?.shebang || '';
return getEntryProxy(
acutalId,
getDefaultIsModuleExports(acutalId),
this.getModuleInfo,
shebang
);
}

if (isWrappedId(id, ES_IMPORT_SUFFIX)) {
Expand Down
10 changes: 7 additions & 3 deletions packages/commonjs/src/proxies.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export async function getStaticRequireProxy(id, requireReturnsDefault, loadModul
return `export { default } from ${JSON.stringify(id)};`;
}

export function getEntryProxy(id, defaultIsModuleExports, getModuleInfo) {
export function getEntryProxy(id, defaultIsModuleExports, getModuleInfo, shebang) {
const {
meta: { commonjs: commonjsMeta },
hasDefaultExport
Expand All @@ -55,9 +55,13 @@ export function getEntryProxy(id, defaultIsModuleExports, getModuleInfo) {
if (hasDefaultExport) {
code += `export { default } from ${stringifiedId};`;
}
return code;
return shebang + code;
}
return getEsImportProxy(id, defaultIsModuleExports);
const result = getEsImportProxy(id, defaultIsModuleExports);
return {
...result,
code: shebang + result.code
};
}

export function getEsImportProxy(id, defaultIsModuleExports) {
Expand Down
11 changes: 9 additions & 2 deletions packages/commonjs/src/transform-commonjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,13 @@ export default async function transformCommonjs(
magicString.remove(0, commentEnd).trim();
}

let shebang = '';
if (code.startsWith('#!')) {
const shebangEndPosition = code.indexOf('\n') + 1;
shebang = code.slice(0, shebangEndPosition);
magicString.remove(0, shebangEndPosition).trim();
}

const exportMode = isEsModule
? 'none'
: shouldWrap
Expand Down Expand Up @@ -561,13 +568,13 @@ function ${requireName} () {

magicString
.trim()
.prepend(leadingComment + importBlock)
.prepend(shebang + leadingComment + importBlock)
.append(exportBlock);

return {
code: magicString.toString(),
map: sourceMap ? magicString.generateMap() : null,
syntheticNamedExports: isEsModule || usesRequireWrapper ? false : '__moduleExports',
meta: { commonjs: commonjsMeta }
meta: { commonjs: { ...commonjsMeta, shebang } }
};
}
2 changes: 2 additions & 0 deletions packages/commonjs/test/fixtures/samples/shebang/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
module.exports = 1;
15 changes: 15 additions & 0 deletions packages/commonjs/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1291,3 +1291,18 @@ test('allows the config to be reused', async (t) => {
['bar.js']
);
});

test('preserve shebang', async (t) => {
const bundle = await rollup({
input: ['fixtures/samples/shebang/main.js'],
plugins: [commonjs()]
});

const { output } = await bundle.generate({
exports: 'auto',
format: 'cjs',
chunkFileNames: '[name].js'
});
console.log(output[0]);

Check warning on line 1306 in packages/commonjs/test/test.js

View workflow job for this annotation

GitHub Actions / Node v20

Unexpected console statement

Check warning on line 1306 in packages/commonjs/test/test.js

View workflow job for this annotation

GitHub Actions / Node v18

Unexpected console statement
t.is(output[0].code.startsWith('#!'), true);
});

0 comments on commit c8bbe4f

Please sign in to comment.