-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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(ssr): strip NULL_BYTE_PLACEHOLDER before import #9124
fix(ssr): strip NULL_BYTE_PLACEHOLDER before import #9124
Conversation
✅ Deploy Preview for vite-docs-main canceled.
|
url = unwrapId(removeImportQuery(url)).replace( | ||
NULL_BYTE_PLACEHOLDER, | ||
'\0' | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is needed. transformRequest
expects an unwrapped+\0
URL (we need a name for the two URL spaces, rollup URL and browser URL?). This may not be related to what #6390 tried to fix, but I think we should always call transformRequest
with a rollup URL. Probably not in this PR, but for 3.1 it may be worth allowing transformRequest
to accept an URL in any space and always call unwrap+removeImportQuery+use\0
(maybe we could have a toRollupURL()
helper that does this? Still unsure about naming).
@@ -38,7 +38,7 @@ export async function ssrLoadModule( | |||
urlStack: string[] = [], | |||
fixStacktrace?: boolean | |||
): Promise<SSRModule> { | |||
url = unwrapId(url).replace(NULL_BYTE_PLACEHOLDER, '\0') | |||
url = unwrapId(url) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the \0
replacement is needed. If we do an unwrapId
, we are trying to go from a browser-friendly URL to a valid rollup URL (what plugins expect). I think this is part of not having a helper like toRollupURL
that does all the needed transforms.
With this line at the start of ssrLoadModule
, it means it can receive both a browser-friendly URL or a Rollup URL and it will normalize it. I think we may want to do this with transformRequest
too later, as I said in my prev comment
@@ -137,7 +137,7 @@ async function instantiateModule( | |||
if (dep[0] !== '.' && dep[0] !== '/') { | |||
return nodeImport(dep, mod.file!, resolveOptions) | |||
} | |||
dep = unwrapId(dep) | |||
dep = unwrapId(dep).replace(NULL_BYTE_PLACEHOLDER, '\0') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC, this may be the only line needed to do the fix. And it isn't needed because we call ssrLoadModule
, but because we check pendingImports
and moduleGraph.urlToModuleMap.get(dep)
, and both should be in the rollup URL space.
Here is a previous attempt trying to solve the same issue: I don't think the implementation is right, but it may be useful to check if the test cases are good to merge later. I think that the PR (with the comments above) should solve the issue of duplicated ids in the module graph due to We would still need to address your comment about // for incoming urls, it is important to:
// 1. remove the HMR timestamp query (?t=xxxx)
// 2. resolve its extension so that urls with or without extension all map to
// the same module
async resolveUrl(url: string, ssr?: boolean): Promise<ResolvedUrl> {
url = removeImportQuery(removeTimestampQuery(url))
const resolved = await this.resolveId(url, !!ssr)
const resolvedId = resolved?.id || url
if (url !== resolvedId && !url.includes('\0') && !url.startsWith(`virtual:`)) {
const ext = extname(cleanUrl(resolvedId))
const { pathname, search, hash } = parseUrl(url)
if (ext && !pathname!.endsWith(ext)) {
url = pathname + ext + (search || '') + (hash || '')
}
}
return [url, resolvedId, resolved?.meta]
} And then we should also merge #9036. |
About this comment, and to leave a trace in the main thread of the PR. For 3.1 or maybe 4.0, I think we should clarify the expected URL space for each of the main public functions. Some definitions:
Right now, we have:
I proposed in the comments yesterday that we may want to make Supporting both is comfortable, you don't need users to think about what space their URLs are (normally if you are in a middleware you are browser space, and if you are in a plugin you are in rollup space). But also then we can't have proper decode/encode escape hatches (these are implemented right now, but maybe we should?). Users can't have a So maybe we should require For reference, the |
Thank you for the detailed explanation. Now I think I understand it properly. I was understanding "URL" stands for browser-space URL and "id" stands for rollup-space URL. I'll create a separate PR to fix the issue described at #9036. (because I noticed that fix will not be related to this one) |
|
Description
This PR revises #6390. That implementation might duplicate moduleGraph entry (not sure if it happens).
Maybe it is safer to go with #9036 for 3.0 and merge this PR in 3.1.reverts #6390
fixes the issue described at #9036(it's still worth to merge that PR because it replaces the deprecatedurl.parse
)Additional context
This line
import 'id'
becomesimport 'resolvedId'
.vite/packages/vite/src/node/ssr/ssrModuleLoader.ts
Lines 87 to 89 in 0cb4b25
Then, that
resolvedId
is passed tossrLoadModule
here.vite/packages/vite/src/node/ssr/ssrModuleLoader.ts
Lines 136 to 162 in 0cb4b25
So for the first
instantiateModule
call byssrLoadModule
, the raw url is passed toinstantiateModule
. But for other calls, the resolvedId is passed as url toinstantiateModule
.What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).