Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(commonjs): Keep the shebang at the top of the file content #1610

Merged
merged 1 commit into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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('keep the shebang at the top of the file content', 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'
});

t.is(output[0].code.startsWith('#!/usr/bin/env node\n'), true);
});
Loading