From 1ee55274ba0468a68d53c85713a7944429e319f2 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Sun, 16 Oct 2022 06:43:11 +0200 Subject: [PATCH] Ignore hashhbang comments --- src/ast/nodes/Program.ts | 7 ++++++- test/form/samples/hashbang/_config.js | 3 +++ test/form/samples/hashbang/_expected.js | 3 +++ test/form/samples/hashbang/main.js | 3 +++ test/form/samples/hashbang/other.js | 2 ++ 5 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 test/form/samples/hashbang/_config.js create mode 100644 test/form/samples/hashbang/_expected.js create mode 100644 test/form/samples/hashbang/main.js create mode 100644 test/form/samples/hashbang/other.js diff --git a/src/ast/nodes/Program.ts b/src/ast/nodes/Program.ts index a14b7459473..d252875a4c6 100644 --- a/src/ast/nodes/Program.ts +++ b/src/ast/nodes/Program.ts @@ -32,8 +32,13 @@ export default class Program extends NodeBase { } render(code: MagicString, options: RenderOptions): void { + let start = this.start; + if (code.original.startsWith('#!')) { + start = Math.min(code.original.indexOf('\n') + 1, this.end); + code.remove(0, start); + } if (this.body.length > 0) { - renderStatementList(this.body, code, this.start, this.end, options); + renderStatementList(this.body, code, start, this.end, options); } else { super.render(code, options); } diff --git a/test/form/samples/hashbang/_config.js b/test/form/samples/hashbang/_config.js new file mode 100644 index 00000000000..f19d68bd304 --- /dev/null +++ b/test/form/samples/hashbang/_config.js @@ -0,0 +1,3 @@ +module.exports = { + description: 'supports input files with leading hashbang comment' +}; diff --git a/test/form/samples/hashbang/_expected.js b/test/form/samples/hashbang/_expected.js new file mode 100644 index 00000000000..180a2a59255 --- /dev/null +++ b/test/form/samples/hashbang/_expected.js @@ -0,0 +1,3 @@ +console.log('other'); + +console.log('main'); diff --git a/test/form/samples/hashbang/main.js b/test/form/samples/hashbang/main.js new file mode 100644 index 00000000000..243bdbbad4d --- /dev/null +++ b/test/form/samples/hashbang/main.js @@ -0,0 +1,3 @@ +#!/usr/bin/env node +import './other'; +console.log('main'); diff --git a/test/form/samples/hashbang/other.js b/test/form/samples/hashbang/other.js new file mode 100644 index 00000000000..b25867312ac --- /dev/null +++ b/test/form/samples/hashbang/other.js @@ -0,0 +1,2 @@ +#!/usr/bin/env node +console.log('other'); \ No newline at end of file