Skip to content

Commit 341a049

Browse files
committed
fix: code review
1 parent 6e9087a commit 341a049

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

packages/esbuild-plugin-lit-css/esbuild-plugin-lit-css.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,15 @@ export function litCssPlugin(options?: LitCSSOptions): Plugin {
195195
Promise.resolve({ contents, changed: false as boolean }),
196196
));
197197

198-
// inject tag import at the top if not already present
199-
if (anyRewritten && !alreadyImported)
200-
contents = `import {${tag}} from '${specifier}';\n${contents}`;
198+
// inject tag import at the top, preserving any leading hashbang
199+
if (anyRewritten && !alreadyImported) {
200+
const tagImport = `import {${tag}} from '${specifier}';\n`;
201+
const hashbangMatch = contents.match(/^#![^\n]*\n/);
202+
if (hashbangMatch)
203+
contents = hashbangMatch[0] + tagImport + contents.slice(hashbangMatch[0].length);
204+
else
205+
contents = tagImport + contents;
206+
}
201207

202208
const ext = extname(args.path);
203209
const loader = LOADER_MAP[ext] ?? 'js';

0 commit comments

Comments
 (0)