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 8773916
Show file tree
Hide file tree
Showing 4 changed files with 40 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
31 changes: 31 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 @@ -232,6 +234,10 @@ class RolldownEnvironment extends DevEnvironment {
moduleTypes: {
'.css': 'js',
},
// TODO: isolating finalizer doesn't rewrite yet
// experimental: {
// resolveNewUrlToAsset: true,
// },
}
this.instance = await rolldown.rolldown(this.inputOptions)

Expand All @@ -254,6 +260,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 +285,12 @@ 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
// we should property run `renderChunk` to hmr chunk too
this.newModules[id] = current
}
if (current !== last) {
this.newModules[id] = current
}
Expand Down Expand Up @@ -426,6 +455,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
4 changes: 4 additions & 0 deletions playground/rolldown-dev-react/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import testStyleInline from './test-style-inline.css?inline'
// TODO: hmr for url assets?
import testStyleUrl from './test-style-url.css?url'

// TODO: isolating finalizer doesn't rewrite yet
// const testAssetTxt = new URL('./test-asset.txt', import.meta.url).href;
// console.log(testAssetTxt);

export function App() {
const [count, setCount] = React.useState(0)

Expand Down
1 change: 1 addition & 0 deletions playground/rolldown-dev-react/src/test-asset.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello

0 comments on commit 8773916

Please sign in to comment.