Skip to content

Commit

Permalink
refactor: simplify runtime globals
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Dec 14, 2024
1 parent bf8fbf7 commit 005c9bb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
6 changes: 6 additions & 0 deletions packages/vite/misc/rolldown-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ var __rolldown_runtime = {
exports: {},
parents: parent ? [parent] : [],
hot: {
...__rolldown_runtime.hot,
selfAccept: false,
acceptCallbacks: [],
accept: function (callback) {
Expand All @@ -146,6 +147,7 @@ var __rolldown_runtime = {
exports: module.exports,
require: this.require.bind(this),
ensureChunk: this.ensureChunk.bind(this),
updateStyle: this.updateStyle.bind(this),
__toCommonJS,
__toESM,
__export,
Expand Down Expand Up @@ -278,4 +280,8 @@ var __rolldown_runtime = {
const file = this.manifest.chunks[name].file
await import(`/` + file)
},

// injected via getRolldownClientCode
hot: {},
updateStyle() {},
}
27 changes: 8 additions & 19 deletions packages/vite/src/node/server/environments/rolldown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ export function rolldownDevHandleConfig(
experimental: {
enableNativePlugin: true,
},
define: {
'import.meta.hot': 'module.hot',
},
environments: {
client: {
dev: {
Expand Down Expand Up @@ -233,21 +236,6 @@ class RolldownEnvironment extends DevEnvironment {
patchRuntimePlugin(this),
patchCssPlugin(),
reactRefreshPlugin(),
{
// TODO: import.meta not supported by app format
name: 'patch-import-meta',
transform: {
filter: {
code: [/import\.meta\.hot/],
},
handler(code) {
const output = new MagicString(code)
output.replaceAll('import.meta.hot.accept', 'module.hot.accept')
output.replaceAll('import.meta.hot.on', 'self.__rolldown_hot.on')
return { code: output.toString(), map: output.generateMap() }
},
},
},
],
moduleTypes: {
'.css': 'js',
Expand Down Expand Up @@ -612,8 +600,8 @@ function patchCssPlugin(): rolldown.Plugin {
// TODO: import.meta.hot.prune
const cssCode = code.match(/^const __vite__css = (.*)$/m)![1]
const jsCode = `
__rolldown_updateStyle(${JSON.stringify(id)}, ${cssCode});
module.hot.accept();
__rolldown_runtime.updateStyle(${JSON.stringify(id)}, ${cssCode});
import.meta.hot.accept();
`
return { code: jsCode, moduleSideEffects: true }
},
Expand Down Expand Up @@ -655,8 +643,9 @@ hot.on("rolldown:hmr", (data) => {
import("/" + data.fileName + "?t=" + Date.now());
}
});
self.__rolldown_hot = hot;
self.__rolldown_updateStyle = updateStyle;
hot.on = hot.on.bind(hot)
__rolldown_runtime.hot = hot;
__rolldown_runtime.updateStyle = updateStyle;
`
return `\n;(() => {/*** @vite/client ***/\n${code}}\n)();\n`
}
Expand Down

0 comments on commit 005c9bb

Please sign in to comment.