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: opt out of direct import in SSR when the file is not JS #8454

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ export function tryNodeResolve(
// otherwise we may introduce duplicated modules for externalized files
// from pre-bundled deps.
if (!isBuild) {
const versionHash = depsOptimizer.metadata({ ssr }).browserHash
const versionHash = depsOptimizer.metadata({ ssr })?.browserHash
Copy link
Member

Choose a reason for hiding this comment

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

This is always defined, why was this change needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It isn't defined when the module is CSS (tried with sanitize.css) maybe because it can't be optimized? This is new by the way, the previous version didn't need this.

Copy link
Member

Choose a reason for hiding this comment

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

I tried this repro https://github.com/cyco130/vite-ssr-react/tree/ssr-css-bug, against beta.5 and it is working 🤔
Could you create a failing repro so I can check what is going on here?

Copy link
Member

Choose a reason for hiding this comment

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

I found why you need this, I'll send a PR tomorrow to fix it. Still a repro of the CSS issue against latest would be good

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could you create a failing repro so I can check what is going on here?

I updated the repro now and it is failing for me. Maybe try rm -rf node_modules/.vite?

if (versionHash && isJsType) {
resolved = injectQuery(resolved, `v=${versionHash}`)
}
Expand Down
12 changes: 9 additions & 3 deletions packages/vite/src/node/ssr/ssrModuleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type { InternalResolveOptions } from '../plugins/resolve'
import { tryNodeResolve } from '../plugins/resolve'
import { hookNodeResolve } from '../plugins/ssrRequireHook'
import { NULL_BYTE_PLACEHOLDER } from '../constants'
import { isCSSRequest } from '../plugins/css'
import {
ssrDynamicImportKey,
ssrExportAllKey,
Expand Down Expand Up @@ -135,7 +136,10 @@ async function instantiateModule(

const ssrImport = async (dep: string) => {
if (dep[0] !== '.' && dep[0] !== '/') {
return nodeImport(dep, mod.file!, resolveOptions)
const imported = await nodeImport(dep, mod.file!, resolveOptions)
if (imported !== undefined) {
return imported
}
}
dep = unwrapId(dep)
if (!isCircular(dep) && !pendingImports.get(dep)?.some(isCircular)) {
Expand Down Expand Up @@ -297,8 +301,10 @@ async function nodeImport(
}

try {
const mod = await dynamicImport(url)
return proxyESM(mod)
if (!isCSSRequest(url)) {
const mod = await dynamicImport(url)
return proxyESM(mod)
}
} finally {
unhookNodeResolve()
}
Expand Down