Download vite-chunk-import-map-shared-plugins/solution/gold.patch from dari-ai/vite-selfbench: direct link, hf CLI and curl.
- Browser
- Download file 9.66 kB
-
https://huggingface.co/datasets/dari-ai/vite-selfbench/resolve/main/vite-chunk-import-map-shared-plugins/solution/gold.patch
- Command line
-
hf download hf://datasets/dari-ai/vite-selfbench/vite-chunk-import-map-shared-plugins/solution/gold.patch
-
curl -L -o gold.patch https://huggingface.co/datasets/dari-ai/vite-selfbench/resolve/main/vite-chunk-import-map-shared-plugins/solution/gold.patch
9.66 kB
| 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 | |
| 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, | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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]), | |
| ) | |
| 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 | |
| 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' | |
| 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 | |
| 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 } : {}) }, | |
| 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 | |
| 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( | |
| 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 | |
| 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]), | |
| export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin[] { | |
| fileName: 'importmap.legacy.json', | |
| source: importMap.asset.source, | |
| }) | |
| - delete bundle[getImportMapFilename(config)] | |
| + delete bundle[getImportMapFilename(this.environment.config)] | |
| } | |
| } | |