1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | return new (P || (P = Promise))(function (resolve, reject) {
|
4 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
7 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8 | });
|
9 | };
|
10 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
11 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
12 | };
|
13 | Object.defineProperty(exports, "__esModule", { value: true });
|
14 | const broccoli_plugin_1 = __importDefault(require("broccoli-plugin"));
|
15 | const debug_1 = __importDefault(require("debug"));
|
16 | const webpack_1 = __importDefault(require("./webpack"));
|
17 | const package_1 = require("./package");
|
18 | const lodash_1 = require("lodash");
|
19 | const path_1 = require("path");
|
20 | const fs_extra_1 = require("fs-extra");
|
21 | const debug = debug_1.default('ember-auto-import:bundler');
|
22 | class Bundler extends broccoli_plugin_1.default {
|
23 | constructor(allAppTree, options) {
|
24 | super([allAppTree], {
|
25 | persistentOutput: true,
|
26 | needsCache: true
|
27 | });
|
28 | this.options = options;
|
29 | this.didEnsureDirs = false;
|
30 | }
|
31 | get publicAssetURL() {
|
32 |
|
33 |
|
34 | let rootPackage = [...this.options.packages.values()].find(pkg => !pkg.isAddon);
|
35 | if (rootPackage) {
|
36 | let url = rootPackage.publicAssetURL;
|
37 | if (url) {
|
38 | if (url[url.length - 1] !== '/') {
|
39 | url = url + '/';
|
40 | }
|
41 | return url;
|
42 | }
|
43 | }
|
44 | }
|
45 | get bundlerHook() {
|
46 | if (!this.cachedBundlerHook) {
|
47 | let extraWebpackConfig = lodash_1.merge({}, ...[...this.options.packages.values()].map(pkg => pkg.webpackConfig));
|
48 | if ([...this.options.packages.values()].find(pkg => pkg.forbidsEval)) {
|
49 | extraWebpackConfig.devtool = 'source-map';
|
50 | }
|
51 | debug('extraWebpackConfig %j', extraWebpackConfig);
|
52 | this.cachedBundlerHook = new webpack_1.default(this.options.bundles, this.options.environment, extraWebpackConfig, this.options.consoleWrite, this.publicAssetURL, this.cachePath);
|
53 | }
|
54 | return this.cachedBundlerHook;
|
55 | }
|
56 | build() {
|
57 | return __awaiter(this, void 0, void 0, function* () {
|
58 | this.ensureDirs();
|
59 | package_1.reloadDevPackages();
|
60 | let { splitter } = this.options;
|
61 | let bundleDeps = yield splitter.deps();
|
62 | if (bundleDeps !== this.lastDeps) {
|
63 | let buildResult = yield this.bundlerHook.build(bundleDeps);
|
64 | this.addEntrypoints(buildResult);
|
65 | this.addLazyAssets(buildResult);
|
66 | this.lastDeps = bundleDeps;
|
67 | }
|
68 | });
|
69 | }
|
70 | ensureDirs() {
|
71 | if (this.didEnsureDirs) {
|
72 | return;
|
73 | }
|
74 | fs_extra_1.emptyDirSync(path_1.join(this.outputPath, 'lazy'));
|
75 | for (let bundle of this.options.bundles.names) {
|
76 | fs_extra_1.emptyDirSync(path_1.join(this.outputPath, 'entrypoints', bundle));
|
77 | }
|
78 | this.didEnsureDirs = true;
|
79 | }
|
80 | addEntrypoints({ entrypoints, dir }) {
|
81 | for (let bundle of this.options.bundles.names) {
|
82 | if (entrypoints.has(bundle)) {
|
83 | entrypoints
|
84 | .get(bundle)
|
85 | .forEach(asset => {
|
86 | fs_extra_1.copySync(path_1.join(dir, asset), path_1.join(this.outputPath, 'entrypoints', bundle, asset));
|
87 | });
|
88 | }
|
89 | }
|
90 | }
|
91 | addLazyAssets({ lazyAssets, dir }) {
|
92 | let contents = lazyAssets.map(asset => {
|
93 |
|
94 | let content = fs_extra_1.readFileSync(path_1.join(dir, asset));
|
95 | fs_extra_1.writeFileSync(path_1.join(this.outputPath, 'lazy', asset), content);
|
96 |
|
97 |
|
98 |
|
99 | if (/\.js$/i.test(asset)) {
|
100 | return content;
|
101 | }
|
102 | }).filter(Boolean);
|
103 | fs_extra_1.writeFileSync(path_1.join(this.outputPath, 'lazy', 'auto-import-fastboot.js'), contents.join('\n'));
|
104 | }
|
105 | }
|
106 | exports.default = Bundler;
|
107 |
|
\ | No newline at end of file |