Skip to content

Commit 6420ba0

Browse files
authored
fix(ssr): allow ssrTransform to parse hashbang (#8005)
1 parent 72f17f8 commit 6420ba0

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts

+19-2
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ test('overwrite bindings', async () => {
370370
`const a = { inject }\n` +
371371
`const b = { test: inject }\n` +
372372
`function c() { const { test: inject } = { test: true }; console.log(inject) }\n` +
373-
`const d = inject \n` +
373+
`const d = inject\n` +
374374
`function f() { console.log(inject) }\n` +
375375
`function e() { const { inject } = { inject: true } }\n` +
376376
`function g() { const f = () => { const inject = true }; console.log(inject) }\n`,
@@ -383,7 +383,7 @@ test('overwrite bindings', async () => {
383383
const a = { inject: __vite_ssr_import_0__.inject }
384384
const b = { test: __vite_ssr_import_0__.inject }
385385
function c() { const { test: inject } = { test: true }; console.log(inject) }
386-
const d = __vite_ssr_import_0__.inject
386+
const d = __vite_ssr_import_0__.inject
387387
function f() { console.log(__vite_ssr_import_0__.inject) }
388388
function e() { const { inject } = { inject: true } }
389389
function g() { const f = () => { const inject = true }; console.log(__vite_ssr_import_0__.inject) }
@@ -719,3 +719,20 @@ export default (function getRandom() {
719719
(await ssrTransform(`export default (class A {});`, null, null)).code
720720
).toMatchInlineSnapshot(`"__vite_ssr_exports__.default = (class A {});"`)
721721
})
722+
723+
// #8002
724+
test('with hashbang', async () => {
725+
expect(
726+
(
727+
await ssrTransform(
728+
`#!/usr/bin/env node
729+
console.log("it can parse the hashbang")`,
730+
null,
731+
null
732+
)
733+
).code
734+
).toMatchInlineSnapshot(`
735+
"#!/usr/bin/env node
736+
console.log(\\"it can parse the hashbang\\")"
737+
`)
738+
})

packages/vite/src/node/ssr/ssrTransform.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ export async function ssrTransform(
3737
ast = parser.parse(code, {
3838
sourceType: 'module',
3939
ecmaVersion: 'latest',
40-
locations: true
40+
locations: true,
41+
allowHashBang: true
4142
})
4243
} catch (err) {
4344
if (!err.loc || !err.loc.line) throw err

0 commit comments

Comments
 (0)