Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: simplify runtime globals #15

Open
wants to merge 1 commit into
base: refactor-runtime
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading