UNPKG

5.11 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6const splitter_1 = __importDefault(require("./splitter"));
7const bundler_1 = __importDefault(require("./bundler"));
8const analyzer_1 = __importDefault(require("./analyzer"));
9const package_1 = __importDefault(require("./package"));
10const broccoli_debug_1 = require("broccoli-debug");
11const bundle_config_1 = __importDefault(require("./bundle-config"));
12const broccoli_append_1 = __importDefault(require("./broccoli-append"));
13const debugTree = broccoli_debug_1.buildDebugCallback('ember-auto-import');
14const protocol = '__ember_auto_import_protocol_v1__';
15class AutoImport {
16 constructor(appOrAddon) {
17 this.packages = new Set();
18 this.analyzers = new Map();
19 this.primaryPackage = appOrAddon;
20 // _findHost is private API but it's been stable in ember-cli for two years.
21 let host = appOrAddon._findHost();
22 this.env = host.env;
23 this.bundles = new bundle_config_1.default(host);
24 if (!this.env) {
25 throw new Error('Bug in ember-auto-import: did not discover environment');
26 }
27 this.consoleWrite = (...args) => appOrAddon.project.ui.write(...args);
28 }
29 static lookup(appOrAddon) {
30 let g = global;
31 if (!g[protocol]) {
32 g[protocol] = new this(appOrAddon);
33 }
34 return g[protocol];
35 }
36 isPrimary(appOrAddon) {
37 return this.primaryPackage === appOrAddon;
38 }
39 analyze(tree, appOrAddon) {
40 let pack = package_1.default.lookup(appOrAddon);
41 this.packages.add(pack);
42 let analyzer = new analyzer_1.default(debugTree(tree, `preprocessor:input-${this.analyzers.size}`), pack);
43 this.analyzers.set(analyzer, pack);
44 return analyzer;
45 }
46 makeBundler(allAppTree) {
47 // The Splitter takes the set of imports from the Analyzer and
48 // decides which ones to include in which bundles
49 let splitter = new splitter_1.default({
50 analyzers: this.analyzers,
51 bundles: this.bundles
52 });
53 // The Bundler asks the splitter for deps it should include and
54 // is responsible for packaging those deps up.
55 return new bundler_1.default(allAppTree, {
56 splitter,
57 environment: this.env,
58 packages: this.packages,
59 consoleWrite: this.consoleWrite,
60 bundles: this.bundles
61 });
62 }
63 addTo(allAppTree) {
64 let bundler = debugTree(this.makeBundler(allAppTree), 'output');
65 let mappings = new Map();
66 for (let name of this.bundles.names) {
67 let byType = new Map();
68 mappings.set(`entrypoints/${name}`, byType);
69 for (let type of this.bundles.types) {
70 let target = this.bundles.bundleEntrypoint(name, type);
71 byType.set(type, target);
72 }
73 }
74 let passthrough = new Map();
75 passthrough.set('lazy', this.bundles.lazyChunkPath);
76 return new broccoli_append_1.default(allAppTree, bundler, {
77 mappings, passthrough
78 });
79 }
80 included(addonInstance) {
81 let host = addonInstance._findHost();
82 this.configureFingerprints(host);
83 // ember-cli as of 3.4-beta has introduced architectural changes that make
84 // it impossible for us to nicely emit the built dependencies via our own
85 // vendor and public trees, because it now considers those as *inputs* to
86 // the trees that we analyze, causing a circle, even though there is no
87 // real circular data dependency.
88 //
89 // We also cannot use postprocessTree('all'), because that only works in
90 // first-level addons.
91 //
92 // So we are forced to monkey patch EmberApp. We insert ourselves right at
93 // the beginning of addonPostprocessTree.
94 let original = host.addonPostprocessTree.bind(host);
95 host.addonPostprocessTree = (which, tree) => {
96 if (which === 'all') {
97 tree = this.addTo(tree);
98 }
99 return original(which, tree);
100 };
101 }
102 // We need to disable fingerprinting of chunks, because (1) they already
103 // have their own webpack-generated hashes and (2) the runtime loader code
104 // can't easily be told about broccoli-asset-rev's hashes.
105 configureFingerprints(host) {
106 let pattern = 'assets/chunk.*.js';
107 if (!host.options.fingerprint) {
108 host.options.fingerprint = {};
109 }
110 if (!host.options.fingerprint.hasOwnProperty('exclude')) {
111 host.options.fingerprint.exclude = [pattern];
112 }
113 else {
114 host.options.fingerprint.exclude.push(pattern);
115 }
116 }
117 updateFastBootManifest(manifest) {
118 manifest.vendorFiles.push(`${this.bundles.lazyChunkPath}/auto-import-fastboot.js`);
119 }
120}
121exports.default = AutoImport;
122//# sourceMappingURL=auto-import.js.map
\No newline at end of file