Skip to content
Merged
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
4 changes: 2 additions & 2 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { fileURLToPath, pathToFileURL } from 'node:url'
import postcssrc from 'postcss-load-config'
import type {
ExistingRawSourceMap,
ModuleFormat,
InternalModuleFormat,
OutputAsset,
OutputChunk,
RenderedChunk,
Expand Down Expand Up @@ -1153,7 +1153,7 @@ function isCssScopeToRendered(
*/
export function getEmptyChunkReplacer(
pureCssChunkNames: string[],
outputFormat: ModuleFormat,
outputFormat: InternalModuleFormat,
): (code: string) => string {
const emptyChunkFiles = pureCssChunkNames
.map((file) => escapeRegex(path.basename(file)))
Expand Down
65 changes: 39 additions & 26 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type { Plugin } from '../plugin'
import type { ResolvedConfig } from '../config'
import { toOutputFilePathInJS } from '../build'
import { genSourceMapUrl } from '../server/sourcemap'
import type { Environment } from '../environment'
import type { PartialEnvironment } from '../baseEnvironment'
import { removedPureCssFilesCache } from './css'
import { createParseErrorInfo } from './importAnalysis'

Expand Down Expand Up @@ -175,11 +175,43 @@ function preload(
})
}

function getPreloadCode(
environment: PartialEnvironment,
renderBuiltUrlBoolean: boolean,
isRelativeBase: boolean,
) {
const { modulePreload } = environment.config.build

const scriptRel =
modulePreload && modulePreload.polyfill
? `'modulepreload'`
: `/* @__PURE__ */ (${detectScriptRel.toString()})()`

// There are two different cases for the preload list format in __vitePreload
//
// __vitePreload(() => import(asyncChunk), [ ...deps... ])
//
// This is maintained to keep backwards compatibility as some users developed plugins
// using regex over this list to workaround the fact that module preload wasn't
// configurable.
const assetsURL =
renderBuiltUrlBoolean || isRelativeBase
? // If `experimental.renderBuiltUrl` is used, the dependencies might be relative to the current chunk.
// If relative base is used, the dependencies are relative to the current chunk.
// The importerUrl is passed as third parameter to __vitePreload in this case
`function(dep, importerUrl) { return new URL(dep, importerUrl).href }`
: // If the base isn't relative, then the deps are relative to the projects `outDir` and the base
// is appended inside __vitePreload too.
`function(dep) { return ${JSON.stringify(environment.config.base)}+dep }`
const preloadCode = `const scriptRel = ${scriptRel};const assetsURL = ${assetsURL};const seen = {};export const ${preloadMethod} = ${preload.toString()}`
return preloadCode
}

/**
* Build only. During serve this is performed as part of ./importAnalysis.
*/
export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
const getInsertPreload = (environment: Environment) =>
const getInsertPreload = (environment: PartialEnvironment) =>
environment.config.consumer === 'client' &&
!config.isWorker &&
!config.build.lib
Expand All @@ -200,30 +232,11 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
load: {
handler(id) {
if (id === preloadHelperId) {
const { modulePreload } = this.environment.config.build

const scriptRel =
modulePreload && modulePreload.polyfill
? `'modulepreload'`
: `/* @__PURE__ */ (${detectScriptRel.toString()})()`

// There are two different cases for the preload list format in __vitePreload
//
// __vitePreload(() => import(asyncChunk), [ ...deps... ])
//
// This is maintained to keep backwards compatibility as some users developed plugins
// using regex over this list to workaround the fact that module preload wasn't
// configurable.
const assetsURL =
renderBuiltUrl || isRelativeBase
? // If `experimental.renderBuiltUrl` is used, the dependencies might be relative to the current chunk.
// If relative base is used, the dependencies are relative to the current chunk.
// The importerUrl is passed as third parameter to __vitePreload in this case
`function(dep, importerUrl) { return new URL(dep, importerUrl).href }`
: // If the base isn't relative, then the deps are relative to the projects `outDir` and the base
// is appended inside __vitePreload too.
`function(dep) { return ${JSON.stringify(config.base)}+dep }`
const preloadCode = `const scriptRel = ${scriptRel};const assetsURL = ${assetsURL};const seen = {};export const ${preloadMethod} = ${preload.toString()}`
const preloadCode = getPreloadCode(
this.environment,
!!renderBuiltUrl,
isRelativeBase,
)
return { code: preloadCode, moduleSideEffects: false }
}
},
Expand Down
19 changes: 9 additions & 10 deletions packages/vite/src/node/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,26 +187,19 @@ export function getCachedFilterForPlugin<
let filter: PluginFilter | TransformHookFilter | undefined
switch (hookName) {
case 'resolveId': {
const rawFilter =
typeof plugin.resolveId === 'object'
? plugin.resolveId.filter?.id
: undefined
const rawFilter = extractFilter(plugin.resolveId)?.id
filters.resolveId = createIdFilter(rawFilter)
filter = filters.resolveId
break
}
case 'load': {
const rawFilter =
typeof plugin.load === 'object' ? plugin.load.filter?.id : undefined
const rawFilter = extractFilter(plugin.load)?.id
filters.load = createIdFilter(rawFilter)
filter = filters.load
break
}
case 'transform': {
const rawFilters =
typeof plugin.transform === 'object'
? plugin.transform.filter
: undefined
const rawFilters = extractFilter(plugin.transform)
filters.transform = createFilterForTransform(
rawFilters?.id,
rawFilters?.code,
Expand All @@ -218,6 +211,12 @@ export function getCachedFilterForPlugin<
return filter as FilterForPluginValue[H] | undefined
}

function extractFilter<T extends Function, F>(
hook: ObjectHook<T, { filter?: F }> | undefined,
) {
return hook && 'filter' in hook && hook.filter ? hook.filter : undefined
}

// Same as `@rollup/plugin-alias` default resolver, but we attach additional meta
// if we can't resolve to something, which will error in `importAnalysis`
export const viteAliasCustomResolver: ResolverFunction = async function (
Expand Down
Loading