Skip to content

Commit bffc82e

Browse files
authored
fix(rsc): ensure .js suffix for internal virtual modules (#744)
1 parent 53b3f48 commit bffc82e

File tree

5 files changed

+49
-48
lines changed

5 files changed

+49
-48
lines changed

packages/plugin-rsc/src/plugin.ts

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ import {
4343
getEntrySource,
4444
hashString,
4545
normalizeRelativePath,
46-
parseIdQuery,
4746
sortObject,
4847
withRollupError,
4948
} from './plugins/utils'
5049
import { createDebug } from '@hiogawa/utils'
5150
import { transformScanBuildStrip } from './plugins/scan'
5251
import { validateImportPlugin } from './plugins/validate-import'
5352
import { vitePluginFindSourceMapURL } from './plugins/find-source-map-url'
53+
import { parseCssVirtual, toCssVirtual, parseIdQuery } from './plugins/shared'
5454

5555
const BUILD_ASSETS_MANIFEST_NAME = '__vite_rsc_assets_manifest.js'
5656

@@ -1797,15 +1797,16 @@ function vitePluginRscCss(
17971797
},
17981798
},
17991799
{
1800-
name: 'rsc:css/dev-ssr-virtual',
1800+
name: 'rsc:css-virtual',
18011801
resolveId(source) {
1802-
if (source.startsWith('virtual:vite-rsc/css/dev-ssr/')) {
1802+
if (source.startsWith('virtual:vite-rsc/css?')) {
18031803
return '\0' + source
18041804
}
18051805
},
18061806
async load(id) {
1807-
if (id.startsWith('\0virtual:vite-rsc/css/dev-ssr/')) {
1808-
id = id.slice('\0virtual:vite-rsc/css/dev-ssr/'.length)
1807+
const parsed = parseCssVirtual(id)
1808+
if (parsed?.type === 'ssr') {
1809+
id = parsed.id
18091810
const { server } = manager
18101811
const mod =
18111812
await server.environments.ssr.moduleGraph.getModuleByUrl(id)
@@ -1852,9 +1853,8 @@ function vitePluginRscCss(
18521853
continue
18531854
}
18541855
}
1855-
const importId = `virtual:vite-rsc/importer-resources?importer=${encodeURIComponent(
1856-
importer,
1857-
)}`
1856+
1857+
const importId = toCssVirtual({ id: importer, type: 'rsc' })
18581858

18591859
// use dynamic import during dev to delay crawling and discover css correctly.
18601860
let replacement: string
@@ -1891,26 +1891,17 @@ function vitePluginRscCss(
18911891
}
18921892
}
18931893
},
1894-
resolveId(source) {
1895-
if (
1896-
source.startsWith('virtual:vite-rsc/importer-resources?importer=')
1897-
) {
1898-
assert(this.environment.name === 'rsc')
1899-
return '\0' + source
1900-
}
1901-
},
19021894
load(id) {
19031895
const { server } = manager
1904-
if (id.startsWith('\0virtual:vite-rsc/importer-resources?importer=')) {
1905-
const importer = decodeURIComponent(
1906-
parseIdQuery(id).query['importer']!,
1907-
)
1896+
const parsed = parseCssVirtual(id)
1897+
if (parsed?.type === 'rsc') {
1898+
assert(this.environment.name === 'rsc')
1899+
const importer = parsed.id
19081900
if (this.environment.mode === 'dev') {
19091901
const result = collectCss(server.environments.rsc!, importer)
19101902
const cssHrefs = result.hrefs.map((href) => href.slice(1))
19111903
const jsHrefs = [
1912-
'@id/__x00__virtual:vite-rsc/importer-resources-browser?importer=' +
1913-
encodeURIComponent(importer),
1904+
`@id/__x00__${toCssVirtual({ id: importer, type: 'rsc-browser' })}`,
19141905
]
19151906
const deps = assetsURLOfDeps(
19161907
{ css: cssHrefs, js: jsHrefs },
@@ -1936,16 +1927,10 @@ function vitePluginRscCss(
19361927
`
19371928
}
19381929
}
1939-
if (
1940-
id.startsWith(
1941-
'\0virtual:vite-rsc/importer-resources-browser?importer=',
1942-
)
1943-
) {
1930+
if (parsed?.type === 'rsc-browser') {
19441931
assert(this.environment.name === 'client')
19451932
assert(this.environment.mode === 'dev')
1946-
const importer = decodeURIComponent(
1947-
parseIdQuery(id).query['importer']!,
1948-
)
1933+
const importer = parsed.id
19491934
const result = collectCss(server.environments.rsc!, importer)
19501935
let code = result.ids
19511936
.map((id) => id.replace(/^\0/, ''))
@@ -1963,14 +1948,13 @@ function vitePluginRscCss(
19631948
const mods = collectModuleDependents(ctx.modules)
19641949
for (const mod of mods) {
19651950
if (mod.id) {
1966-
const importer = encodeURIComponent(mod.id)
19671951
invalidteModuleById(
19681952
server.environments.rsc!,
1969-
`\0virtual:vite-rsc/importer-resources?importer=${importer}`,
1953+
`\0` + toCssVirtual({ id: mod.id, type: 'rsc' }),
19701954
)
19711955
invalidteModuleById(
19721956
server.environments.client,
1973-
`\0virtual:vite-rsc/importer-resources-browser?importer=${importer}`,
1957+
`\0` + toCssVirtual({ id: mod.id, type: 'rsc-browser' }),
19741958
)
19751959
}
19761960
}

packages/plugin-rsc/src/plugins/cjs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { parseAstAsync, type Plugin } from 'vite'
2-
import { parseIdQuery } from './utils'
32
import { findClosestPkgJsonPath } from 'vitefu'
43
import path from 'node:path'
54
import fs from 'node:fs'
65
import * as esModuleLexer from 'es-module-lexer'
76
import { transformCjsToEsm } from '../transforms/cjs'
87
import { createDebug } from '@hiogawa/utils'
8+
import { parseIdQuery } from './shared'
99

1010
const debug = createDebug('vite-rsc:cjs')
1111

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
type CssVirtual = {
2+
id: string
3+
type: 'ssr' | 'rsc' | 'rsc-browser'
4+
}
5+
6+
export function toCssVirtual({ id, type }: CssVirtual) {
7+
// ensure other plugins treat it as a plain js file
8+
// e.g. https://github.com/vitejs/rolldown-vite/issues/372#issuecomment-3193401601
9+
return `virtual:vite-rsc/css?type=${type}&id=${encodeURIComponent(id)}&lang.js`
10+
}
11+
12+
export function parseCssVirtual(id: string): CssVirtual | undefined {
13+
if (id.startsWith('\0virtual:vite-rsc/css?')) {
14+
return parseIdQuery(id).query as any
15+
}
16+
}
17+
18+
// https://github.com/vitejs/vite-plugin-vue/blob/06931b1ea2b9299267374cb8eb4db27c0626774a/packages/plugin-vue/src/utils/query.ts#L13
19+
export function parseIdQuery(id: string): {
20+
filename: string
21+
query: {
22+
[k: string]: string
23+
}
24+
} {
25+
if (!id.includes('?')) return { filename: id, query: {} }
26+
const [filename, rawQuery] = id.split(`?`, 2) as [string, string]
27+
const query = Object.fromEntries(new URLSearchParams(rawQuery))
28+
return { filename, query }
29+
}

packages/plugin-rsc/src/plugins/utils.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,6 @@ export function evalValue<T = any>(rawValue: string): T {
1616
return fn()
1717
}
1818

19-
// https://github.com/vitejs/vite-plugin-vue/blob/06931b1ea2b9299267374cb8eb4db27c0626774a/packages/plugin-vue/src/utils/query.ts#L13
20-
export function parseIdQuery(id: string): {
21-
filename: string
22-
query: {
23-
[k: string]: string
24-
}
25-
} {
26-
if (!id.includes('?')) return { filename: id, query: {} }
27-
const [filename, rawQuery] = id.split(`?`, 2) as [string, string]
28-
const query = Object.fromEntries(new URLSearchParams(rawQuery))
29-
return { filename, query }
30-
}
31-
3219
// https://github.com/vitejs/vite/blob/946831f986cb797009b8178659d2b31f570c44ff/packages/vite/src/shared/utils.ts#L31-L34
3320
const postfixRE = /[?#].*$/
3421
export function cleanUrl(url: string): string {

packages/plugin-rsc/src/ssr.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as clientReferences from 'virtual:vite-rsc/client-references'
33
import * as ReactDOM from 'react-dom'
44
import { setRequireModule } from './core/ssr'
55
import type { ResolvedAssetDeps } from './plugin'
6+
import { toCssVirtual } from './plugins/shared'
67

78
export { createServerConsumerManifest } from './core/ssr'
89

@@ -16,7 +17,7 @@ function initialize(): void {
1617
if (!import.meta.env.__vite_rsc_build__) {
1718
const mod = await import(/* @vite-ignore */ id)
1819
const modCss = await import(
19-
/* @vite-ignore */ '/@id/__x00__virtual:vite-rsc/css/dev-ssr/' + id
20+
/* @vite-ignore */ '/@id/__x00__' + toCssVirtual({ id, type: 'ssr' })
2021
)
2122
return wrapResourceProxy(mod, { js: [], css: modCss.default })
2223
} else {

0 commit comments

Comments
 (0)