Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ssr): allow ssrTransform to parse hashbang #8005

Merged
merged 3 commits into from May 4, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 19 additions & 2 deletions packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts
Expand Up @@ -77,7 +77,7 @@ test('export named', async () => {
(await ssrTransform(`const a = 1, b = 2; export { a, b as c }`, null, null))
.code
).toMatchInlineSnapshot(`
"const a = 1, b = 2;
"const a = 1, b = 2;
Object.defineProperty(__vite_ssr_exports__, \\"a\\", { enumerable: true, configurable: true, get(){ return a }});
Object.defineProperty(__vite_ssr_exports__, \\"c\\", { enumerable: true, configurable: true, get(){ return b }});"
`)
Expand Down Expand Up @@ -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
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