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(vite-node): fix esm false-detection inside comment #6506

Merged
merged 3 commits into from
Sep 17, 2024
Merged
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
5 changes: 4 additions & 1 deletion packages/vite-node/src/externalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ const ESM_SYNTAX_RE
const ESM_EXT_RE = /\.(es|esm|esm-browser|esm-bundler|es6|module)\.js$/
const ESM_FOLDER_RE = /\/(es|esm)\/(.*\.js)$/

// https://stackoverflow.com/a/15123777
const COMMENT_RE = /\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm

Comment on lines +14 to +16
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would https://github.com/antfu/strip-literal be better here? It should be able to cover all kinds of edge cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that would be an option, but that's essentially running js-token based lexer, so if we go with something more sophisticated, it would be probably more direct (and also faster?) to use es-module-lexer's esm detection https://github.com/guybedford/es-module-lexer?tab=readme-ov-file#esm-detection

My impression is that over-stripping the code is mostly fine, so this regex should probably suffice for our usage.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This regex looks good enough for the reproduction's case. Also fast enough: https://regexr.com/85v1c

const defaultInline = [
/virtual:/,
/\.[mc]?ts$/,
Expand Down Expand Up @@ -79,7 +82,7 @@ async function isValidNodeImport(id: string) {

const code = await fsp.readFile(id, 'utf8').catch(() => '')

return !ESM_SYNTAX_RE.test(code)
return !ESM_SYNTAX_RE.test(code.replace(COMMENT_RE, ''))
}

const _defaultExternalizeCache = new Map<string, Promise<string | false>>()
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions test/core/deps/dep-esm-comment/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// import x from "x"
/** import x from "x" */
/**
* import x from "x"
*/
module.exports = { test: 'ok' }
5 changes: 5 additions & 0 deletions test/core/deps/dep-esm-comment/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "@vitest/test-dep-esm-comment",
"type": "commonjs",
"exports": "./index.js"
}
1 change: 1 addition & 0 deletions test/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@vitest/expect": "workspace:*",
"@vitest/mocker": "workspace:*",
"@vitest/runner": "workspace:*",
"@vitest/test-dep-esm-comment": "file:./deps/dep-esm-comment",
"@vitest/test-dep1": "file:./deps/dep1",
"@vitest/test-dep2": "file:./deps/dep2",
"@vitest/utils": "workspace:*",
Expand Down
11 changes: 11 additions & 0 deletions test/core/test/dual-package-hazard.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { createRequire } from 'node:module'
import { expect, test } from 'vitest'

// @ts-expect-error no ts
Expand All @@ -6,7 +7,17 @@ import * as dep1 from '@vitest/test-dep1'
// @ts-expect-error no ts
import * as dep2 from '@vitest/test-dep2'

// @ts-expect-error no ts
import depEsmComment from '@vitest/test-dep-esm-comment'

const require = createRequire(import.meta.url)

test('no dual package hazard by externalizing esm deps by default', async () => {
dep1.data.hello = 'world'
expect(dep2.data.hello).toBe('world')
})

test('externalize cjs with esm comment', async () => {
const depEsmCommentRequire = require('@vitest/test-dep-esm-comment')
expect(depEsmComment).toBe(depEsmCommentRequire)
})
Loading