File size: 9,659 Bytes
8579f8a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | diff --git a/packages/plugin-legacy/src/index.ts b/packages/plugin-legacy/src/index.ts
index 7387a63aa..2ade9ba84 100644
--- a/packages/plugin-legacy/src/index.ts
+++ b/packages/plugin-legacy/src/index.ts
@@ -723,7 +723,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
// The legacy bundle is built first, and its index.html isn't actually emitted if
// modern bundle will be generated. Here we simply record its corresponding legacy chunk.
facadeToLegacyChunkMap.set(chunk.facadeModuleId, chunk.fileName)
- if (config.build.chunkImportMap) {
+ if (config.environments.client.build.chunkImportMap) {
facadeToLegacyImportMap.set(
chunk.facadeModuleId,
bundle![getImportMapFilename(config)]! as Rollup.OutputAsset,
@@ -773,7 +773,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
}
// 2. inject importmaps
- if (config.build.chunkImportMap) {
+ if (config.environments.client.build.chunkImportMap) {
const importMap = facadeToLegacyImportMap.get(chunk.facadeModuleId)!
const decoder = new TextDecoder()
tags.push({
diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts
index 28cc7f1cf..b61689a1c 100644
--- a/packages/vite/src/node/build.ts
+++ b/packages/vite/src/node/build.ts
@@ -685,6 +685,10 @@ export function resolveRolldownOptions(
viteMode: true,
chunkImportMap: options.chunkImportMap
? {
+ ...(typeof options.rolldownOptions.experimental?.chunkImportMap ===
+ 'object'
+ ? options.rolldownOptions.experimental?.chunkImportMap
+ : {}),
baseUrl: base,
}
: options.rolldownOptions.experimental?.chunkImportMap,
diff --git a/packages/vite/src/node/plugins/asset.ts b/packages/vite/src/node/plugins/asset.ts
index 35cd29ea8..b7a0a1b32 100644
--- a/packages/vite/src/node/plugins/asset.ts
+++ b/packages/vite/src/node/plugins/asset.ts
@@ -42,6 +42,7 @@ import {
} from '../../shared/utils'
import type { Environment } from '../environment'
import type { PartialEnvironment } from '../baseEnvironment'
+import { getImportMapFilename } from './html'
// referenceId is base64url but replaces - with $
export const assetUrlRE: RegExp = /__VITE_ASSET__([\w$]+)__(?:\$_(.*?)__)?/g
@@ -299,11 +300,17 @@ export function assetPlugin(config: ResolvedConfig): Plugin {
config.command === 'build' &&
!this.environment.config.build.emitAssets
) {
+ const chunkImportMapEnabled =
+ this.environment.config.build.chunkImportMap
for (const file in bundle) {
if (
bundle[file].type === 'asset' &&
!file.endsWith('ssr-manifest.json') &&
- !jsSourceMapRE.test(file)
+ !jsSourceMapRE.test(file) &&
+ !(
+ chunkImportMapEnabled &&
+ file === getImportMapFilename(this.environment.config)
+ )
) {
delete bundle[file]
}
diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts
index 909dbac62..984c516f8 100644
--- a/packages/vite/src/node/plugins/css.ts
+++ b/packages/vite/src/node/plugins/css.ts
@@ -1074,11 +1074,14 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
// With `cssCodeSplit: false`, CSS is emitted as a single stylesheet in the HTML,
// so this per-chunk import map handling is irrelevant.
- if (config.build.chunkImportMap && chunkCssReferences.size) {
+ if (
+ this.environment.config.build.chunkImportMap &&
+ chunkCssReferences.size
+ ) {
// The import map hash identifies the JS chunk independently of its content.
// Since each chunk has at most one extracted CSS sidecar, we can reuse that
// stable identity with a `.css` extension while mapping it to the CSS content hash.
- const importMap = getImportMap(bundle, config)!
+ const importMap = getImportMap(bundle, this.environment.config)!
const importMapReverseMapping = Object.fromEntries(
Object.entries(importMap.mapping).map(([k, v]) => [v, k]),
)
@@ -1126,8 +1129,8 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
const pureCssChunkNameSet = new Set(pureCssChunkNames)
let importMapReverseMapping: Record<string, string> | undefined
- if (config.build.chunkImportMap) {
- const importMap = getImportMap(bundle, config)!
+ if (this.environment.config.build.chunkImportMap) {
+ const importMap = getImportMap(bundle, this.environment.config)!
importMapReverseMapping = Object.fromEntries(
Object.entries(importMap.mapping).map(([k, v]) => [v, k]),
)
diff --git a/packages/vite/src/node/plugins/html.ts b/packages/vite/src/node/plugins/html.ts
index 7f0ae6b1c..36dfafd4f 100644
--- a/packages/vite/src/node/plugins/html.ts
+++ b/packages/vite/src/node/plugins/html.ts
@@ -33,7 +33,7 @@ import {
removeLeadingSlash,
unique,
} from '../utils'
-import type { ResolvedConfig } from '../config'
+import type { ResolvedConfig, ResolvedEnvironmentOptions } from '../config'
import { checkPublicFile } from '../publicDir'
import { BUNDLED_DEV_CLIENT_FILENAME } from '../constants'
import { toOutputFilePathInHtml } from '../build'
@@ -1233,7 +1233,8 @@ export function postImportMapHook(
const decoder = new TextDecoder()
return function (html, { bundle }) {
const chunkImportMapEnabled =
- config.command === 'build' && config.build.chunkImportMap
+ config.command === 'build' &&
+ config.environments.client.build.chunkImportMap
if (importMapAppendRE.test(html)) {
let importMap: string | undefined
@@ -1259,7 +1260,9 @@ export function postImportMapHook(
if (chunkImportMapEnabled) {
const nonce = config.html?.cspNonce
- const importMap = bundle![getImportMapFilename(config)] as OutputAsset
+ const importMap = bundle![
+ getImportMapFilename(config.environments.client)
+ ] as OutputAsset
const importMapHtml = serializeTag({
tag: 'script',
attrs: { type: 'importmap', ...(nonce ? { nonce } : {}) },
@@ -1686,22 +1689,33 @@ function incrementIndent(indent: string = '') {
return `${indent}${indent[0] === '\t' ? '\t' : ' '}`
}
-export function getImportMapFilename(config: ResolvedConfig): string {
+export function getImportMapFilename(
+ options: ResolvedEnvironmentOptions,
+): string {
const chunkImportMap =
- config.build.rolldownOptions.experimental?.chunkImportMap
+ options.build.rolldownOptions.experimental?.chunkImportMap
if (typeof chunkImportMap === 'object' && chunkImportMap.fileName) {
return chunkImportMap.fileName
}
return 'importmap.json'
}
+function getImportMapBaseUrl(options: ResolvedEnvironmentOptions): string {
+ const chunkImportMap =
+ options.build.rolldownOptions.experimental?.chunkImportMap
+ if (typeof chunkImportMap === 'object' && chunkImportMap.baseUrl) {
+ return chunkImportMap.baseUrl
+ }
+ return '/'
+}
+
/**
* Read and parse the chunk import map asset from the bundle.
* Returns `undefined` when the import map is not present in the bundle.
*/
export function getImportMap(
bundle: OutputBundle,
- config: ResolvedConfig,
+ options: ResolvedEnvironmentOptions & ResolvedConfig,
):
| {
asset: OutputAsset
@@ -1710,7 +1724,7 @@ export function getImportMap(
mapping: Record<string, string>
}
| undefined {
- const asset = bundle[getImportMapFilename(config)] as OutputAsset | undefined
+ const asset = bundle[getImportMapFilename(options)] as OutputAsset | undefined
if (!asset) return undefined
const content: { imports: Record<string, string> } = JSON.parse(
@@ -1718,10 +1732,11 @@ export function getImportMap(
? asset.source
: new TextDecoder().decode(asset.source),
)
+ const baseUrl = getImportMapBaseUrl(options)
const mapping = Object.fromEntries(
Object.entries(content.imports).map(([k, v]) => [
- k.slice(config.base.length),
- v.slice(config.base.length),
+ k.slice(baseUrl.length),
+ v.slice(baseUrl.length),
]),
)
return { asset, content, mapping }
diff --git a/packages/vite/src/node/plugins/importAnalysisBuild.ts b/packages/vite/src/node/plugins/importAnalysisBuild.ts
index c6e577252..5ba08c7a5 100644
--- a/packages/vite/src/node/plugins/importAnalysisBuild.ts
+++ b/packages/vite/src/node/plugins/importAnalysisBuild.ts
@@ -357,8 +357,8 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin[] {
let importMapMapping: Record<string, string> | undefined
let importMapReverseMapping: Record<string, string> | undefined
- if (config.build.chunkImportMap) {
- const importMap = getImportMap(bundle, config)!
+ if (this.environment.config.build.chunkImportMap) {
+ const importMap = getImportMap(bundle, this.environment.config)!
importMapMapping = importMap.mapping
importMapReverseMapping = Object.fromEntries(
Object.entries(importMapMapping).map(([k, v]) => [v, k]),
@@ -370,7 +370,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin[] {
fileName: 'importmap.legacy.json',
source: importMap.asset.source,
})
- delete bundle[getImportMapFilename(config)]
+ delete bundle[getImportMapFilename(this.environment.config)]
}
}
|