-
-
Notifications
You must be signed in to change notification settings - Fork 198
feat(rsc): add support for experimental.renderBuiltUrl on assets metadata
#612
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
Merged
+267
−21
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
98e75df
feat(rsc): add support for renderBuiltInUrl on assets metadata
3fdf2bc
refactor: fix types by adding `ResolvedAssetDeps`
hi-ogawa b791ba3
test: rename root
hi-ogawa 9a00e4d
test: revert unneeded change for now
hi-ogawa 99cb014
test: tweak
hi-ogawa 8fd23df
test: tweak renderBuiltUrl-string test
hi-ogawa bff5ea1
refactor: remove unecessary params
HenriqueLimas 3eda709
fix: evaluate bootstrapScriptContent import url on server
hi-ogawa 74cbe31
test: wip renderBuiltUrl runtime
hi-ogawa 87c9d0c
chore: pretty json
hi-ogawa 6fbeba2
test: test dynamic base
hi-ogawa 7bc7532
test: put back minimal dev test
hi-ogawa 9ea817c
chore: cleanup
hi-ogawa 51a3aa2
test: test dynamic base on browser
hi-ogawa d8cb6d5
Merge branch 'main' into render-built-in-url
hi-ogawa 37844ea
test: verify runtime url in built manifest
hi-ogawa 9ae2459
refactor: remove trivial assertion
hi-ogawa 9626836
chore: const
hi-ogawa 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -242,10 +242,8 @@ export default function vitePluginRsc( | |
| serverResourcesMetaMap = sortObject(serverResourcesMetaMap) | ||
| await builder.build(builder.environments.client!) | ||
|
|
||
| const assetsManifestCode = `export default ${JSON.stringify( | ||
| const assetsManifestCode = `export default ${serializeValueWithRuntime( | ||
| buildAssetsManifest, | ||
| null, | ||
| 2, | ||
| )}` | ||
| const manifestPath = path.join( | ||
| builder.environments!.rsc!.config.build!.outDir!, | ||
|
|
@@ -586,7 +584,7 @@ export default function vitePluginRsc( | |
| assert(this.environment.mode === 'dev') | ||
| const entryUrl = assetsURL('@id/__x00__' + VIRTUAL_ENTRIES.browser) | ||
| const manifest: AssetsManifest = { | ||
| bootstrapScriptContent: `import(${JSON.stringify(entryUrl)})`, | ||
| bootstrapScriptContent: `import(${serializeValueWithRuntime(entryUrl)})`, | ||
| clientReferenceDeps: {}, | ||
| } | ||
| return `export default ${JSON.stringify(manifest, null, 2)}` | ||
|
|
@@ -640,8 +638,16 @@ export default function vitePluginRsc( | |
| mergeAssetDeps(deps, entry.deps), | ||
| ) | ||
| } | ||
| let bootstrapScriptContent: string | RuntimeAsset | ||
| if (typeof entryUrl === 'string') { | ||
| bootstrapScriptContent = `import(${JSON.stringify(entryUrl)})` | ||
| } else { | ||
| bootstrapScriptContent = new RuntimeAsset( | ||
| `"import(" + JSON.stringify(${entryUrl.runtime}) + ")"`, | ||
| ) | ||
| } | ||
|
Comment on lines
+641
to
+648
Contributor
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. Previously {
"bootstrapScriptContent":"import(globalThis.__dynamicBase + \"assets/index-BxtkZ08f.js\")",But, I think this should be aligned to other assets, so the runtime is resolved on server: {
"bootstrapScriptContent": "import(" + JSON.stringify(globalThis.__dynamicBase + "assets/index-BxtkZ08f.js") + ")", |
||
| buildAssetsManifest = { | ||
| bootstrapScriptContent: `import(${JSON.stringify(entryUrl)})`, | ||
| bootstrapScriptContent, | ||
| clientReferenceDeps, | ||
| serverResources, | ||
| } | ||
|
|
@@ -671,10 +677,8 @@ export default function vitePluginRsc( | |
| if (this.environment.name === 'ssr') { | ||
| // output client manifest to non-client build directly. | ||
| // this makes server build to be self-contained and deploy-able for cloudflare. | ||
| const assetsManifestCode = `export default ${JSON.stringify( | ||
| const assetsManifestCode = `export default ${serializeValueWithRuntime( | ||
| buildAssetsManifest, | ||
| null, | ||
| 2, | ||
| )}` | ||
| for (const name of ['ssr', 'rsc']) { | ||
| const manifestPath = path.join( | ||
|
|
@@ -1273,15 +1277,76 @@ function generateDynamicImportCode(map: Record<string, string>) { | |
| return `export default {${code}};\n` | ||
| } | ||
|
|
||
| // // https://github.com/vitejs/vite/blob/2a7473cfed96237711cda9f736465c84d442ddef/packages/vite/src/node/plugins/importAnalysisBuild.ts#L222-L230 | ||
| class RuntimeAsset { | ||
| runtime: string | ||
| constructor(value: string) { | ||
| this.runtime = value | ||
| } | ||
| } | ||
|
|
||
| function serializeValueWithRuntime(value: any) { | ||
| const replacements: [string, string][] = [] | ||
| let result = JSON.stringify( | ||
| value, | ||
| (_key, value) => { | ||
| if (value instanceof RuntimeAsset) { | ||
| const placeholder = `__runtime_placeholder_${replacements.length}__` | ||
| replacements.push([placeholder, value.runtime]) | ||
| return placeholder | ||
| } | ||
|
|
||
| return value | ||
| }, | ||
| 2, | ||
| ) | ||
|
|
||
| for (const [placeholder, runtime] of replacements) { | ||
| result = result.replace(`"${placeholder}"`, runtime) | ||
| } | ||
|
|
||
| return result | ||
| } | ||
|
|
||
| function assetsURL(url: string) { | ||
| if ( | ||
| config.command === 'build' && | ||
| typeof config.experimental?.renderBuiltUrl === 'function' | ||
| ) { | ||
| // https://github.com/vitejs/vite/blob/bdde0f9e5077ca1a21a04eefc30abad055047226/packages/vite/src/node/build.ts#L1369 | ||
| const result = config.experimental.renderBuiltUrl(url, { | ||
| type: 'asset', | ||
| hostType: 'js', | ||
hi-ogawa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ssr: true, | ||
| hostId: '', | ||
| }) | ||
|
|
||
| if (typeof result === 'object') { | ||
| if (result.runtime) { | ||
| return new RuntimeAsset(result.runtime) | ||
hi-ogawa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| assert( | ||
| !result.relative, | ||
| '"result.relative" not supported on renderBuiltUrl() for RSC', | ||
| ) | ||
| } else if (result) { | ||
| return result satisfies string | ||
| } | ||
| } | ||
|
|
||
| // https://github.com/vitejs/vite/blob/2a7473cfed96237711cda9f736465c84d442ddef/packages/vite/src/node/plugins/importAnalysisBuild.ts#L222-L230 | ||
| return config.base + url | ||
| } | ||
|
|
||
| function assetsURLOfDeps(deps: AssetDeps) { | ||
| return { | ||
| js: deps.js.map((href) => assetsURL(href)), | ||
| css: deps.css.map((href) => assetsURL(href)), | ||
| js: deps.js.map((href) => { | ||
| assert(typeof href === 'string') | ||
| return assetsURL(href) | ||
| }), | ||
| css: deps.css.map((href) => { | ||
| assert(typeof href === 'string') | ||
| return assetsURL(href) | ||
| }), | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -1290,12 +1355,23 @@ function assetsURLOfDeps(deps: AssetDeps) { | |
| // | ||
|
|
||
| export type AssetsManifest = { | ||
| bootstrapScriptContent: string | ||
| bootstrapScriptContent: string | RuntimeAsset | ||
| clientReferenceDeps: Record<string, AssetDeps> | ||
| serverResources?: Record<string, { css: string[] }> | ||
| serverResources?: Record<string, Pick<AssetDeps, 'css'>> | ||
| } | ||
|
|
||
| export type AssetDeps = { | ||
| js: (string | RuntimeAsset)[] | ||
| css: (string | RuntimeAsset)[] | ||
| } | ||
|
|
||
| export type ResolvedAssetsManifest = { | ||
| bootstrapScriptContent: string | ||
| clientReferenceDeps: Record<string, ResolvedAssetDeps> | ||
| serverResources?: Record<string, Pick<ResolvedAssetDeps, 'css'>> | ||
| } | ||
|
|
||
| export type ResolvedAssetDeps = { | ||
| js: string[] | ||
| css: string[] | ||
| } | ||
|
|
@@ -1574,7 +1650,7 @@ export function vitePluginRscCss( | |
| this.addWatchFile(file) | ||
| } | ||
| const hrefs = result.hrefs.map((href) => assetsURL(href.slice(1))) | ||
| return `export default ${JSON.stringify(hrefs)}` | ||
| return `export default ${serializeValueWithRuntime(hrefs)}` | ||
| } | ||
| }, | ||
| }, | ||
|
|
@@ -1661,7 +1737,7 @@ export function vitePluginRscCss( | |
| encodeURIComponent(importer), | ||
| ] | ||
| const deps = assetsURLOfDeps({ css: cssHrefs, js: jsHrefs }) | ||
| return generateResourcesCode(JSON.stringify(deps, null, 2)) | ||
| return generateResourcesCode(serializeValueWithRuntime(deps)) | ||
| } else { | ||
| const key = normalizePath(path.relative(config.root, importer)) | ||
| serverResourcesMetaMap[importer] = { key } | ||
|
|
@@ -1742,7 +1818,10 @@ function collectModuleDependents(mods: EnvironmentModuleNode[]) { | |
| } | ||
|
|
||
| function generateResourcesCode(depsCode: string) { | ||
| const ResourcesFn = (React: typeof import('react'), deps: AssetDeps) => { | ||
| const ResourcesFn = ( | ||
| React: typeof import('react'), | ||
| deps: ResolvedAssetDeps, | ||
| ) => { | ||
| return function Resources() { | ||
| return React.createElement(React.Fragment, null, [ | ||
| ...deps.css.map((href: string) => | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.