Skip to content
Open
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
25 changes: 25 additions & 0 deletions packages/vite/src/node/__tests__/plugins/css.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -829,4 +829,29 @@ exports.foo = foo;
})();"
`)
})

test('should inject CSS after the shebang line for es', async () => {
const result = getInlinedCSSInjectedCode(
`#!/usr/bin/env node
console.log("foo");`,
'es',
)
expect(result).toMatchInlineSnapshot(`
"#!/usr/bin/env node
injectCSS();console.log("foo");"
`)
})

test('should inject CSS at the start for es without shebang', async () => {
const result = getInlinedCSSInjectedCode(`console.log("foo");`, 'es')
expect(result).toMatchInlineSnapshot(`"injectCSS();console.log("foo");"`)
})

test('should inject CSS for es shebang without trailing newline', async () => {
const result = getInlinedCSSInjectedCode(`#!/usr/bin/env node`, 'es')
expect(result).toMatchInlineSnapshot(`
"#!/usr/bin/env node
injectCSS();"
`)
})
})
11 changes: 7 additions & 4 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1159,11 +1159,14 @@ export function injectInlinedCSS(
} else if (format === 'es') {
// legacy build
if (code.startsWith('#!')) {
let secondLinePos = code.indexOf('\n')
if (secondLinePos === -1) {
secondLinePos = 0
// inject after the shebang line instead of into it
const newlinePos = code.indexOf('\n')
if (newlinePos === -1) {
// the shebang has no trailing newline, add one so it stays intact
s.append(`\n${injectCode}`)
return
}
injectionPoint = secondLinePos
injectionPoint = newlinePos + 1
} else {
injectionPoint = 0
}
Expand Down
Loading