Skip to content

Commit

Permalink
fix(vite-node): fix esm false-detection inside comment (#6506)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Sep 17, 2024
1 parent 7d028cb commit 91f8599
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 1 deletion.
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

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)
})

0 comments on commit 91f8599

Please sign in to comment.