UNPKG

8.1 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
5 * This code may only be used under the BSD style license found at
6 * http://polymer.github.io/LICENSE.txt
7 * The complete set of authors may be found at
8 * http://polymer.github.io/AUTHORS.txt
9 * The complete set of contributors may be found at
10 * http://polymer.github.io/CONTRIBUTORS.txt
11 * Code distributed by Google as part of the polymer project is also
12 * subject to an additional IP rights grant found at
13 * http://polymer.github.io/PATENTS.txt
14 */
15var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
16 return new (P || (P = Promise))(function (resolve, reject) {
17 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
18 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
19 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
20 step((generator = generator.apply(thisArg, _arguments || [])).next());
21 });
22};
23var __asyncValues = (this && this.__asyncValues) || function (o) {
24 if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
25 var m = o[Symbol.asyncIterator], i;
26 return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
27 function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
28 function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
29};
30var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
31var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
32 if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
33 var g = generator.apply(thisArg, _arguments || []), i, q = [];
34 return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
35 function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
36 function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
37 function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
38 function fulfill(value) { resume("next", value); }
39 function reject(value) { resume("throw", value); }
40 function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
41};
42Object.defineProperty(exports, "__esModule", { value: true });
43const File = require("vinyl");
44const polymer_bundler_1 = require("polymer-bundler");
45const file_map_url_loader_1 = require("./file-map-url-loader");
46const path_transformers_1 = require("./path-transformers");
47const streams_1 = require("./streams");
48class BuildBundler extends streams_1.AsyncTransformStream {
49 constructor(config, buildAnalyzer, options = {}) {
50 super({ objectMode: true });
51 // A map of urls to file objects. As the transform stream handleds files
52 // coming into the stream, it collects all files here. After bundlling,
53 // we remove files from this set that have been inlined and replace
54 // entrypoint/fragment files with bundled versions.
55 this.files = new Map();
56 this.config = config;
57 this._buildAnalyzer = buildAnalyzer;
58 const bundlerOptions = Object.assign({}, options);
59 const urlLoader = new file_map_url_loader_1.FileMapUrlLoader(this.files, bundlerOptions.analyzer || buildAnalyzer.analyzer);
60 bundlerOptions.analyzer =
61 (bundlerOptions.analyzer || buildAnalyzer.analyzer)._fork({ urlLoader });
62 if (bundlerOptions.strategy === undefined &&
63 this.config.shell !== undefined) {
64 bundlerOptions.strategy = polymer_bundler_1.generateShellMergeStrategy(bundlerOptions.analyzer.resolveUrl(path_transformers_1.urlFromPath(this.config.root, this.config.shell)));
65 }
66 this._bundler = new polymer_bundler_1.Bundler(bundlerOptions);
67 }
68 _transformIter(files) {
69 return __asyncGenerator(this, arguments, function* _transformIter_1() {
70 var e_1, _a;
71 try {
72 for (var files_1 = __asyncValues(files), files_1_1; files_1_1 = yield __await(files_1.next()), !files_1_1.done;) {
73 const file = files_1_1.value;
74 this._mapFile(file);
75 }
76 }
77 catch (e_1_1) { e_1 = { error: e_1_1 }; }
78 finally {
79 try {
80 if (files_1_1 && !files_1_1.done && (_a = files_1.return)) yield __await(_a.call(files_1));
81 }
82 finally { if (e_1) throw e_1.error; }
83 }
84 yield __await(this._buildBundles());
85 for (const file of this.files.values()) {
86 yield yield __await(file);
87 }
88 });
89 }
90 _buildBundles() {
91 return __awaiter(this, void 0, void 0, function* () {
92 // Tell the analyzer about changed files so it can purge them from its cache
93 // before using the analyzer for bundling.
94 yield this._bundler.analyzer.filesChanged(this._getFilesChangedSinceInitialAnalysis());
95 const { documents, manifest } = yield this._bundler.bundle(yield this._generateBundleManifest());
96 // Remove the bundled files from the file map so they are not emitted later.
97 this._unmapBundledFiles(manifest);
98 // Map the bundles into the file map.
99 for (const [url, document] of documents) {
100 this._mapFile(new File({
101 path: path_transformers_1.pathFromUrl(this.config.root, this._bundler.analyzer.urlResolver.relative(url)),
102 contents: Buffer.from(document.content),
103 }));
104 }
105 });
106 }
107 _generateBundleManifest() {
108 return __awaiter(this, void 0, void 0, function* () {
109 const entrypoints = this.config.allFragments.map((e) => this._bundler.analyzer.resolveUrl(path_transformers_1.urlFromPath(this.config.root, e)));
110 return this._bundler.generateManifest(entrypoints);
111 });
112 }
113 _getFilesChangedSinceInitialAnalysis() {
114 const filesChanged = [];
115 for (const [url, originalFile] of this._buildAnalyzer.files) {
116 const downstreamFile = this.files.get(this._buildAnalyzer.analyzer.resolveUrl(url));
117 if (downstreamFile == null) {
118 throw new Error(`Internal error: could not find downstreamFile at ${url}`);
119 }
120 if (downstreamFile.contents.toString() !==
121 originalFile.contents.toString()) {
122 filesChanged.push(url);
123 }
124 }
125 return filesChanged;
126 }
127 _mapFile(file) {
128 this.files.set(this._buildAnalyzer.analyzer.resolveUrl(path_transformers_1.urlFromPath(this.config.root, file.path)), file);
129 }
130 /**
131 * Removes all of the inlined files in a bundle manifest from the filemap.
132 */
133 _unmapBundledFiles(manifest) {
134 for (const { files, inlinedHtmlImports, inlinedScripts, inlinedStyles, } of manifest.bundles.values()) {
135 for (const url of [...files,
136 ...inlinedHtmlImports,
137 ...inlinedScripts,
138 ...inlinedStyles]) {
139 // Don't unmap the bundle file url itself.
140 if (!manifest.bundles.has(url)) {
141 this.files.delete(url);
142 }
143 }
144 }
145 }
146}
147exports.BuildBundler = BuildBundler;
148//# sourceMappingURL=bundle.js.map
\No newline at end of file