Skip to content

Commit

Permalink
wip: asset url hmr
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Dec 3, 2024
1 parent 7290436 commit 30ed368
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/vite/src/node/plugins/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const assetCache = new WeakMap<Environment, Map<string, string>>()
export interface GeneratedAssetMeta {
originalFileName: string | undefined
isEntry?: boolean
content?: Buffer
}
export const generatedAssetsMap = new WeakMap<
Environment,
Expand Down Expand Up @@ -421,7 +422,9 @@ async function fileToBuiltUrl(
originalFileName,
source: content,
})
generatedAssetsMap.get(environment)!.set(referenceId, { originalFileName })
generatedAssetsMap
.get(environment)!
.set(referenceId, { originalFileName, content })

url = `__VITE_ASSET__${referenceId}__${postfix ? `$_${postfix}__` : ``}`
}
Expand Down
25 changes: 25 additions & 0 deletions packages/vite/src/node/server/environments/rolldown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type {
import { CLIENT_ENTRY, VITE_PACKAGE_DIR } from '../../constants'
import { injectEnvironmentToHooks } from '../../build'
import { cleanUrl } from '../../../shared/utils'
import { generatedAssetsMap } from '../../plugins/asset'

const require = createRequire(import.meta.url)

Expand Down Expand Up @@ -142,6 +143,7 @@ class RolldownEnvironment extends DevEnvironment {
outputOptions!: rolldown.OutputOptions
lastModules: Record<string, string | null> = {}
newModules: Record<string, string | null> = {}
lastAssets: Record<string, string> = {}
fileModuleIds = new Set<string>()
buildPromise?: Promise<void>

Expand Down Expand Up @@ -254,6 +256,23 @@ class RolldownEnvironment extends DevEnvironment {
// `generate` should work but we use `write` so it's easier to see output and debug
this.result = await this.instance.write(this.outputOptions)

// find changed assets
const changedAssets: string[] = []
for (const [id, { content }] of generatedAssetsMap.get(this) ?? []) {
if (content) {
const data = content.toString('utf8')
if (this.lastAssets[id] !== data) {
changedAssets.push(id)
}
this.lastAssets[id] = data
}
}
// detect change of content of assert url placeholder __VITE_ASSET__xxx
const changedAssetsRegex = new RegExp(
// eslint-disable-next-line
`__VITE_ASSET__(${changedAssets.join('|')})__`,
)

// extract hmr chunk
// cf. https://github.com/web-infra-dev/rspack/blob/5a967f7a10ec51171a304a1ce8d741bd09fa8ed5/crates/rspack_plugin_hmr/src/lib.rs#L60
const chunk = this.result.output[0]
Expand All @@ -262,6 +281,10 @@ class RolldownEnvironment extends DevEnvironment {
for (const [id, mod] of Object.entries(chunk.modules)) {
const current = mod.code
const last = this.lastModules?.[id]
if (current?.match(changedAssetsRegex)) {
// TODO: need to replace __VITE_ASSET__xxx
this.newModules[id] = current
}
if (current !== last) {
this.newModules[id] = current
}
Expand Down Expand Up @@ -426,6 +449,8 @@ function patchRuntimePlugin(environment: RolldownEnvironment): rolldown.Plugin {
},
},
renderChunk(code, chunk) {
// TODO: this magic string is heavy

// silly but we can do `render_app` on our own for now
// https://github.com/rolldown/rolldown/blob/a29240168290e45b36fdc1a6d5c375281fb8dc3e/crates/rolldown/src/ecmascript/format/app.rs#L28-L55
const output = new MagicString(code)
Expand Down

0 comments on commit 30ed368

Please sign in to comment.