| 18 | const debug = createDebugger('vite:optimize-deps') |
| 19 | |
| 20 | export function optimizedDepsPlugin(): Plugin { |
| 21 | return { |
| 22 | name: 'vite:optimized-deps', |
| 23 | |
| 24 | applyToEnvironment(environment) { |
| 25 | if (environment.config.isBundled) { |
| 26 | return false |
| 27 | } |
| 28 | return !isDepOptimizationDisabled(environment.config.optimizeDeps) |
| 29 | }, |
| 30 | |
| 31 | resolveId(id) { |
| 32 | const environment = this.environment as DevEnvironment |
| 33 | if (environment.depsOptimizer?.isOptimizedDepFile(id)) { |
| 34 | return id |
| 35 | } |
| 36 | }, |
| 37 | |
| 38 | // this.load({ id }) isn't implemented in PluginContainer |
| 39 | // The logic to register an id to wait until it is processed |
| 40 | // is in importAnalysis, see call to delayDepsOptimizerUntil |
| 41 | |
| 42 | async load(id) { |
| 43 | const environment = this.environment as DevEnvironment |
| 44 | const depsOptimizer = environment.depsOptimizer |
| 45 | if (depsOptimizer?.isOptimizedDepFile(id)) { |
| 46 | const metadata = depsOptimizer.metadata |
| 47 | const file = cleanUrl(id) |
| 48 | const versionMatch = DEP_VERSION_RE.exec(id) |
| 49 | const browserHash = versionMatch |
| 50 | ? versionMatch[1].split('=')[1] |
| 51 | : undefined |
| 52 | |
| 53 | // Search in both the currently optimized and newly discovered deps |
| 54 | const info = optimizedDepInfoFromFile(metadata, file) |
| 55 | if (info) { |
| 56 | if ( |
| 57 | browserHash && |
| 58 | info.browserHash !== browserHash && |
| 59 | !environment.config.optimizeDeps.ignoreOutdatedRequests |
| 60 | ) { |
| 61 | throwOutdatedRequest(id) |
| 62 | } |
| 63 | try { |
| 64 | // This is an entry point, it may still not be bundled |
| 65 | await info.processing |
| 66 | } catch { |
| 67 | // If the refresh has not happened after timeout, Vite considers |
| 68 | // something unexpected has happened. In this case, Vite |
| 69 | // returns an empty response that will error. |
| 70 | throwProcessingError(id) |
| 71 | } |
| 72 | const newMetadata = depsOptimizer.metadata |
| 73 | if (metadata !== newMetadata) { |
| 74 | const currentInfo = optimizedDepInfoFromFile(newMetadata!, file) |
| 75 | if ( |
| 76 | info.browserHash !== currentInfo?.browserHash && |
| 77 | !environment.config.optimizeDeps.ignoreOutdatedRequests |