-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
feat: support full bundle mode in ssr #21626
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
Draft
sheremet-va
wants to merge
22
commits into
vitejs:main
Choose a base branch
from
sheremet-va:fix/support-full-bundle-mode-in-ssr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+855
−351
Draft
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
a6026e4
feat: support ful bundle mode in ssr
sheremet-va 570c5c8
fix: resolve import's url correctly
sheremet-va 7f78249
fix: scope rolldown runtime to a module runner
sheremet-va a15b57a
fix: move entry resolution to `fetchModule`
sheremet-va ebfbb4e
fix: resolve url as a virtual module, support dynamic (statically ana…
sheremet-va 62878a7
chore: cleanup
sheremet-va 6ec7690
chore: add `_waitForInitialBuildSuccess`
sheremet-va 6b2b2e6
Merge branch 'main' of github.com:vitejs/vite into fix/support-full-b…
sheremet-va 08de7c9
chore: increase the limit
sheremet-va c7a70da
test: fix tests
sheremet-va ca267cb
refactor: use `fullReload` as a fixture
sheremet-va eb52e5f
fix: provide a file
sheremet-va 81c0c97
test: fix test
sheremet-va d6d6b73
chore: cleanup tests
sheremet-va 3a517a8
chore: tests
sheremet-va d7a452a
chore: use posix to resolve
sheremet-va 073737c
fix: normalize windows paths
sheremet-va cb586d9
chore: cleanup
sheremet-va 1120a86
chore: remove only
sheremet-va c0993e9
fix: hmr
sheremet-va f28cec6
refactor: introduce createFullBundleRunnableDevEnvironment and move t…
sheremet-va 867f528
chore: cleanup
sheremet-va File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import type { ViteHotContext } from '#types/hot' | ||
| import type { DevRuntime } from 'rolldown/experimental/runtime-types' | ||
| import { HMRClient, HMRContext, type HMRLogger } from '../shared/hmr' | ||
| import { cleanUrl, isPrimitive } from '../shared/utils' | ||
| import { analyzeImportedModDifference } from '../shared/ssrTransform' | ||
|
|
@@ -16,14 +16,18 @@ import type { | |
| ResolvedResult, | ||
| SSRImportMetadata, | ||
| } from './types' | ||
| import { posixDirname, posixPathToFileHref, posixResolve } from './utils' | ||
| import { posixDirname, posixJoin } from './utils' | ||
| import { | ||
| ssrDynamicImportKey, | ||
| ssrExportAllKey, | ||
| ssrExportNameKey, | ||
| ssrImportKey, | ||
| ssrImportMetaKey, | ||
| ssrModuleExportsKey, | ||
| ssrRolldownRuntimeCreateHotContextMethod, | ||
| ssrRolldownRuntimeDefineMethod, | ||
| ssrRolldownRuntimeKey, | ||
| ssrRolldownRuntimeTransport, | ||
| } from './constants' | ||
| import { hmrLogger, silentConsole } from './hmrLogger' | ||
| import { createHMRHandlerForRunner } from './hmrHandler' | ||
|
|
@@ -47,6 +51,42 @@ export class ModuleRunner { | |
| >() | ||
| private isBuiltin?: (id: string) => boolean | ||
| private builtinsPromise?: Promise<void> | ||
| private rolldownDevRuntime?: DevRuntime | ||
|
|
||
| // We need the proxy because the runtime MUST be ready before the first import is processed. | ||
| // Because `context['__rolldown_runtime__']` is passed down before the modules are executed as a function argument. | ||
| private rolldownDevRuntimeProxy = new Proxy( | ||
| {}, | ||
| { | ||
| get: (_, p, receiver) => { | ||
| // Special `__rolldown_runtime__.__vite_ssr_defineRuntime__` method only for the module runner, | ||
| // It's not available in the browser because it's a global there. We cannot have it as a global because | ||
| // - It's possible to have multiple runners (`ssrLoadModule` has its own compat runner); | ||
| // - We don't want to pollute Dev Server's global namespace. | ||
| if (p === ssrRolldownRuntimeDefineMethod) { | ||
| return (runtime: DevRuntime) => { | ||
| this.rolldownDevRuntime = runtime | ||
| } | ||
| } | ||
|
|
||
| if (p === ssrRolldownRuntimeCreateHotContextMethod) { | ||
| return this.closed | ||
| ? () => {} | ||
| : (url: string) => this.ensureModuleHotContext(url) | ||
| } | ||
|
|
||
| if (p === ssrRolldownRuntimeTransport) { | ||
| return this.closed ? undefined : this.transport | ||
| } | ||
|
|
||
| if (!this.rolldownDevRuntime) { | ||
| throw new Error(`__rolldown_runtime__ was not initialized.`) | ||
| } | ||
|
|
||
| return Reflect.get(this.rolldownDevRuntime, p, receiver) | ||
| }, | ||
| }, | ||
| ) as DevRuntime | ||
|
|
||
| private closed = false | ||
|
|
||
|
|
@@ -89,7 +129,7 @@ export class ModuleRunner { | |
| */ | ||
| public async import<T = any>(url: string): Promise<T> { | ||
| const fetchedModule = await this.cachedModule(url) | ||
| return await this.cachedRequest(url, fetchedModule) | ||
| return await this.cachedRequest(fetchedModule.url, fetchedModule) | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -353,7 +393,7 @@ export class ModuleRunner { | |
| // it's possible to provide an object with toString() method inside import() | ||
| dep = String(dep) | ||
| if (dep[0] === '.') { | ||
| dep = posixResolve(posixDirname(url), dep) | ||
| dep = posixJoin(posixDirname(url), dep) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
| return request(dep, { isDynamicImport: true }) | ||
| } | ||
|
|
@@ -380,9 +420,10 @@ export class ModuleRunner { | |
| const createImportMeta = | ||
| this.options.createImportMeta ?? createDefaultImportMeta | ||
|
|
||
| const modulePath = cleanUrl(file || moduleId) | ||
| const modulePath = file | ||
| ? cleanUrl(file) | ||
| : `data:application/javascript,${code};` | ||
| // disambiguate the `<UNIT>:/` on windows: see nodejs/node#31710 | ||
| const href = posixPathToFileHref(modulePath) | ||
| const meta = await createImportMeta(modulePath) | ||
| const exports = Object.create(null) | ||
| Object.defineProperty(exports, Symbol.toStringTag, { | ||
|
|
@@ -393,20 +434,19 @@ export class ModuleRunner { | |
|
|
||
| mod.exports = exports | ||
|
|
||
| let hotContext: ViteHotContext | undefined | ||
| if (this.hmrClient) { | ||
| Object.defineProperty(meta, 'hot', { | ||
| enumerable: true, | ||
| get: () => { | ||
| if (!this.hmrClient) { | ||
| throw new Error(`[module runner] HMR client was closed.`) | ||
| return | ||
| } | ||
| this.debug?.('[module runner] creating hmr context for', mod.url) | ||
| hotContext ||= new HMRContext(this.hmrClient, mod.url) | ||
| return hotContext | ||
| this.ensureModuleHotContext(mod.url) | ||
| return this.moduleHotContexts.get(mod.url) | ||
| }, | ||
| set: (value) => { | ||
| hotContext = value | ||
| this.moduleHotContexts.set(mod.url, value) | ||
| }, | ||
| }) | ||
| } | ||
|
|
@@ -423,14 +463,29 @@ export class ModuleRunner { | |
| get: getter, | ||
| }), | ||
| [ssrImportMetaKey]: meta, | ||
| [ssrRolldownRuntimeKey]: this.rolldownDevRuntimeProxy, | ||
| } | ||
|
|
||
| this.debug?.('[module runner] executing', href) | ||
| this.debug?.('[module runner] executing', meta.href) | ||
|
|
||
| await this.evaluator.runInlinedModule(context, code, mod) | ||
|
|
||
| return exports | ||
| } | ||
|
|
||
| private moduleHotContexts = new Map<string, HMRContext>() | ||
|
|
||
| private ensureModuleHotContext(url: string) { | ||
| if (!this.hmrClient) { | ||
| return | ||
| } | ||
|
|
||
| if (!this.moduleHotContexts.has(url)) { | ||
| const hotContext = new HMRContext(this.hmrClient, url) | ||
| this.moduleHotContexts.set(url, hotContext) | ||
| } | ||
| return this.moduleHotContexts.get(url) | ||
| } | ||
| } | ||
|
|
||
| function exportAll(exports: any, sourceModule: any) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is what Node returns in
import.meta.urlif it runs in REPL or inside--import=data/application. I think we can reuse this ideaimport.meta.dirnameandfilenameare also not available there since files are virtualBut it will be quite inconvenient at runtime
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.
Another idea is to use the file where it would be bundled. However then something like
readFileSync(import.meta.filename)will throw an errorThere 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 decided to go with the second approach for now to avoid any breaking changes