From 005c9bb0d20cf1605898867883bc273989822778 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Sat, 14 Dec 2024 12:40:04 +0900 Subject: [PATCH] refactor: simplify runtime globals --- packages/vite/misc/rolldown-runtime.js | 6 +++++ .../src/node/server/environments/rolldown.ts | 27 ++++++------------- 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/packages/vite/misc/rolldown-runtime.js b/packages/vite/misc/rolldown-runtime.js index b7914e635c921a..5e0930bfabc6e0 100644 --- a/packages/vite/misc/rolldown-runtime.js +++ b/packages/vite/misc/rolldown-runtime.js @@ -127,6 +127,7 @@ var __rolldown_runtime = { exports: {}, parents: parent ? [parent] : [], hot: { + ...__rolldown_runtime.hot, selfAccept: false, acceptCallbacks: [], accept: function (callback) { @@ -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, @@ -278,4 +280,8 @@ var __rolldown_runtime = { const file = this.manifest.chunks[name].file await import(`/` + file) }, + + // injected via getRolldownClientCode + hot: {}, + updateStyle() {}, } diff --git a/packages/vite/src/node/server/environments/rolldown.ts b/packages/vite/src/node/server/environments/rolldown.ts index 70a0d2d21d1112..ba7bb3ae90dc78 100644 --- a/packages/vite/src/node/server/environments/rolldown.ts +++ b/packages/vite/src/node/server/environments/rolldown.ts @@ -59,6 +59,9 @@ export function rolldownDevHandleConfig( experimental: { enableNativePlugin: true, }, + define: { + 'import.meta.hot': 'module.hot', + }, environments: { client: { dev: { @@ -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', @@ -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 } }, @@ -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` }