Skip to content

Commit

Permalink
wip: new runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Dec 8, 2024
1 parent 23498cb commit b7f5f78
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 40 deletions.
58 changes: 31 additions & 27 deletions packages/vite/misc/rolldown-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,33 +82,27 @@ var __toBinary = /* @__PURE__ */ (() => {
}
})()

// TODO: for now need to expose these utilities used by IsolatingModuleFinalizermodule
self.__toCommonJS = __toCommonJS
self.__toESM = __toESM
self.__export = __export
self.__reExport = __reExport

var rolldown_runtime = (self.rolldown_runtime = {
patching: false,
patchedModuleFactoryMap: {},
self.__rolldown_runtime = {
// patching: false,
// patchedModuleFactoryMap: {},
executeModuleStack: [],
moduleCache: {},
moduleFactoryMap: {},
define: function (id, factory) {
if (self.patching) {
this.patchedModuleFactoryMap[id] = factory
} else {
this.moduleFactoryMap[id] = factory
}
modules(modules) {
Object.assign(this.moduleFactoryMap, modules)
},
// define: function (id, factory) {
// if (this.patching) {
// this.patchedModuleFactoryMap[id] = factory
// } else {
// this.moduleFactoryMap[id] = factory
// }
// },
require: function (id) {
const parent =
this.executeModuleStack.length >= 1
? this.executeModuleStack[this.executeModuleStack.length - 1]
: null
const parent = this.executeModuleStack.at(-1)
if (this.moduleCache[id]) {
var module = this.moduleCache[id]
if (parent && module.parents.indexOf(parent) === -1) {
if (parent && !module.parents.includes(parent)) {
module.parents.push(parent)
}
return module.exports
Expand Down Expand Up @@ -139,14 +133,22 @@ var rolldown_runtime = (self.rolldown_runtime = {
},
})
this.executeModuleStack.push(id)
factory(this.require.bind(this), module, module.exports)
factory({
require: this.require.bind(this),
module,
exports: module.exports,
__toCommonJS,
__toESM,
__export,
__reExport,
})
this.executeModuleStack.pop()
return module.exports
},
patch: function (updateModuleIds, callback) {
self.patching = true
patch: function (updateModuleIds) {
// this.patching = true

callback()
// callback()

var boundaries = []
var invalidModuleIds = []
Expand All @@ -158,6 +160,7 @@ var rolldown_runtime = (self.rolldown_runtime = {
boundaries,
invalidModuleIds,
acceptCallbacks,
this.moduleCache,
)
}

Expand All @@ -183,13 +186,14 @@ var rolldown_runtime = (self.rolldown_runtime = {
)
}

self.patching = false
this.patching = false

function foundBoundariesAndInvalidModuleIds(
updateModuleId,
boundaries,
invalidModuleIds,
acceptCallbacks,
moduleCache,
) {
var queue = [{ moduleId: updateModuleId, chain: [updateModuleId] }]
var visited = {}
Expand All @@ -203,7 +207,7 @@ var rolldown_runtime = (self.rolldown_runtime = {
continue
}

var module = rolldown_runtime.moduleCache[moduleId]
var module = moduleCache[moduleId]
if (!module) {
continue
}
Expand Down Expand Up @@ -244,4 +248,4 @@ var rolldown_runtime = (self.rolldown_runtime = {
}
}
},
})
}
1 change: 1 addition & 0 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ export function oxcResolvePlugin(
const filteredNoExternal = noExternal as true | string[]

return viteResolvePlugin({
dedupe: [],
resolveOptions: {
isBuild: options.isBuild,
isProduction: options.isProduction,
Expand Down
23 changes: 10 additions & 13 deletions packages/vite/src/node/server/environments/rolldown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,18 +307,15 @@ class RolldownEnvironment extends DevEnvironment {
}
assert(chunk.type === 'chunk')
const { output, stableIds } = patchIsolatedModuleChunk(chunk.code)
output.prepend(
`self.rolldown_runtime.patch(${JSON.stringify(stableIds)}, function(){\n`,
)
output.append('\n});')
output.replace(/^\/\/# sourceMappingURL=.*/gm, '')
output.append(`__rolldown_runtime.patch(${JSON.stringify(stableIds)});\n`)
let outputString = output.toString()
if (chunk.map) {
// collapse sourcemap
const map = combineSourcemaps(chunk.fileName, [
output.generateMap({ hires: 'boundary' }) as any,
chunk.map as any,
])
outputString = outputString.replace(/^\/\/# sourceMappingURL=.*/gm, '')
outputString += `\n//# sourceMappingURL=${genSourceMapUrl(map as any)}`
}
return [updatePath, outputString]
Expand Down Expand Up @@ -394,13 +391,13 @@ function patchIsolatedModuleChunk(code: string) {
for (const match of matches) {
const stableId = match[1]!
stableIds.push(stableId)
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 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`)
}
return { output, stableIds }
}
Expand Down Expand Up @@ -464,7 +461,7 @@ function patchRuntimePlugin(environment: RolldownEnvironment): rolldown.Plugin {
chunk.facadeModuleId,
)
output.append(
`\nrolldown_runtime.require(${JSON.stringify(stableId)});\n`,
`\n__rolldown_runtime.require(${JSON.stringify(stableId)});\n`,
)

// inject runtime
Expand Down

0 comments on commit b7f5f78

Please sign in to comment.