-
-
Notifications
You must be signed in to change notification settings - Fork 241
Expand file tree
/
Copy pathimport-asset.ts
More file actions
180 lines (167 loc) · 6.46 KB
/
import-asset.ts
File metadata and controls
180 lines (167 loc) · 6.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import assert from 'node:assert'
import path from 'node:path'
import MagicString from 'magic-string'
import { stripLiteral } from 'strip-literal'
import { normalizePath, type Plugin } from 'vite'
import type { RscPluginManager } from '../plugin'
import { evalValue } from './vite-utils'
// Virtual module prefix for entry asset wrappers in dev mode
const ASSET_ENTRY_VIRTUAL_PREFIX = 'virtual:vite-rsc/asset-entry/'
export type AssetImportMeta = {
resolvedId: string
sourceEnv: string
specifier: string
isEntry: boolean
}
export function vitePluginImportAsset(manager: RscPluginManager): Plugin[] {
return [
{
name: 'rsc:import-asset',
resolveId(source) {
// Handle virtual asset entry modules
if (source.startsWith(ASSET_ENTRY_VIRTUAL_PREFIX)) {
return '\0' + source
}
},
async load(id) {
// Handle virtual asset entry modules in dev mode
if (id.startsWith('\0' + ASSET_ENTRY_VIRTUAL_PREFIX)) {
assert(this.environment.mode === 'dev')
const resolvedId = id.slice(
('\0' + ASSET_ENTRY_VIRTUAL_PREFIX).length,
)
let code = ''
// Enable HMR only when react plugin is available
const resolved = await this.resolve('/@react-refresh')
if (resolved) {
code += `
import RefreshRuntime from "/@react-refresh";
RefreshRuntime.injectIntoGlobalHook(window);
window.$RefreshReg$ = () => {};
window.$RefreshSig$ = () => (type) => type;
window.__vite_plugin_react_preamble_installed__ = true;
`
}
code += `await import(${JSON.stringify(resolvedId)});`
// Server CSS cleanup on HMR
code += /* js */ `
const ssrCss = document.querySelectorAll("link[rel='stylesheet']");
import.meta.hot.on("vite:beforeUpdate", () => {
ssrCss.forEach(node => {
if (node.dataset.precedence?.startsWith("vite-rsc/client-references")) {
node.remove();
}
});
});
`
// Close error overlay after syntax error is fixed
code += `
import.meta.hot.on("rsc:update", () => {
document.querySelectorAll("vite-error-overlay").forEach((n) => n.close())
});
`
return code
}
},
buildStart() {
// Emit discovered entries during build
if (this.environment.mode !== 'build') return
if (this.environment.name !== 'client') return
// Collect unique entries targeting client environment
const emitted = new Set<string>()
for (const metas of Object.values(manager.assetImportMetaMap)) {
for (const meta of Object.values(metas)) {
if (meta.isEntry && !emitted.has(meta.resolvedId)) {
emitted.add(meta.resolvedId)
this.emitFile({
type: 'chunk',
id: meta.resolvedId,
})
}
}
}
},
transform: {
async handler(code, id) {
if (!code.includes('import.meta.viteRsc.importAsset')) return
const { server, config } = manager
const s = new MagicString(code)
for (const match of stripLiteral(code).matchAll(
/import\.meta\.viteRsc\.importAsset\s*\(([\s\S]*?)\)/dg,
)) {
const [argStart, argEnd] = match.indices![1]!
const argCode = code.slice(argStart, argEnd).trim()
// Parse: ('./entry.browser.tsx', { entry: true })
const [specifier, options]: [string, { entry?: boolean }?] =
evalValue(`[${argCode}]`)
const isEntry = options?.entry ?? false
// Resolve specifier relative to importer against client environment
let resolvedId: string
if (this.environment.mode === 'dev') {
const clientEnv = server.environments.client
assert(clientEnv, `[vite-rsc] client environment not found`)
const resolved = await clientEnv.pluginContainer.resolveId(
specifier,
id,
)
assert(
resolved,
`[vite-rsc] failed to resolve '${specifier}' for client environment`,
)
resolvedId = resolved.id
} else {
// Build mode: resolve in client environment config
const clientEnvConfig = config.environments.client
assert(clientEnvConfig, `[vite-rsc] client environment not found`)
// Use this environment's resolver for now
const resolved = await this.resolve(specifier, id)
assert(
resolved,
`[vite-rsc] failed to resolve '${specifier}' for client environment`,
)
resolvedId = resolved.id
}
// Track discovered asset, keyed by [sourceEnv][resolvedId]
const sourceEnv = this.environment.name
manager.assetImportMetaMap[sourceEnv] ??= {}
manager.assetImportMetaMap[sourceEnv]![resolvedId] = {
resolvedId,
sourceEnv,
specifier,
isEntry,
}
let replacement: string
if (this.environment.mode === 'dev') {
if (isEntry) {
// Dev + entry: use virtual wrapper with HMR support
const virtualId = ASSET_ENTRY_VIRTUAL_PREFIX + resolvedId
const url = config.base + '@id/__x00__' + virtualId
replacement = `Promise.resolve({ url: ${JSON.stringify(url)} })`
} else {
// Dev + non-entry: compute URL directly
const relativePath = normalizePath(
path.relative(config.root, resolvedId),
)
const url = config.base + relativePath
replacement = `Promise.resolve({ url: ${JSON.stringify(url)} })`
}
} else {
// Build: use existing assets manifest
// Use relative ID for stable builds across different machines
const relativeId = manager.toRelativeId(resolvedId)
replacement = `(async () => (await import("virtual:vite-rsc/assets-manifest")).default.importAssets[${JSON.stringify(relativeId)}])()`
}
const [start, end] = match.indices![0]!
s.overwrite(start, end, replacement)
}
if (s.hasChanged()) {
return {
code: s.toString(),
map: s.generateMap({ hires: 'boundary' }),
}
}
},
},
},
]
}