1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | var __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 | };
|
23 | var __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 | };
|
30 | var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
|
31 | var __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 | };
|
42 | Object.defineProperty(exports, "__esModule", { value: true });
|
43 | const path = require("path");
|
44 | const polymer_analyzer_1 = require("polymer-analyzer");
|
45 | const deps_index_1 = require("polymer-bundler/lib/deps-index");
|
46 | const File = require("vinyl");
|
47 | const path_transformers_1 = require("./path-transformers");
|
48 | const file_map_url_loader_1 = require("./file-map-url-loader");
|
49 | const streams_1 = require("./streams");
|
50 |
|
51 |
|
52 |
|
53 | const extensionToTypeMapping = new Map([
|
54 | ['.css', 'style'],
|
55 | ['.gif', 'image'],
|
56 | ['.html', 'document'],
|
57 | ['.png', 'image'],
|
58 | ['.jpg', 'image'],
|
59 | ['.js', 'script'],
|
60 | ['.json', 'script'],
|
61 | ['.svg', 'image'],
|
62 | ['.webp', 'image'],
|
63 | ['.woff', 'font'],
|
64 | ['.woff2', 'font'],
|
65 | ]);
|
66 |
|
67 |
|
68 |
|
69 | function getResourceTypeFromUrl(url) {
|
70 | return extensionToTypeMapping.get(path.extname(url));
|
71 | }
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | function getResourceTypeFromImport(importFeature) {
|
78 | const importKinds = importFeature.kinds;
|
79 | if (importKinds.has('css-import') || importKinds.has('html-style')) {
|
80 | return 'style';
|
81 | }
|
82 | if (importKinds.has('html-import')) {
|
83 | return 'document';
|
84 | }
|
85 | if (importKinds.has('html-script')) {
|
86 | return 'script';
|
87 | }
|
88 |
|
89 |
|
90 |
|
91 | return getResourceTypeFromUrl(importFeature.url);
|
92 | }
|
93 |
|
94 |
|
95 |
|
96 | function createPushEntryFromImport(importFeature) {
|
97 | return {
|
98 | type: getResourceTypeFromImport(importFeature),
|
99 | weight: 1,
|
100 | };
|
101 | }
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | function generatePushManifestEntryForUrl(analyzer, url) {
|
107 | return __awaiter(this, void 0, void 0, function* () {
|
108 | const analysis = yield analyzer.analyze([url]);
|
109 | const result = analysis.getDocument(url);
|
110 | if (result.successful === false) {
|
111 | const message = result.error && result.error.message || 'unknown';
|
112 | throw new Error(`Unable to get document ${url}: ${message}`);
|
113 | }
|
114 | const analyzedDocument = result.value;
|
115 | const rawImports = [...analyzedDocument.getFeatures({
|
116 | kind: 'import',
|
117 | externalPackages: true,
|
118 | imported: true,
|
119 | noLazyImports: true,
|
120 | })];
|
121 | const importsToPush = rawImports.filter((i) => !(i.type === 'html-import' && i.lazy) &&
|
122 | !(i.kinds.has('html-script-back-reference')));
|
123 | const pushManifestEntries = {};
|
124 | for (const analyzedImport of importsToPush) {
|
125 |
|
126 |
|
127 |
|
128 | const analyzedImportUrl = analyzedImport.url;
|
129 | const relativeImportUrl = analyzer.urlResolver.relative(analyzedImportUrl);
|
130 | const analyzedImportEntry = pushManifestEntries[relativeImportUrl];
|
131 | if (!analyzedImportEntry) {
|
132 | pushManifestEntries[relativeImportUrl] =
|
133 | createPushEntryFromImport(analyzedImport);
|
134 | }
|
135 | }
|
136 | return pushManifestEntries;
|
137 | });
|
138 | }
|
139 |
|
140 |
|
141 |
|
142 |
|
143 | class AddPushManifest extends streams_1.AsyncTransformStream {
|
144 | constructor(config, outPath, basePath) {
|
145 | super({ objectMode: true });
|
146 | this.files = new Map();
|
147 | this.config = config;
|
148 | this.analyzer = new polymer_analyzer_1.Analyzer({
|
149 | urlLoader: new file_map_url_loader_1.FileMapUrlLoader(this.files),
|
150 | urlResolver: new polymer_analyzer_1.FsUrlResolver(config.root),
|
151 | });
|
152 | this.outPath =
|
153 | path.join(this.config.root, outPath || 'push-manifest.json');
|
154 | this.basePath = (basePath || '');
|
155 | }
|
156 | _transformIter(files) {
|
157 | return __asyncGenerator(this, arguments, function* _transformIter_1() {
|
158 | var e_1, _a;
|
159 | try {
|
160 | for (var files_1 = __asyncValues(files), files_1_1; files_1_1 = yield __await(files_1.next()), !files_1_1.done;) {
|
161 | const file = files_1_1.value;
|
162 | this.files.set(this.analyzer.resolveUrl(path_transformers_1.urlFromPath(this.config.root, file.path)), file);
|
163 | yield yield __await(file);
|
164 | }
|
165 | }
|
166 | catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
167 | finally {
|
168 | try {
|
169 | if (files_1_1 && !files_1_1.done && (_a = files_1.return)) yield __await(_a.call(files_1));
|
170 | }
|
171 | finally { if (e_1) throw e_1.error; }
|
172 | }
|
173 |
|
174 | const pushManifest = yield __await(this.generatePushManifest());
|
175 | const pushManifestContents = JSON.stringify(pushManifest, undefined, ' ');
|
176 |
|
177 | yield yield __await(new File({
|
178 | path: this.outPath,
|
179 | contents: Buffer.from(pushManifestContents),
|
180 | }));
|
181 | });
|
182 | }
|
183 | generatePushManifest() {
|
184 | return __awaiter(this, void 0, void 0, function* () {
|
185 |
|
186 |
|
187 |
|
188 | const depsIndex = yield deps_index_1.buildDepsIndex(this.config.allFragments.map((path) => this.analyzer.resolveUrl(path_transformers_1.urlFromPath(this.config.root, path))), this.analyzer);
|
189 |
|
190 |
|
191 | const allFragments = new Set([...depsIndex.keys()].filter((url) => !url.includes('>')));
|
192 |
|
193 |
|
194 | const mainPushEntrypointUrl = this.analyzer.resolveUrl(path_transformers_1.urlFromPath(this.config.root, this.config.shell ||
|
195 | this.config.entrypoint));
|
196 | allFragments.add(mainPushEntrypointUrl);
|
197 |
|
198 | const pushManifest = {};
|
199 | for (const fragment of allFragments) {
|
200 | const absoluteFragmentUrl = '/' + this.analyzer.urlResolver.relative(fragment);
|
201 | pushManifest[absoluteFragmentUrl] =
|
202 | yield generatePushManifestEntryForUrl(this.analyzer, fragment);
|
203 | }
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 | const normalize = (p) => path.posix.join(this.basePath, p).replace(/^\/+/, '');
|
214 | const normalized = {};
|
215 | for (const source of Object.keys(pushManifest)) {
|
216 | const targets = {};
|
217 | for (const target of Object.keys(pushManifest[source])) {
|
218 | targets[normalize(target)] = pushManifest[source][target];
|
219 | }
|
220 | normalized[normalize(source)] = targets;
|
221 | }
|
222 | return normalized;
|
223 | });
|
224 | }
|
225 | }
|
226 | exports.AddPushManifest = AddPushManifest;
|
227 |
|
\ | No newline at end of file |