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

feat!: remove ssr proxy for externalized modules #14521

Merged
merged 10 commits into from
Oct 19, 2023
44 changes: 16 additions & 28 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ import {
dynamicImport,
isBuiltin,
isExternalUrl,
isFilePathESM,
isNodeBuiltin,
isObject,
lookupFile,
mergeAlias,
mergeConfig,
normalizeAlias,
Expand Down Expand Up @@ -323,8 +323,16 @@ export interface ExperimentalOptions {

export interface LegacyOptions {
/**
* No longer needed for now, but kept for backwards compatibility.
* In Vite 4, SSR-externalized modules (modules not bundled and loaded by Node.js at runtime)
* are implicitly proxied in dev to automatically handle `default` and `__esModule` access.
* However, this does not correctly reflect how it works in the Node.js runtime, causing
* inconsistencies between dev and prod.
*
* In Vite 5, the proxy is removed so dev and prod are consistent, but if you still require
* the old behaviour, you can enable this option. If so, please leave your feedback at
* **TODO GitHub discussion link**.
*/
proxySsrExternalModules?: boolean
}

export interface ResolveWorkerOptions extends PluginHookUtils {
Expand Down Expand Up @@ -967,19 +975,7 @@ export async function loadConfigFromFile(
return null
}

let isESM = false
if (/\.m[jt]s$/.test(resolvedPath)) {
isESM = true
} else if (/\.c[jt]s$/.test(resolvedPath)) {
isESM = false
} else {
// check package.json for type: "module" and set `isESM` to true
try {
const pkg = lookupFile(configRoot, ['package.json'])
isESM =
!!pkg && JSON.parse(fs.readFileSync(pkg, 'utf-8')).type === 'module'
} catch (e) {}
}
const isESM = isFilePathESM(resolvedPath)

try {
const bundled = await bundleConfigFile(resolvedPath, isESM)
Expand Down Expand Up @@ -1065,18 +1061,6 @@ async function bundleConfigFile(
false,
)?.id
}
const isESMFile = (id: string): boolean => {
if (id.endsWith('.mjs')) return true
if (id.endsWith('.cjs')) return false

const nearestPackageJson = findNearestPackageData(
path.dirname(id),
packageCache,
)
return (
!!nearestPackageJson && nearestPackageJson.data.type === 'module'
)
}

// externalize bare imports
build.onResolve(
Expand Down Expand Up @@ -1124,7 +1108,11 @@ async function bundleConfigFile(
if (idFsPath && isImport) {
idFsPath = pathToFileURL(idFsPath).href
}
if (idFsPath && !isImport && isESMFile(idFsPath)) {
if (
idFsPath &&
!isImport &&
isFilePathESM(idFsPath, packageCache)
) {
throw new Error(
`${JSON.stringify(
id,
Expand Down
10 changes: 2 additions & 8 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
isBuiltin,
isDataUrl,
isExternalUrl,
isFilePathESM,
isInNodeModules,
isNonDriveRelativeAbsolutePath,
isObject,
Expand Down Expand Up @@ -822,8 +823,6 @@ export function tryNodeResolve(
})
}

const ext = path.extname(resolved)

if (
!options.ssrOptimizeCheck &&
(!isInNodeModules(resolved) || // linked
Expand Down Expand Up @@ -859,12 +858,7 @@ export function tryNodeResolve(
(!options.ssrOptimizeCheck && !isBuild && ssr) ||
// Only optimize non-external CJS deps during SSR by default
(ssr &&
!(
ext === '.cjs' ||
(ext === '.js' &&
findNearestPackageData(path.dirname(resolved), options.packageCache)
?.data.type !== 'module')
) &&
isFilePathESM(resolved, options.packageCache) &&
!(include?.includes(pkgId) || include?.includes(id)))

if (options.ssrOptimizeCheck) {
Expand Down
Loading