diff --git a/packages/vite/package.json b/packages/vite/package.json index 665032c1eeca5b..f9adad2fc70f63 100644 --- a/packages/vite/package.json +++ b/packages/vite/package.json @@ -88,7 +88,7 @@ "esbuild": "^0.24.0", "postcss": "^8.4.49", "react-refresh": "^0.14.2", - "rolldown": "0.15.0", + "rolldown": "./rolldown-0.15.0.tgz", "rollup": "^4.23.0" }, "optionalDependencies": { diff --git a/packages/vite/rolldown-0.15.0.tgz b/packages/vite/rolldown-0.15.0.tgz new file mode 100644 index 00000000000000..964a740dc67c46 Binary files /dev/null and b/packages/vite/rolldown-0.15.0.tgz differ diff --git a/packages/vite/src/node/server/environments/rolldown.ts b/packages/vite/src/node/server/environments/rolldown.ts index b571e3b3943d5e..f4840a73c27fd8 100644 --- a/packages/vite/src/node/server/environments/rolldown.ts +++ b/packages/vite/src/node/server/environments/rolldown.ts @@ -5,7 +5,8 @@ import { createRequire } from 'node:module' import path from 'node:path' import { pathToFileURL } from 'node:url' import MagicString from 'magic-string' -import * as rolldown from 'rolldown' +import type * as rolldown from 'rolldown' +import * as rolldownExperimental from 'rolldown/experimental' import sirv from 'sirv' import { createLogger } from '../../publicUtils' import { DevEnvironment } from '../environment' @@ -135,7 +136,7 @@ export async function rolldownDevHandleHotUpdate( // class RolldownEnvironment extends DevEnvironment { - instance!: rolldown.RolldownBuild + instance!: Awaited> result!: rolldown.RolldownOutput outDir!: string buildTimestamp = Date.now() @@ -186,8 +187,6 @@ class RolldownEnvironment extends DevEnvironment { return } - await this.instance?.close() - if (this.config.build.emptyOutDir !== false) { fs.rmSync(this.outDir, { recursive: true, force: true }) } @@ -239,7 +238,6 @@ class RolldownEnvironment extends DevEnvironment { // resolveNewUrlToAsset: true, // }, } - this.instance = await rolldown.rolldown(this.inputOptions) const format: rolldown.ModuleFormat = this.name === 'client' || this.rolldownDevOptions.ssrModuleRunner @@ -257,8 +255,15 @@ class RolldownEnvironment extends DevEnvironment { ? `import __nodeModule from "node:module"; const require = __nodeModule.createRequire(import.meta.url);` : undefined, } + + this.instance = await rolldownExperimental.rebuild({ + ...this.inputOptions, + output: this.outputOptions, + }) + this.result = await this.instance.build() + // `generate` should work but we use `write` so it's easier to see output and debug - this.result = await this.instance.write(this.outputOptions) + // this.result = await this.instance.write(this.outputOptions) // find changed assets const changedAssets: string[] = [] @@ -305,27 +310,36 @@ class RolldownEnvironment extends DevEnvironment { async buildHmr(file: string) { logger.info(`hmr '${file}'`, { timestamp: true }) - await this.build() + const result = await this.instance.build() + const chunk = result.output.find( + (v) => v.type === 'chunk' && v.name === 'hmr-update', + ) + const updatePath = path.join(this.outDir, 'hmr-update.js') + if (!chunk) { + return [updatePath, ''] + } + assert(chunk.type === 'chunk') + const code = chunk.code + const output = new MagicString(code) + // extract isolated module between #region and #endregion + const matches = chunk.code.matchAll(/^\/\/#region (.*)$/gm) const stableIds: string[] = [] - let innerCode = '' - for (const [id, code] of Object.entries(this.newModules)) { - const stableId = path.relative(this.config.root, id) + for (const match of matches) { + const stableId = match[1]! stableIds.push(stableId) - innerCode += `\ - rolldown_runtime.define(${JSON.stringify(stableId)},function(require, module, exports){ - ${code} - }); -` + const start = match.index! + const end = code.indexOf('//#endregion', match.index) + output.appendLeft( + start, + `rolldown_runtime.define(${JSON.stringify(stableId)},function(require, module, exports){\n\n`, + ) + output.appendRight(end, `\n\n});\n`) } - const output = `\ -self.rolldown_runtime.patch(${JSON.stringify(stableIds)}, function(){ -${innerCode} -}); -` - // dump for debugging - const updatePath = path.join(this.outDir, `hmr-update-${Date.now()}.js`) - fs.writeFileSync(updatePath, output) - return [updatePath, output] + output.prepend( + `self.rolldown_runtime.patch(${JSON.stringify(stableIds)}, function(){\n`, + ) + output.append('\n});') + return [updatePath, output.toString()] } async handleUpdate(ctx: HmrContext): Promise { @@ -455,6 +469,9 @@ function patchRuntimePlugin(environment: RolldownEnvironment): rolldown.Plugin { }, }, renderChunk(code, chunk) { + if (!chunk.isEntry) { + return + } // TODO: this magic string is heavy // silly but we can do `render_app` on our own for now diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0a42ee0c9ac21b..45952cb86a3670 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -233,8 +233,8 @@ importers: specifier: ^0.14.2 version: 0.14.2 rolldown: - specifier: 0.15.0 - version: 0.15.0(@babel/runtime@7.26.0) + specifier: ./rolldown-0.15.0.tgz + version: file:packages/vite/rolldown-0.15.0.tgz(@babel/runtime@7.26.0) rollup: specifier: ^4.23.0 version: 4.24.4 @@ -6418,8 +6418,9 @@ packages: deprecated: Rimraf versions prior to v4 are no longer supported hasBin: true - rolldown@0.15.0: - resolution: {integrity: sha512-R+OnyuN+t91ZszrNmBRG/1rIPFEH4RuBGYX9lDnCNXvz74w9bxZxzpyUK0bOhxXiNeWPVoylrH/itJJAUDSTEw==} + rolldown@file:packages/vite/rolldown-0.15.0.tgz: + resolution: {integrity: sha512-E3+UoVR5jK1wx/tGU9bW8krY50YlgMu/HrzWUXVMCAcK6WxzZni3geyvB2SWk9KzjAkDJ77ftsDVS+ccgflsUQ==, tarball: file:packages/vite/rolldown-0.15.0.tgz} + version: 0.15.0 hasBin: true peerDependencies: '@babel/runtime': '>=7' @@ -12366,7 +12367,7 @@ snapshots: dependencies: glob: 7.2.3 - rolldown@0.15.0(@babel/runtime@7.26.0): + rolldown@file:packages/vite/rolldown-0.15.0.tgz(@babel/runtime@7.26.0): dependencies: zod: 3.23.8 optionalDependencies: