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: extract hmr chunk from chunk.modules #6

Open
wants to merge 11 commits into
base: feat-rolldown-dev
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
1 change: 1 addition & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default tseslint.config(
{
ignores: [
'packages/create-vite/template-*',
'packages/vite/misc/rolldown-runtime.js',
'**/dist/**',
'**/fixtures/**',
'**/playground-temp/**',
Expand Down
237 changes: 237 additions & 0 deletions packages/vite/misc/rolldown-runtime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
// based on
// https://github.com/rolldown/rolldown/blob/a29240168290e45b36fdc1a6d5c375281fb8dc3e/crates/rolldown/src/runtime/runtime-without-comments.js#L69
// https://github.com/hi-ogawa/rolldown/blob/27d203a74d8dd95aed256bde29232d535bd294f4/crates/rolldown/src/runtime/runtime-app.js

var __create = Object.create
var __defProp = Object.defineProperty
var __getOwnPropDesc = Object.getOwnPropertyDescriptor
var __getOwnPropNames = Object.getOwnPropertyNames
var __getProtoOf = Object.getPrototypeOf
var __hasOwnProp = Object.prototype.hasOwnProperty
var __esm = (fn, res) =>
function () {
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])((fn = 0))), res
}
var __esmMin = (fn, res) => () => (fn && (res = fn((fn = 0))), res)
var __commonJS = (cb, mod) =>
function () {
return (
mod ||
(0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod),
mod.exports
)
}
var __commonJSMin = (cb, mod) => () => (
mod || cb((mod = { exports: {} }).exports, mod), mod.exports
)
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true })
}
var __copyProps = (to, from, except, desc) => {
if ((from && typeof from === 'object') || typeof from === 'function')
for (
var keys = __getOwnPropNames(from), i = 0, n = keys.length, key;
i < n;
i++
) {
key = keys[i]
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, {
get: ((k) => from[k]).bind(null, key),
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable,
})
}
return to
}
var __reExport = (target, mod, secondTarget) => (
__copyProps(target, mod, 'default'),
secondTarget && __copyProps(secondTarget, mod, 'default')
)
var __toESM = (mod, isNodeMode, target) => (
(target = mod != null ? __create(__getProtoOf(mod)) : {}),
__copyProps(
isNodeMode || !mod || !mod.__esModule
? __defProp(target, 'default', { value: mod, enumerable: true })
: target,
mod,
)
)
var __toCommonJS = (mod) =>
__copyProps(__defProp({}, '__esModule', { value: true }), mod)
var __toBinaryNode = (base64) => new Uint8Array(Buffer.from(base64, 'base64'))
var __toBinary = /* @__PURE__ */ (() => {
var table = new Uint8Array(128)
for (var i = 0; i < 64; i++)
table[i < 26 ? i + 65 : i < 52 ? i + 71 : i < 62 ? i - 4 : i * 4 - 205] = i
return (base64) => {
var n = base64.length,
bytes = new Uint8Array(
(((n - (base64[n - 1] == '=') - (base64[n - 2] == '=')) * 3) / 4) | 0,
)
for (var i = 0, j = 0; i < n; ) {
var c0 = table[base64.charCodeAt(i++)],
c1 = table[base64.charCodeAt(i++)]
var c2 = table[base64.charCodeAt(i++)],
c3 = table[base64.charCodeAt(i++)]
bytes[j++] = (c0 << 2) | (c1 >> 4)
bytes[j++] = (c1 << 4) | (c2 >> 2)
bytes[j++] = (c2 << 6) | c3
}
return bytes
}
})()

var rolldown_runtime = (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
}
},
require: function (id) {
const parent =
this.executeModuleStack.length >= 1
? this.executeModuleStack[this.executeModuleStack.length - 1]
: null
if (this.moduleCache[id]) {
var module = this.moduleCache[id]
if (parent && module.parents.indexOf(parent) === -1) {
module.parents.push(parent)
}
return module.exports
}
var factory = this.moduleFactoryMap[id]
if (!factory) {
throw new Error('Module not found: ' + id)
}
var module = (this.moduleCache[id] = {
exports: {},
parents: parent ? [parent] : [],
hot: {
selfAccept: false,
acceptCallbacks: [],
accept: function (callback) {
this.selfAccept = true
if (callback && typeof callback === 'function') {
this.acceptCallbacks.push({
deps: [id],
callback,
})
}
},
},
})
this.executeModuleStack.push(id)
factory(this.require.bind(this), module, module.exports)
this.executeModuleStack.pop()
return module.exports
},
patch: function (updateModuleIds, callback) {
self.patching = true

callback()

var boundaries = []
var invalidModuleIds = []
var acceptCallbacks = []

for (var i = 0; i < updateModuleIds.length; i++) {
foundBoundariesAndInvalidModuleIds(
updateModuleIds[i],
boundaries,
invalidModuleIds,
acceptCallbacks,
)
}

for (var i = 0; i < invalidModuleIds.length; i++) {
var id = invalidModuleIds[i]
delete this.moduleCache[id]
}

for (var id in this.patchedModuleFactoryMap) {
this.moduleFactoryMap[id] = this.patchedModuleFactoryMap[id]
}
this.patchedModuleFactoryMap = {}

for (var i = 0; i < boundaries.length; i++) {
this.require(boundaries[i])
}

for (var i = 0; i < acceptCallbacks.length; i++) {
var item = acceptCallbacks[i]
item.callback.apply(
null,
item.deps.map((dep) => this.moduleCache[dep].exports),
)
}

self.patching = false

function foundBoundariesAndInvalidModuleIds(
updateModuleId,
boundaries,
invalidModuleIds,
acceptCallbacks,
) {
var queue = [{ moduleId: updateModuleId, chain: [updateModuleId] }]
var visited = {}

while (queue.length > 0) {
var item = queue.pop()
var moduleId = item.moduleId
var chain = item.chain

if (visited[moduleId]) {
continue
}

var module = rolldown_runtime.moduleCache[moduleId]
if (!module) {
continue
}

if (module.hot.selfAccept) {
if (boundaries.indexOf(moduleId) === -1) {
boundaries.push(moduleId)

for (var i = 0; i < module.hot.acceptCallbacks.length; i++) {
var item = module.hot.acceptCallbacks[i]
acceptCallbacks.push(item)
}
}
for (var i = 0; i < chain.length; i++) {
if (invalidModuleIds.indexOf(chain[i]) === -1) {
invalidModuleIds.push(chain[i])
}
}
continue
}

boundaries.push(moduleId)
invalidModuleIds.push(moduleId)
if (module.parents.length === 0) {
globalThis.window?.location.reload()
break
}

for (var i = 0; i < module.parents.length; i++) {
var parent = module.parents[i]
queue.push({
moduleId: parent,
chain: chain.concat([parent]),
})
}

visited[moduleId] = true
}
}
},
})
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": "file:../../rolldown-0.14.0.tgz",
"rolldown": "0.15.0",
"rollup": "^4.23.0"
},
"optionalDependencies": {
Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/node/__tests__/build.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ describe('build', () => {
assertOutputHashContentChange(result[0], result[1])
})

test('file hash should change when pure css chunk changes', async () => {
// https://github.com/rolldown/rolldown/issues/3049
test.skip('file hash should change when pure css chunk changes', async () => {
const buildProject = async (cssColor: string) => {
return (await build({
root: resolve(__dirname, 'packages/build-project'),
Expand Down
5 changes: 4 additions & 1 deletion packages/vite/src/node/plugins/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const assetCache = new WeakMap<Environment, Map<string, string>>()
export interface GeneratedAssetMeta {
originalFileName: string | undefined
isEntry?: boolean
content?: Buffer
}
export const generatedAssetsMap = new WeakMap<
Environment,
Expand Down Expand Up @@ -421,7 +422,9 @@ async function fileToBuiltUrl(
originalFileName,
source: content,
})
generatedAssetsMap.get(environment)!.set(referenceId, { originalFileName })
generatedAssetsMap
.get(environment)!
.set(referenceId, { originalFileName, content })

url = `__VITE_ASSET__${referenceId}__${postfix ? `$_${postfix}__` : ``}`
}
Expand Down
Loading
Loading