@@ -2,7 +2,7 @@ import Splitter from './splitter';
2
2
import { Bundler , debugBundler } from './bundler' ;
3
3
import Analyzer from './analyzer' ;
4
4
import type { TreeType } from './analyzer' ;
5
- import Package from './package' ;
5
+ import Package , { V2AddonResolver } from './package' ;
6
6
import BroccoliDebug from 'broccoli-debug' ;
7
7
import BundleConfig from './bundle-config' ;
8
8
import type { Node } from 'broccoli-node-api' ;
@@ -12,6 +12,7 @@ import {
12
12
AppInstance ,
13
13
findTopmostAddon ,
14
14
isDeepAddonInstance ,
15
+ PackageCache ,
15
16
} from '@embroider/shared-internals' ;
16
17
import WebpackBundler from './webpack' ;
17
18
import { Memoize } from 'typescript-memoize' ;
@@ -49,6 +50,7 @@ export interface AutoImportSharedAPI {
49
50
50
51
export default class AutoImport implements AutoImportSharedAPI {
51
52
private packages : Set < Package > = new Set ( ) ;
53
+ private packageCache : PackageCache ;
52
54
private env : 'development' | 'test' | 'production' ;
53
55
private consoleWrite : ( msg : string ) => void ;
54
56
private analyzers : Map < Analyzer , Package > = new Map ( ) ;
@@ -67,7 +69,13 @@ export default class AutoImport implements AutoImportSharedAPI {
67
69
68
70
constructor ( addonInstance : AddonInstance ) {
69
71
let topmostAddon = findTopmostAddon ( addonInstance ) ;
70
- this . packages . add ( Package . lookupParentOf ( topmostAddon ) ) ;
72
+ this . packageCache = PackageCache . shared (
73
+ 'ember-auto-import' ,
74
+ topmostAddon . project . root
75
+ ) ;
76
+ this . packages . add (
77
+ Package . lookupParentOf ( topmostAddon , this . v2AddonResolver )
78
+ ) ;
71
79
let host = topmostAddon . app ;
72
80
73
81
this . installAppFilter ( host ) ;
@@ -108,7 +116,7 @@ export default class AutoImport implements AutoImportSharedAPI {
108
116
treeType ?: TreeType ,
109
117
supportsFastAnalyzer ?: true
110
118
) {
111
- let pack = Package . lookupParentOf ( addon ) ;
119
+ let pack = Package . lookupParentOf ( addon , this . v2AddonResolver ) ;
112
120
this . packages . add ( pack ) ;
113
121
let analyzer = new Analyzer (
114
122
debugTree ( tree , `preprocessor:input-${ this . analyzers . size } ` ) ,
@@ -124,17 +132,55 @@ export default class AutoImport implements AutoImportSharedAPI {
124
132
this . v2Addons . set ( packageName , packageRoot ) ;
125
133
}
126
134
127
- private makeBundler ( allAppTree : Node ) : Bundler {
128
- // this is a concession to compatibility with ember-cli's treeForApp
129
- // merging. Addons are allowed to inject modules into the app, and it's
130
- // extremely common that those modules want to import from the addons
131
- // themselves, even though this jumps arbitrarily many levels in the
132
- // dependency graph.
133
- //
134
- // Since we handle v2 addons, we need to make sure all v2 addons function as
135
- // "dependencies" of the app even though they're not really.
136
- this . rootPackage . magicDeps = this . v2Addons ;
135
+ get v2AddonResolver ( ) : V2AddonResolver {
136
+ return {
137
+ hasV2Addon : ( name : string ) : boolean => {
138
+ return this . v2Addons . has ( name ) ;
139
+ } ,
140
+
141
+ v2AddonRoot : ( name : string ) : string | undefined => {
142
+ return this . v2Addons . get ( name ) ;
143
+ } ,
144
+
145
+ handleRenaming : ( name : string ) : string => {
146
+ let hit = this . renamedModules ( ) . get ( name ) ;
147
+ if ( hit ) {
148
+ return hit ;
149
+ }
150
+ hit = this . renamedModules ( ) . get ( name + '.js' ) ;
151
+ if ( hit ) {
152
+ return hit ;
153
+ }
154
+ hit = this . renamedModules ( ) . get ( name + '/index.js' ) ;
155
+ if ( hit ) {
156
+ return hit ;
157
+ }
158
+ return name ;
159
+ } ,
160
+ } ;
161
+ }
137
162
163
+ private _renamedModules : Map < string , string > | undefined ;
164
+
165
+ private renamedModules ( ) : Map < string , string > {
166
+ if ( ! this . _renamedModules ) {
167
+ this . _renamedModules = new Map ( ) ;
168
+ for ( let packageRoot of this . v2Addons . values ( ) ) {
169
+ let pkg = this . packageCache . get ( packageRoot ) ;
170
+ if ( pkg . isV2Addon ( ) ) {
171
+ let renamedModules = pkg . meta [ 'renamed-modules' ] ;
172
+ if ( renamedModules ) {
173
+ for ( let [ from , to ] of Object . entries ( renamedModules ) ) {
174
+ this . _renamedModules . set ( from , to ) ;
175
+ }
176
+ }
177
+ }
178
+ }
179
+ }
180
+ return this . _renamedModules ;
181
+ }
182
+
183
+ private makeBundler ( allAppTree : Node ) : Bundler {
138
184
// The Splitter takes the set of imports from the Analyzer and
139
185
// decides which ones to include in which bundles
140
186
let splitter = new Splitter ( {
@@ -166,7 +212,6 @@ export default class AutoImport implements AutoImportSharedAPI {
166
212
consoleWrite : this . consoleWrite ,
167
213
bundles : this . bundles ,
168
214
webpack,
169
- v2Addons : this . v2Addons ,
170
215
rootPackage : this . rootPackage ,
171
216
} ) ;
172
217
}
0 commit comments