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

AriPerkkio marked this conversation as resolved.
Show resolved Hide resolved
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