Skip to content

Commit 2ced1fe

Browse files
bteasapphi-red
andauthored
fix(config): exclude virtual modules from native config compat check (#22979)
Co-authored-by: sapphi-red <[email protected]>
1 parent abb793e commit 2ced1fe

5 files changed

Lines changed: 33 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "@vitejs/test-native-compat-mts",
3+
"type": "commonjs",
4+
"private": true
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { defineConfig } from 'vite'
2+
3+
export default defineConfig({})

‎packages/vite/src/node/nativeConfigCompat.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,13 @@ export function createNativeConfigCompatPlugin(
217217
return {
218218
name: 'vite:native-config-compat-check',
219219
transform: {
220-
filter: { id: /\.[cm]?[jt]sx?$/ },
220+
filter: {
221+
id: {
222+
include: /\.[cm]?[jt]sx?$/,
223+
// exclude rolldown runtime
224+
exclude: /^\0/,
225+
},
226+
},
221227
async handler(code, id) {
222228
const isESM =
223229
typeof process.versions.deno === 'string' || isFilePathESM(id)

‎packages/vite/src/node/utils.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,13 @@ export function isFilePathESM(
430430
return true
431431
} else if (/\.c[jt]s$/.test(filePath)) {
432432
return false
433+
} else if (filePath.startsWith('\0')) {
434+
// treat virtual modules as ESM
435+
return true
436+
} else if (!path.isAbsolute(filePath)) {
437+
// should not rely on `process.cwd()` as that would depend on
438+
// the environment and make it unreproducible
439+
return false
433440
} else {
434441
// check package.json for type: "module"
435442
try {
@@ -453,6 +460,15 @@ export function isFilePathFormatExplicit(
453460
if (/\.[mc][jt]s$/.test(filePath)) {
454461
return true
455462
}
463+
if (filePath.startsWith('\0')) {
464+
// treat virtual modules as ESM
465+
return true
466+
}
467+
if (!path.isAbsolute(filePath)) {
468+
// should not rely on `process.cwd()` as that would depend on
469+
// the environment and make it unreproducible
470+
return false
471+
}
456472
try {
457473
const pkg = findNearestPackageData(path.dirname(filePath), packageCache)
458474
return pkg?.data.type === 'module' || pkg?.data.type === 'commonjs'

‎pnpm-lock.yaml‎

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)