diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index ee224befd45..696d91b48f6 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -274,6 +274,7 @@ namespace ts { function writeBundle(bundle: Bundle, output: EmitTextWriter) { const previousWriter = writer; setWriter(output); + emitShebangIfNeeded(bundle); emitHelpersIndirect(bundle); for (const sourceFile of bundle.sourceFiles) { print(EmitHint.SourceFile, sourceFile, sourceFile); @@ -285,6 +286,7 @@ namespace ts { function writeFile(sourceFile: SourceFile, output: EmitTextWriter) { const previousWriter = writer; setWriter(output); + emitShebangIfNeeded(sourceFile); print(EmitHint.SourceFile, sourceFile, sourceFile); reset(); writer = previousWriter; @@ -2051,7 +2053,6 @@ namespace ts { function emitSourceFile(node: SourceFile) { writeLine(); - emitShebang(); emitBodyIndirect(node, node.statements, emitSourceFileWorker); } @@ -2095,11 +2096,26 @@ namespace ts { // Helpers // - function emitShebang() { - const shebang = getShebang(currentSourceFile.text); - if (shebang) { - write(shebang); - writeLine(); + function emitShebangIfNeeded(sourceFileOrBundle: Bundle | SourceFile) { + if (sourceFileOrBundle.kind === SyntaxKind.SourceFile) { + emitShebangInSourceFile(sourceFileOrBundle as SourceFile); + } + else { + for (const sourceFile of (sourceFileOrBundle as Bundle).sourceFiles) { + // Emit only the first Shebang with encounter + if (emitShebangInSourceFile(sourceFile)) { + break; + } + } + } + + function emitShebangInSourceFile(sourceFile: SourceFile): boolean { + const shebang = getShebang(sourceFile.text); + if (shebang) { + write(shebang); + writeLine(); + return true; + } } } diff --git a/tests/baselines/reference/emitBundleWithShebang1.js b/tests/baselines/reference/emitBundleWithShebang1.js index a034d59f2a8..1c7b2140eb5 100644 --- a/tests/baselines/reference/emitBundleWithShebang1.js +++ b/tests/baselines/reference/emitBundleWithShebang1.js @@ -4,6 +4,7 @@ class Doo {} class Scooby extends Doo {} //// [outFile.js] +#!/usr/bin/env gjs var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || @@ -14,7 +15,6 @@ var __extends = (this && this.__extends) || (function () { d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); -#!/usr/bin/env gjs var Doo = (function () { function Doo() { } diff --git a/tests/baselines/reference/emitBundleWithShebang2.js b/tests/baselines/reference/emitBundleWithShebang2.js index f771e52b922..b5d3d34641a 100644 --- a/tests/baselines/reference/emitBundleWithShebang2.js +++ b/tests/baselines/reference/emitBundleWithShebang2.js @@ -11,6 +11,7 @@ class Dood {} class Scoobyd extends Dood {} //// [outFile.js] +#!/usr/bin/env gjs var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || @@ -21,7 +22,6 @@ var __extends = (this && this.__extends) || (function () { d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); -#!/usr/bin/env gjs var Doo = (function () { function Doo() { } @@ -34,7 +34,6 @@ var Scooby = (function (_super) { } return Scooby; }(Doo)); -#!/usr/bin/env js var Dood = (function () { function Dood() { }