@@ -6,54 +6,59 @@ import { buildDebugCallback } from 'broccoli-debug';
6
6
import BundleConfig from './bundle-config' ;
7
7
import Append from './broccoli-append' ;
8
8
import { Node } from 'broccoli-node-api' ;
9
+ import { LeaderChooser } from './leader' ;
10
+ import { AddonInstance , AppInstance , findTopmostAddon } from './ember-cli-models' ;
9
11
10
12
const debugTree = buildDebugCallback ( 'ember-auto-import' ) ;
11
- const protocol = '__ember_auto_import_protocol_v1__' ;
12
13
13
- export default class AutoImport {
14
- private primaryPackage : any ;
14
+ // This interface must be stable across all versions of ember-auto-import that
15
+ // speak the same leader-election protocol. So don't change this unless you know
16
+ // what you're doing.
17
+ export interface AutoImportSharedAPI {
18
+ isPrimary ( addonInstance : AddonInstance ) : boolean ;
19
+ analyze ( tree : Node , addon : AddonInstance ) : Node ;
20
+ included ( addonInstance : AddonInstance ) : void ;
21
+ updateFastBootManifest ( manifest : { vendorFiles : string [ ] } ) : void ;
22
+ }
23
+
24
+ export default class AutoImport implements AutoImportSharedAPI {
25
+ private primaryPackage : AddonInstance ;
15
26
private packages : Set < Package > = new Set ( ) ;
16
27
private env : 'development' | 'test' | 'production' ;
17
28
private consoleWrite : ( msg : string ) => void ;
18
29
private analyzers : Map < Analyzer , Package > = new Map ( ) ;
19
30
private bundles : BundleConfig ;
20
31
private targets : unknown ;
21
32
22
- static lookup ( appOrAddon : any ) : AutoImport {
23
- let g = global as any ;
24
- if ( ! g [ protocol ] ) {
25
- g [ protocol ] = new this ( appOrAddon ) ;
26
- }
27
- return g [ protocol ] ;
33
+ static register ( addon : AddonInstance ) {
34
+ LeaderChooser . for ( addon ) . register ( addon , ( ) => new AutoImport ( addon ) ) ;
28
35
}
29
36
30
- constructor ( appOrAddon : any ) {
31
- function findHostContext ( appOrAddon : any ) : any {
32
- return appOrAddon . parent . parent
33
- ? findHostContext ( appOrAddon . parent )
34
- : appOrAddon ;
35
- }
37
+ static lookup ( addon : AddonInstance ) : AutoImportSharedAPI {
38
+ return LeaderChooser . for ( addon ) . leader ;
39
+ }
36
40
37
- this . primaryPackage = appOrAddon ;
38
- let hostContext = findHostContext ( appOrAddon ) ;
39
- this . packages . add ( Package . lookup ( hostContext ) ) ;
40
- let host = hostContext . app ;
41
+ constructor ( addonInstance : AddonInstance ) {
42
+ this . primaryPackage = addonInstance ;
43
+ let topmostAddon = findTopmostAddon ( addonInstance ) ;
44
+ this . packages . add ( Package . lookupParentOf ( topmostAddon ) ) ;
45
+ let host = topmostAddon . app ;
41
46
this . env = host . env ;
42
47
this . targets = host . project . targets ;
43
48
this . bundles = new BundleConfig ( host ) ;
44
49
if ( ! this . env ) {
45
50
throw new Error ( 'Bug in ember-auto-import: did not discover environment' ) ;
46
51
}
47
52
48
- this . consoleWrite = ( ...args ) => appOrAddon . project . ui . write ( ...args ) ;
53
+ this . consoleWrite = ( ...args ) => addonInstance . project . ui . write ( ...args ) ;
49
54
}
50
55
51
- isPrimary ( appOrAddon : any ) {
52
- return this . primaryPackage === appOrAddon ;
56
+ isPrimary ( addon : AddonInstance ) {
57
+ return this . primaryPackage === addon ;
53
58
}
54
59
55
- analyze ( tree : Node , appOrAddon : any ) {
56
- let pack = Package . lookup ( appOrAddon ) ;
60
+ analyze ( tree : Node , addon : AddonInstance ) {
61
+ let pack = Package . lookupParentOf ( addon ) ;
57
62
this . packages . add ( pack ) ;
58
63
let analyzer = new Analyzer (
59
64
debugTree ( tree , `preprocessor:input-${ this . analyzers . size } ` ) ,
@@ -63,7 +68,7 @@ export default class AutoImport {
63
68
return analyzer ;
64
69
}
65
70
66
- makeBundler ( allAppTree : Node ) {
71
+ private makeBundler ( allAppTree : Node ) {
67
72
// The Splitter takes the set of imports from the Analyzer and
68
73
// decides which ones to include in which bundles
69
74
let splitter = new Splitter ( {
@@ -105,8 +110,8 @@ export default class AutoImport {
105
110
} ) ;
106
111
}
107
112
108
- included ( addonInstance : any ) {
109
- let host = addonInstance . _findHost ( ) ;
113
+ included ( addonInstance : AddonInstance ) {
114
+ let host = findTopmostAddon ( addonInstance ) . app ;
110
115
this . configureFingerprints ( host ) ;
111
116
112
117
// ember-cli as of 3.4-beta has introduced architectural changes that make
@@ -132,7 +137,7 @@ export default class AutoImport {
132
137
// We need to disable fingerprinting of chunks, because (1) they already
133
138
// have their own webpack-generated hashes and (2) the runtime loader code
134
139
// can't easily be told about broccoli-asset-rev's hashes.
135
- private configureFingerprints ( host : any ) {
140
+ private configureFingerprints ( host : AppInstance ) {
136
141
let pattern = 'assets/chunk.*.js' ;
137
142
if ( ! host . options . fingerprint ) {
138
143
host . options . fingerprint = { } ;
0 commit comments