Skip to content

Commit eac0cc8

Browse files
authored
fix(bundled-dev): inject client script tag before chunk scripts (#23161)
1 parent 23b8a08 commit eac0cc8

3 files changed

Lines changed: 29 additions & 10 deletions

File tree

‎packages/vite/src/node/constants.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ export const BUNDLED_DEV_CLIENT_ENTRY: string = resolve(
133133
VITE_PACKAGE_DIR,
134134
'dist/client/bundledDevClient.mjs',
135135
)
136+
/** URL filename the bundled-dev server serves the vite client under */
137+
export const BUNDLED_DEV_CLIENT_FILENAME: string = 'bundledDevClient.mjs'
136138
export const ENV_ENTRY: string = resolve(
137139
VITE_PACKAGE_DIR,
138140
'dist/client/env.mjs',

‎packages/vite/src/node/plugins/html.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
} from '../utils'
3636
import type { ResolvedConfig } from '../config'
3737
import { checkPublicFile } from '../publicDir'
38+
import { BUNDLED_DEV_CLIENT_FILENAME } from '../constants'
3839
import { toOutputFilePathInHtml } from '../build'
3940
import { resolveEnvPrefix } from '../env'
4041
import { cleanUrl } from '../../shared/utils'
@@ -991,6 +992,30 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
991992
result = injectToHead(result, assetTags)
992993
}
993994

995+
// prepend dev client runtime for bundled client build before other chunk scripts
996+
if (
997+
config.command === 'serve' &&
998+
this.environment.config.consumer === 'client' &&
999+
this.environment.config.isBundled
1000+
) {
1001+
result = injectToHead(
1002+
result,
1003+
[
1004+
{
1005+
tag: 'script',
1006+
attrs: {
1007+
type: 'module',
1008+
src: path.posix.join(
1009+
config.base,
1010+
BUNDLED_DEV_CLIENT_FILENAME,
1011+
),
1012+
},
1013+
},
1014+
],
1015+
true,
1016+
)
1017+
}
1018+
9941019
// inject css link when cssCodeSplit is false
9951020
if (!this.environment.config.build.cssCodeSplit) {
9961021
const cssBundleName = cssBundleNameCache.get(config)

‎packages/vite/src/node/server/bundledDev.ts‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import path from 'node:path'
21
import { setTimeout } from 'node:timers/promises'
32
import {
43
type BindingClientHmrUpdate,
@@ -9,14 +8,14 @@ import type { RolldownOutput } from 'rolldown'
98
import colors from 'picocolors'
109
import getEtag from 'etag'
1110
import { ChunkMetadataMap, resolveRolldownOptions } from '../build'
11+
import { BUNDLED_DEV_CLIENT_FILENAME } from '../constants'
1212
import { getHmrImplementation } from '../plugins/clientInjections'
1313
import { createDebugger, formatAndTruncateFileList } from '../utils'
1414
import type { DevEnvironment } from './environment'
1515
import { type NormalizedHotChannelClient, debugHmr, getShortName } from './hmr'
1616
import { prepareError } from './middlewares/error'
1717

1818
const debug = createDebugger('vite:full-bundle-mode')
19-
const BUNDLED_DEV_CLIENT_FILENAME = 'bundledDevClient.mjs'
2019

2120
type HmrOutput = BindingClientHmrUpdate['update']
2221

@@ -375,15 +374,8 @@ export class BundledDev {
375374
}
376375
for (const outputFile of output) {
377376
this.memoryFiles.set(outputFile.fileName, () => {
378-
let source =
377+
const source =
379378
outputFile.type === 'chunk' ? outputFile.code : outputFile.source
380-
if (outputFile.type === 'chunk' && outputFile.isEntry) {
381-
const runtimePath = path.posix.join(
382-
this.environment.config.base,
383-
BUNDLED_DEV_CLIENT_FILENAME,
384-
)
385-
source = `import ${JSON.stringify(runtimePath)}\n${source}`
386-
}
387379
return {
388380
source,
389381
etag: getEtag(Buffer.from(source), { weak: true }),

0 commit comments

Comments
 (0)