1
- import type { Alias , AliasOptions , ResolvedConfig } from '..'
1
+ import fs from 'node:fs'
2
+ import path from 'node:path'
3
+ import type {
4
+ Alias ,
5
+ AliasOptions ,
6
+ DepOptimizationOptions ,
7
+ ResolvedConfig
8
+ } from '..'
2
9
import type { Plugin } from '../plugin'
3
- import { bareImportRE } from '../utils'
10
+ import { createIsConfiguredAsSsrExternal } from '../ssr/ssrExternal'
11
+ import { bareImportRE , isOptimizable , moduleListContains } from '../utils'
4
12
import { getDepsOptimizer } from '../optimizer'
5
13
import { tryOptimizedResolve } from './resolve'
6
14
@@ -9,6 +17,8 @@ import { tryOptimizedResolve } from './resolve'
9
17
*/
10
18
export function preAliasPlugin ( config : ResolvedConfig ) : Plugin {
11
19
const findPatterns = getAliasPatterns ( config . resolve . alias )
20
+ const isConfiguredAsExternal = createIsConfiguredAsSsrExternal ( config )
21
+ const isBuild = config . command === 'build'
12
22
return {
13
23
name : 'vite:pre-alias' ,
14
24
async resolveId ( id , importer , options ) {
@@ -18,16 +28,70 @@ export function preAliasPlugin(config: ResolvedConfig): Plugin {
18
28
importer &&
19
29
depsOptimizer &&
20
30
bareImportRE . test ( id ) &&
21
- ! options ?. scan
31
+ ! options ?. scan &&
32
+ id !== '@vite/client' &&
33
+ id !== '@vite/env'
22
34
) {
23
35
if ( findPatterns . find ( ( pattern ) => matches ( pattern , id ) ) ) {
24
- return await tryOptimizedResolve ( depsOptimizer , id , importer )
36
+ const optimizedId = await tryOptimizedResolve (
37
+ depsOptimizer ,
38
+ id ,
39
+ importer
40
+ )
41
+ if ( optimizedId ) {
42
+ return optimizedId // aliased dep already optimized
43
+ }
44
+
45
+ const resolved = await this . resolve ( id , importer , {
46
+ skipSelf : true ,
47
+ ...options
48
+ } )
49
+ if ( resolved && ! depsOptimizer . isOptimizedDepFile ( resolved . id ) ) {
50
+ const optimizeDeps = depsOptimizer . options
51
+ const resolvedId = resolved . id
52
+ const isVirtual = resolvedId === id || resolvedId . includes ( '\0' )
53
+ if (
54
+ ! isVirtual &&
55
+ fs . existsSync ( resolvedId ) &&
56
+ ! moduleListContains ( optimizeDeps . exclude , id ) &&
57
+ path . isAbsolute ( resolvedId ) &&
58
+ ( resolvedId . includes ( 'node_modules' ) ||
59
+ optimizeDeps . include ?. includes ( id ) ) &&
60
+ isOptimizable ( resolvedId , optimizeDeps ) &&
61
+ ! ( isBuild && ssr && isConfiguredAsExternal ( id ) ) &&
62
+ ( ! ssr || optimizeAliasReplacementForSSR ( resolvedId , optimizeDeps ) )
63
+ ) {
64
+ // aliased dep has not yet been optimized
65
+ const optimizedInfo = depsOptimizer ! . registerMissingImport (
66
+ id ,
67
+ resolvedId
68
+ )
69
+ return { id : depsOptimizer ! . getOptimizedDepId ( optimizedInfo ) }
70
+ }
71
+ }
72
+ return resolved
25
73
}
26
74
}
27
75
}
28
76
}
29
77
}
30
78
79
+ function optimizeAliasReplacementForSSR (
80
+ id : string ,
81
+ optimizeDeps : DepOptimizationOptions
82
+ ) {
83
+ if ( optimizeDeps . include ?. includes ( id ) ) {
84
+ return true
85
+ }
86
+ // In the regular resolution, the default for non-external modules is to
87
+ // be optimized if they are CJS. Here, we don't have the package id but
88
+ // only the replacement file path. We could find the package.json from
89
+ // the id and respect the same default in the future.
90
+ // Default to not optimize an aliased replacement for now, forcing the
91
+ // user to explicitly add it to the ssr.optimizeDeps.include list.
92
+ return false
93
+ }
94
+
31
95
// In sync with rollup plugin alias logic
32
96
function matches ( pattern : string | RegExp , importee : string ) {
33
97
if ( pattern instanceof RegExp ) {
0 commit comments