Skip to content

Commit 2e8050e

Browse files
authored
refactor: minor changes to reduce diff between normal Vite and rolldown-vite (vitejs#20354)
1 parent 2f17d2f commit 2e8050e

File tree

3 files changed

+50
-38
lines changed

3 files changed

+50
-38
lines changed

packages/vite/src/node/plugins/css.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { fileURLToPath, pathToFileURL } from 'node:url'
66
import postcssrc from 'postcss-load-config'
77
import type {
88
ExistingRawSourceMap,
9-
ModuleFormat,
9+
InternalModuleFormat,
1010
OutputAsset,
1111
OutputChunk,
1212
RenderedChunk,
@@ -1153,7 +1153,7 @@ function isCssScopeToRendered(
11531153
*/
11541154
export function getEmptyChunkReplacer(
11551155
pureCssChunkNames: string[],
1156-
outputFormat: ModuleFormat,
1156+
outputFormat: InternalModuleFormat,
11571157
): (code: string) => string {
11581158
const emptyChunkFiles = pureCssChunkNames
11591159
.map((file) => escapeRegex(path.basename(file)))

packages/vite/src/node/plugins/importAnalysisBuild.ts

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type { Plugin } from '../plugin'
1313
import type { ResolvedConfig } from '../config'
1414
import { toOutputFilePathInJS } from '../build'
1515
import { genSourceMapUrl } from '../server/sourcemap'
16-
import type { Environment } from '../environment'
16+
import type { PartialEnvironment } from '../baseEnvironment'
1717
import { removedPureCssFilesCache } from './css'
1818
import { createParseErrorInfo } from './importAnalysis'
1919

@@ -175,11 +175,43 @@ function preload(
175175
})
176176
}
177177

178+
function getPreloadCode(
179+
environment: PartialEnvironment,
180+
renderBuiltUrlBoolean: boolean,
181+
isRelativeBase: boolean,
182+
) {
183+
const { modulePreload } = environment.config.build
184+
185+
const scriptRel =
186+
modulePreload && modulePreload.polyfill
187+
? `'modulepreload'`
188+
: `/* @__PURE__ */ (${detectScriptRel.toString()})()`
189+
190+
// There are two different cases for the preload list format in __vitePreload
191+
//
192+
// __vitePreload(() => import(asyncChunk), [ ...deps... ])
193+
//
194+
// This is maintained to keep backwards compatibility as some users developed plugins
195+
// using regex over this list to workaround the fact that module preload wasn't
196+
// configurable.
197+
const assetsURL =
198+
renderBuiltUrlBoolean || isRelativeBase
199+
? // If `experimental.renderBuiltUrl` is used, the dependencies might be relative to the current chunk.
200+
// If relative base is used, the dependencies are relative to the current chunk.
201+
// The importerUrl is passed as third parameter to __vitePreload in this case
202+
`function(dep, importerUrl) { return new URL(dep, importerUrl).href }`
203+
: // If the base isn't relative, then the deps are relative to the projects `outDir` and the base
204+
// is appended inside __vitePreload too.
205+
`function(dep) { return ${JSON.stringify(environment.config.base)}+dep }`
206+
const preloadCode = `const scriptRel = ${scriptRel};const assetsURL = ${assetsURL};const seen = {};export const ${preloadMethod} = ${preload.toString()}`
207+
return preloadCode
208+
}
209+
178210
/**
179211
* Build only. During serve this is performed as part of ./importAnalysis.
180212
*/
181213
export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
182-
const getInsertPreload = (environment: Environment) =>
214+
const getInsertPreload = (environment: PartialEnvironment) =>
183215
environment.config.consumer === 'client' &&
184216
!config.isWorker &&
185217
!config.build.lib
@@ -200,30 +232,11 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
200232
load: {
201233
handler(id) {
202234
if (id === preloadHelperId) {
203-
const { modulePreload } = this.environment.config.build
204-
205-
const scriptRel =
206-
modulePreload && modulePreload.polyfill
207-
? `'modulepreload'`
208-
: `/* @__PURE__ */ (${detectScriptRel.toString()})()`
209-
210-
// There are two different cases for the preload list format in __vitePreload
211-
//
212-
// __vitePreload(() => import(asyncChunk), [ ...deps... ])
213-
//
214-
// This is maintained to keep backwards compatibility as some users developed plugins
215-
// using regex over this list to workaround the fact that module preload wasn't
216-
// configurable.
217-
const assetsURL =
218-
renderBuiltUrl || isRelativeBase
219-
? // If `experimental.renderBuiltUrl` is used, the dependencies might be relative to the current chunk.
220-
// If relative base is used, the dependencies are relative to the current chunk.
221-
// The importerUrl is passed as third parameter to __vitePreload in this case
222-
`function(dep, importerUrl) { return new URL(dep, importerUrl).href }`
223-
: // If the base isn't relative, then the deps are relative to the projects `outDir` and the base
224-
// is appended inside __vitePreload too.
225-
`function(dep) { return ${JSON.stringify(config.base)}+dep }`
226-
const preloadCode = `const scriptRel = ${scriptRel};const assetsURL = ${assetsURL};const seen = {};export const ${preloadMethod} = ${preload.toString()}`
235+
const preloadCode = getPreloadCode(
236+
this.environment,
237+
!!renderBuiltUrl,
238+
isRelativeBase,
239+
)
227240
return { code: preloadCode, moduleSideEffects: false }
228241
}
229242
},

packages/vite/src/node/plugins/index.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -187,26 +187,19 @@ export function getCachedFilterForPlugin<
187187
let filter: PluginFilter | TransformHookFilter | undefined
188188
switch (hookName) {
189189
case 'resolveId': {
190-
const rawFilter =
191-
typeof plugin.resolveId === 'object'
192-
? plugin.resolveId.filter?.id
193-
: undefined
190+
const rawFilter = extractFilter(plugin.resolveId)?.id
194191
filters.resolveId = createIdFilter(rawFilter)
195192
filter = filters.resolveId
196193
break
197194
}
198195
case 'load': {
199-
const rawFilter =
200-
typeof plugin.load === 'object' ? plugin.load.filter?.id : undefined
196+
const rawFilter = extractFilter(plugin.load)?.id
201197
filters.load = createIdFilter(rawFilter)
202198
filter = filters.load
203199
break
204200
}
205201
case 'transform': {
206-
const rawFilters =
207-
typeof plugin.transform === 'object'
208-
? plugin.transform.filter
209-
: undefined
202+
const rawFilters = extractFilter(plugin.transform)
210203
filters.transform = createFilterForTransform(
211204
rawFilters?.id,
212205
rawFilters?.code,
@@ -218,6 +211,12 @@ export function getCachedFilterForPlugin<
218211
return filter as FilterForPluginValue[H] | undefined
219212
}
220213

214+
function extractFilter<T extends Function, F>(
215+
hook: ObjectHook<T, { filter?: F }> | undefined,
216+
) {
217+
return hook && 'filter' in hook && hook.filter ? hook.filter : undefined
218+
}
219+
221220
// Same as `@rollup/plugin-alias` default resolver, but we attach additional meta
222221
// if we can't resolve to something, which will error in `importAnalysis`
223222
export const viteAliasCustomResolver: ResolverFunction = async function (

0 commit comments

Comments
 (0)