Skip to content

Commit

Permalink
wip: use rolldown rebuild
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Dec 4, 2024
1 parent 8773916 commit cfd6d17
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 30 deletions.
2 changes: 1 addition & 1 deletion packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
Binary file added packages/vite/rolldown-0.15.0.tgz
Binary file not shown.
65 changes: 41 additions & 24 deletions packages/vite/src/node/server/environments/rolldown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -135,7 +136,7 @@ export async function rolldownDevHandleHotUpdate(
//

class RolldownEnvironment extends DevEnvironment {
instance!: rolldown.RolldownBuild
instance!: Awaited<ReturnType<typeof rolldownExperimental.rebuild>>
result!: rolldown.RolldownOutput
outDir!: string
buildTimestamp = Date.now()
Expand Down Expand Up @@ -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 })
}
Expand Down Expand Up @@ -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
Expand All @@ -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[] = []
Expand Down Expand Up @@ -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<void> {
Expand Down Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cfd6d17

Please sign in to comment.