1 | import Splitter from './splitter';
|
2 | import Bundler from './bundler';
|
3 | import Analyzer from './analyzer';
|
4 | import Package from './package';
|
5 | import { buildDebugCallback } from 'broccoli-debug';
|
6 | import BundleConfig from './bundle-config';
|
7 | import Append from './broccoli-append';
|
8 | import { Tree } from 'broccoli-plugin';
|
9 |
|
10 | const debugTree = buildDebugCallback('ember-auto-import');
|
11 | const protocol = '__ember_auto_import_protocol_v1__';
|
12 |
|
13 | export default class AutoImport {
|
14 | private primaryPackage: any;
|
15 | private packages: Set<Package> = new Set();
|
16 | private env: "development" | "test" | "production";
|
17 | private consoleWrite: (msg: string) => void;
|
18 | private analyzers: Map<Analyzer, Package> = new Map();
|
19 | private bundles: BundleConfig;
|
20 |
|
21 | static lookup(appOrAddon: any): AutoImport {
|
22 | let g = global as any;
|
23 | if (!g[protocol]) {
|
24 | g[protocol] = new this(appOrAddon);
|
25 | }
|
26 | return g[protocol];
|
27 | }
|
28 |
|
29 | constructor(appOrAddon: any) {
|
30 | this.primaryPackage = appOrAddon;
|
31 |
|
32 | let host = appOrAddon._findHost();
|
33 | this.env = host.env;
|
34 | this.bundles = new BundleConfig(host);
|
35 | if (!this.env) {
|
36 | throw new Error('Bug in ember-auto-import: did not discover environment');
|
37 | }
|
38 |
|
39 | this.consoleWrite = (...args) => appOrAddon.project.ui.write(...args);
|
40 | }
|
41 |
|
42 | isPrimary(appOrAddon: any) {
|
43 | return this.primaryPackage === appOrAddon;
|
44 | }
|
45 |
|
46 | analyze(tree: Tree, appOrAddon: any) {
|
47 | let pack = Package.lookup(appOrAddon);
|
48 | this.packages.add(pack);
|
49 | let analyzer = new Analyzer(
|
50 | debugTree(tree, `preprocessor:input-${this.analyzers.size}`),
|
51 | pack
|
52 | );
|
53 | this.analyzers.set(analyzer, pack);
|
54 | return analyzer;
|
55 | }
|
56 |
|
57 | makeBundler(allAppTree: Tree) {
|
58 |
|
59 |
|
60 | let splitter = new Splitter({
|
61 | analyzers: this.analyzers,
|
62 | bundles: this.bundles
|
63 | });
|
64 |
|
65 |
|
66 |
|
67 | return new Bundler(allAppTree, {
|
68 | splitter,
|
69 | environment: this.env,
|
70 | packages: this.packages,
|
71 | consoleWrite: this.consoleWrite,
|
72 | bundles: this.bundles
|
73 | });
|
74 | }
|
75 |
|
76 | addTo(allAppTree: Tree) {
|
77 | let bundler = debugTree(this.makeBundler(allAppTree), 'output');
|
78 |
|
79 | let mappings = new Map();
|
80 | for (let name of this.bundles.names) {
|
81 | let byType = new Map();
|
82 | mappings.set(`entrypoints/${name}`, byType);
|
83 | for (let type of this.bundles.types) {
|
84 | let target = this.bundles.bundleEntrypoint(name, type);
|
85 | byType.set(type, target);
|
86 | }
|
87 | }
|
88 |
|
89 | let passthrough = new Map();
|
90 | passthrough.set('lazy', this.bundles.lazyChunkPath);
|
91 |
|
92 | return new Append(allAppTree, bundler, {
|
93 | mappings, passthrough
|
94 | });
|
95 | }
|
96 |
|
97 | included(addonInstance: any) {
|
98 | let host = addonInstance._findHost();
|
99 | this.configureFingerprints(host);
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | let original = host.addonPostprocessTree.bind(host);
|
113 | host.addonPostprocessTree = (which: string, tree: Tree) => {
|
114 | if (which === 'all') {
|
115 | tree = this.addTo(tree);
|
116 | }
|
117 | return original(which, tree);
|
118 | };
|
119 | }
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | private configureFingerprints(host: any) {
|
125 | let pattern = 'assets/chunk.*.js';
|
126 | if (!host.options.fingerprint) {
|
127 | host.options.fingerprint = {};
|
128 | }
|
129 | if (!host.options.fingerprint.hasOwnProperty('exclude')) {
|
130 | host.options.fingerprint.exclude = [pattern];
|
131 | } else {
|
132 | host.options.fingerprint.exclude.push(pattern);
|
133 | }
|
134 | }
|
135 |
|
136 | updateFastBootManifest(manifest: { vendorFiles: string[] }) {
|
137 | manifest.vendorFiles.push(`${this.bundles.lazyChunkPath}/auto-import-fastboot.js`);
|
138 | }
|
139 |
|
140 | }
|