Skip to content

Commit c580d0c

Browse files
committed
feat: add experimental.fullBundleMode option
1 parent c7faa65 commit c580d0c

27 files changed

+1347
-1283
lines changed

packages/vite/src/client/client.ts

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
} from '../shared/moduleRunnerTransport'
99
import { ErrorOverlay, overlayId } from './overlay'
1010
import './hmrModuleRunner'
11-
// import '@vite/env'
11+
import '@vite/env'
1212

1313
// injected by the hmr plugin when served
1414
declare const __BASE__: string
@@ -21,6 +21,7 @@ declare const __HMR_BASE__: string
2121
declare const __HMR_TIMEOUT__: number
2222
declare const __HMR_ENABLE_OVERLAY__: boolean
2323
declare const __WS_TOKEN__: string
24+
declare const __FULL_BUNDLE_MODE__: boolean
2425

2526
console.debug('[vite] connecting...')
2627

@@ -143,15 +144,34 @@ const hmrClient = new HMRClient(
143144
async function importUpdatedModule({
144145
url,
145146
acceptedPath,
146-
// timestamp,
147-
// explicitImportRequired,
147+
timestamp,
148+
explicitImportRequired,
148149
isWithinCircularImport,
149150
}) {
150-
// const [acceptedPathWithoutQuery, query] = acceptedPath.split(`?`)
151-
const importPromise = import(
152-
/* @vite-ignore */
153-
base + url
154-
)
151+
function importModuleWithFullBundleMode() {
152+
const importPromise = import(base + url)
153+
return importPromise.then(() =>
154+
// @ts-expect-error globalThis.__rolldown_runtime__
155+
globalThis.__rolldown_runtime__.loadExports(acceptedPath),
156+
)
157+
}
158+
159+
function importModule() {
160+
const [acceptedPathWithoutQuery, query] = acceptedPath.split(`?`)
161+
const importPromise = import(
162+
/* @vite-ignore */
163+
base +
164+
acceptedPathWithoutQuery.slice(1) +
165+
`?${explicitImportRequired ? 'import&' : ''}t=${timestamp}${
166+
query ? `&${query}` : ''
167+
}`
168+
)
169+
return importPromise
170+
}
171+
172+
const importPromise = __FULL_BUNDLE_MODE__
173+
? importModuleWithFullBundleMode()
174+
: importModule()
155175
if (isWithinCircularImport) {
156176
importPromise.catch(() => {
157177
console.info(
@@ -161,10 +181,7 @@ const hmrClient = new HMRClient(
161181
pageReload()
162182
})
163183
}
164-
// @ts-expect-error globalThis.__rolldown_runtime__
165-
return await importPromise.then(() =>
166-
globalThis.__rolldown_runtime__.loadExports(acceptedPath),
167-
)
184+
return await importPromise
168185
},
169186
)
170187
transport.connect!(handleMessage)

packages/vite/src/client/env.ts

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
declare const __DEFINES__: Record<string, any>
2+
declare const __FULL_BUNDLE_MODE__: boolean
23

3-
const context = (() => {
4-
if (typeof globalThis !== 'undefined') {
5-
return globalThis
6-
} else if (typeof self !== 'undefined') {
7-
return self
8-
} else if (typeof window !== 'undefined') {
9-
return window
10-
} else {
11-
return Function('return this')()
12-
}
13-
})()
14-
15-
// assign defines
16-
const defines = __DEFINES__
17-
Object.keys(defines).forEach((key) => {
18-
const segments = key.split('.')
19-
let target = context
20-
for (let i = 0; i < segments.length; i++) {
21-
const segment = segments[i]
22-
if (i === segments.length - 1) {
23-
target[segment] = defines[key]
4+
if (!__FULL_BUNDLE_MODE__) {
5+
const context = (() => {
6+
if (typeof globalThis !== 'undefined') {
7+
return globalThis
8+
} else if (typeof self !== 'undefined') {
9+
return self
10+
} else if (typeof window !== 'undefined') {
11+
return window
2412
} else {
25-
target = target[segment] || (target[segment] = {})
13+
return Function('return this')()
14+
}
15+
})()
16+
17+
// assign defines
18+
const defines = __DEFINES__
19+
Object.keys(defines).forEach((key) => {
20+
const segments = key.split('.')
21+
let target = context
22+
for (let i = 0; i < segments.length; i++) {
23+
const segment = segments[i]
24+
if (i === segments.length - 1) {
25+
target[segment] = defines[key]
26+
} else {
27+
target = target[segment] || (target[segment] = {})
28+
}
2629
}
27-
}
28-
})
30+
})
31+
}
Lines changed: 67 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,83 @@
11
import { createHotContext } from './client'
22

3-
class DevRuntime {
4-
modules: Record<string, { exports: any }> = {}
3+
declare const __FULL_BUNDLE_MODE__: boolean
54

6-
static getInstance() {
7-
// @ts-expect-error __rolldown_runtime__
8-
let instance = globalThis.__rolldown_runtime__
9-
if (!instance) {
10-
instance = new DevRuntime()
5+
if (__FULL_BUNDLE_MODE__) {
6+
class DevRuntime {
7+
modules: Record<string, { exports: any }> = {}
8+
9+
static getInstance() {
1110
// @ts-expect-error __rolldown_runtime__
12-
globalThis.__rolldown_runtime__ = instance
11+
let instance = globalThis.__rolldown_runtime__
12+
if (!instance) {
13+
instance = new DevRuntime()
14+
// @ts-expect-error __rolldown_runtime__
15+
globalThis.__rolldown_runtime__ = instance
16+
}
17+
return instance
1318
}
14-
return instance
15-
}
1619

17-
createModuleHotContext(moduleId: string) {
18-
return createHotContext(moduleId)
19-
}
20+
createModuleHotContext(moduleId: string) {
21+
return createHotContext(moduleId)
22+
}
2023

21-
applyUpdates(_boundaries: string[]) {
22-
// trigger callbacks of accept() correctly
23-
// for (const moduleId of boundaries) {
24-
// const hotContext = this.moduleHotContexts.get(moduleId);
25-
// if (hotContext) {
26-
// const acceptCallbacks = hotContext.acceptCallbacks;
27-
// acceptCallbacks.filter((cb) => {
28-
// cb.fn(this.modules[moduleId].exports);
29-
// })
30-
// }
31-
// }
32-
// this.moduleHotContextsToBeUpdated.forEach((hotContext, moduleId) => {
33-
// this.moduleHotContexts[moduleId] = hotContext;
34-
// })
35-
// this.moduleHotContextsToBeUpdated.clear()
36-
// swap new contexts
37-
}
24+
applyUpdates(_boundaries: string[]) {
25+
//
26+
}
3827

39-
registerModule(
40-
id: string,
41-
esmExportGettersOrCjsExports: Record<string, () => any>,
42-
meta: { cjs?: boolean } = {},
43-
) {
44-
const exports = {}
45-
Object.keys(esmExportGettersOrCjsExports).forEach((key) => {
46-
if (
47-
Object.prototype.hasOwnProperty.call(esmExportGettersOrCjsExports, key)
48-
) {
49-
if (meta.cjs) {
50-
Object.defineProperty(exports, key, {
51-
enumerable: true,
52-
get: () => esmExportGettersOrCjsExports[key],
53-
})
54-
} else {
55-
Object.defineProperty(exports, key, {
56-
enumerable: true,
57-
get: esmExportGettersOrCjsExports[key],
58-
})
28+
registerModule(
29+
id: string,
30+
esmExportGettersOrCjsExports: Record<string, () => any>,
31+
meta: { cjs?: boolean } = {},
32+
) {
33+
const exports = {}
34+
Object.keys(esmExportGettersOrCjsExports).forEach((key) => {
35+
if (
36+
Object.prototype.hasOwnProperty.call(
37+
esmExportGettersOrCjsExports,
38+
key,
39+
)
40+
) {
41+
if (meta.cjs) {
42+
Object.defineProperty(exports, key, {
43+
enumerable: true,
44+
get: () => esmExportGettersOrCjsExports[key],
45+
})
46+
} else {
47+
Object.defineProperty(exports, key, {
48+
enumerable: true,
49+
get: esmExportGettersOrCjsExports[key],
50+
})
51+
}
52+
}
53+
})
54+
if (this.modules[id]) {
55+
this.modules[id] = {
56+
exports,
57+
}
58+
} else {
59+
// If the module is not in the cache, we need to register it.
60+
this.modules[id] = {
61+
exports,
5962
}
60-
}
61-
})
62-
if (this.modules[id]) {
63-
this.modules[id] = {
64-
exports,
65-
}
66-
} else {
67-
// If the module is not in the cache, we need to register it.
68-
this.modules[id] = {
69-
exports,
7063
}
7164
}
72-
}
7365

74-
loadExports(id: string) {
75-
const module = this.modules[id]
76-
if (module) {
77-
return module.exports
78-
} else {
79-
console.warn(`Module ${id} not found`)
80-
return {}
66+
loadExports(id: string) {
67+
const module = this.modules[id]
68+
if (module) {
69+
return module.exports
70+
} else {
71+
console.warn(`Module ${id} not found`)
72+
return {}
73+
}
8174
}
75+
76+
// __esmMin
77+
// createEsmInitializer = (fn, res) => () => (fn && (res = fn(fn = 0)), res)
78+
// __commonJSMin
79+
// createCjsInitializer = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports)
8280
}
8381

84-
// __esmMin
85-
// createEsmInitializer = (fn, res) => () => (fn && (res = fn(fn = 0)), res)
86-
// __commonJSMin
87-
// createCjsInitializer = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports)
82+
DevRuntime.getInstance()
8883
}
89-
90-
DevRuntime.getInstance()
91-
92-
// export function loadScript(url: string): void {
93-
// const script = document.createElement('script');
94-
// script.src = url;
95-
// script.type = 'module';
96-
// script.onerror = function () {
97-
// console.error('Failed to load script: ' + url);
98-
// }
99-
// document.body.appendChild(script);
100-
// }

packages/vite/src/node/build.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ async function buildEnvironment(
571571
environment: BuildEnvironment,
572572
server?: ViteDevServer,
573573
): Promise<RolldownOutput | RolldownOutput[] /* | RollupWatcher */> {
574-
const { root, packageCache, mode } = environment.config
574+
const { root, packageCache } = environment.config
575575
const options = environment.config.build
576576
const libOptions = options.lib
577577
const { logger } = environment
@@ -661,11 +661,6 @@ async function buildEnvironment(
661661
implement: await getHmrImplement(environment.config),
662662
}
663663
: false,
664-
// hmr: true,
665-
// hmr: server ? {
666-
// host: server._currentServerHost!,
667-
// port: server._currentServerPort!,
668-
// } : false,
669664
},
670665
}
671666

@@ -810,13 +805,11 @@ async function buildEnvironment(
810805
(isSsrTargetWebworkerEnvironment &&
811806
(typeof input === 'string' || Object.keys(input).length === 1)),
812807
minify:
813-
mode === 'production'
814-
? options.minify === 'oxc'
815-
? true
816-
: options.minify === false
817-
? 'dce-only'
818-
: false
819-
: false,
808+
options.minify === 'oxc'
809+
? true
810+
: options.minify === false
811+
? 'dce-only'
812+
: false,
820813
...output,
821814
}
822815
}
@@ -943,6 +936,7 @@ async function buildEnvironment(
943936
url,
944937
path: boundary.boundary,
945938
acceptedPath: boundary.acceptedVia,
939+
timestamp: 0,
946940
}
947941
}),
948942
})

0 commit comments

Comments
 (0)