-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
fix: build.modulePreload.resolveDependencies is optimizable
#16083
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
patak-dev
merged 4 commits into
vitejs:main
from
sapphi-red:feat/using-resolveDependencies-is-optimizable
Aug 9, 2024
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -150,15 +150,9 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { | |
| const isWorker = config.isWorker | ||
| const insertPreload = !(ssr || !!config.build.lib || isWorker) | ||
|
|
||
| const resolveModulePreloadDependencies = | ||
| config.build.modulePreload && config.build.modulePreload.resolveDependencies | ||
| const renderBuiltUrl = config.experimental.renderBuiltUrl | ||
| const customModulePreloadPaths = !!( | ||
| resolveModulePreloadDependencies || renderBuiltUrl | ||
| ) | ||
| const isRelativeBase = config.base === './' || config.base === '' | ||
| const optimizeModulePreloadRelativePaths = | ||
| isRelativeBase && !customModulePreloadPaths | ||
| const optimizeModulePreloadRelativePaths = isRelativeBase && !renderBuiltUrl | ||
|
|
||
| const { modulePreload } = config.build | ||
| const scriptRel = | ||
|
|
@@ -173,10 +167,10 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { | |
| // 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 = customModulePreloadPaths | ||
| ? // If `experimental.renderBuiltUrl` or `build.modulePreload.resolveDependencies` are used | ||
| // the dependencies are already resolved. To avoid the need for `new URL(dep, import.meta.url)` | ||
| // a helper `__vitePreloadRelativeDep` is used to resolve from relative paths which can be minimized. | ||
| const assetsURL = renderBuiltUrl | ||
| ? // If `experimental.renderBuiltUrl`is used the dependencies are already resolved. | ||
sapphi-red marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // To avoid the need for `new URL(dep, import.meta.url)`, a helper `__vitePreloadRelativeDep` is | ||
| // used to resolve from relative paths which can be minimized. | ||
| `function(dep, importerUrl) { return dep[0] === '.' ? new URL(dep, importerUrl).href : dep }` | ||
| : optimizeModulePreloadRelativePaths | ||
| ? // If there isn't custom resolvers affecting the deps list, deps in the list are relative | ||
|
|
@@ -251,7 +245,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { | |
| str().appendRight( | ||
| expEnd, | ||
| `,${isModernFlag}?"${preloadMarker}":void 0${ | ||
| optimizeModulePreloadRelativePaths || customModulePreloadPaths | ||
| optimizeModulePreloadRelativePaths || renderBuiltUrl | ||
| ? ',import.meta.url' | ||
| : '' | ||
| })`, | ||
|
|
@@ -420,7 +414,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { | |
|
|
||
| if (markerStartPos > 0) { | ||
| // the dep list includes the main chunk, so only need to reload when there are actual other deps. | ||
| const depsArray = | ||
| let depsArray = | ||
| deps.size > 1 || | ||
| // main chunk is removed | ||
| (hasRemovedPureCssChunk && deps.size > 0) | ||
|
|
@@ -431,33 +425,29 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { | |
| : [...deps] | ||
| : [] | ||
|
|
||
| let renderedDeps: number[] | ||
| if (normalizedFile && customModulePreloadPaths) { | ||
| const { modulePreload } = config.build | ||
| const resolveDependencies = modulePreload | ||
| ? modulePreload.resolveDependencies | ||
| : undefined | ||
| let resolvedDeps: string[] | ||
| if (resolveDependencies) { | ||
| // We can't let the user remove css deps as these aren't really preloads, they are just using | ||
| // the same mechanism as module preloads for this chunk | ||
| const cssDeps: string[] = [] | ||
| const otherDeps: string[] = [] | ||
| for (const dep of depsArray) { | ||
| ;(dep.endsWith('.css') ? cssDeps : otherDeps).push(dep) | ||
| } | ||
| resolvedDeps = [ | ||
| ...resolveDependencies(normalizedFile, otherDeps, { | ||
| hostId: file, | ||
| hostType: 'js', | ||
| }), | ||
| ...cssDeps, | ||
| ] | ||
| } else { | ||
| resolvedDeps = depsArray | ||
| const resolveDependencies = modulePreload | ||
| ? modulePreload.resolveDependencies | ||
| : undefined | ||
| if (resolveDependencies && normalizedFile) { | ||
| // We can't let the user remove css deps as these aren't really preloads, they are just using | ||
| // the same mechanism as module preloads for this chunk | ||
| const cssDeps: string[] = [] | ||
| const otherDeps: string[] = [] | ||
| for (const dep of depsArray) { | ||
| ;(dep.endsWith('.css') ? cssDeps : otherDeps).push(dep) | ||
| } | ||
| depsArray = [ | ||
| ...resolveDependencies(normalizedFile, otherDeps, { | ||
| hostId: file, | ||
| hostType: 'js', | ||
| }), | ||
| ...cssDeps, | ||
| ] | ||
| } | ||
|
|
||
| renderedDeps = resolvedDeps.map((dep) => { | ||
| let renderedDeps: number[] | ||
| if (renderBuiltUrl) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If |
||
| renderedDeps = depsArray.map((dep) => { | ||
| const replacement = toOutputFilePathInJS( | ||
| dep, | ||
| 'asset', | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.