Skip to content

Commit

Permalink
fix(ssr): allow ssrTransform to parse hashbang (#8005)
Browse files Browse the repository at this point in the history
  • Loading branch information
togami2864 authored May 4, 2022
1 parent 72f17f8 commit 6420ba0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
21 changes: 19 additions & 2 deletions packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ test('overwrite bindings', async () => {
`const a = { inject }\n` +
`const b = { test: inject }\n` +
`function c() { const { test: inject } = { test: true }; console.log(inject) }\n` +
`const d = inject \n` +
`const d = inject\n` +
`function f() { console.log(inject) }\n` +
`function e() { const { inject } = { inject: true } }\n` +
`function g() { const f = () => { const inject = true }; console.log(inject) }\n`,
Expand All @@ -383,7 +383,7 @@ test('overwrite bindings', async () => {
const a = { inject: __vite_ssr_import_0__.inject }
const b = { test: __vite_ssr_import_0__.inject }
function c() { const { test: inject } = { test: true }; console.log(inject) }
const d = __vite_ssr_import_0__.inject
const d = __vite_ssr_import_0__.inject
function f() { console.log(__vite_ssr_import_0__.inject) }
function e() { const { inject } = { inject: true } }
function g() { const f = () => { const inject = true }; console.log(__vite_ssr_import_0__.inject) }
Expand Down Expand Up @@ -719,3 +719,20 @@ export default (function getRandom() {
(await ssrTransform(`export default (class A {});`, null, null)).code
).toMatchInlineSnapshot(`"__vite_ssr_exports__.default = (class A {});"`)
})

// #8002
test('with hashbang', async () => {
expect(
(
await ssrTransform(
`#!/usr/bin/env node
console.log("it can parse the hashbang")`,
null,
null
)
).code
).toMatchInlineSnapshot(`
"#!/usr/bin/env node
console.log(\\"it can parse the hashbang\\")"
`)
})
3 changes: 2 additions & 1 deletion packages/vite/src/node/ssr/ssrTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ export async function ssrTransform(
ast = parser.parse(code, {
sourceType: 'module',
ecmaVersion: 'latest',
locations: true
locations: true,
allowHashBang: true
})
} catch (err) {
if (!err.loc || !err.loc.line) throw err
Expand Down

0 comments on commit 6420ba0

Please sign in to comment.