1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | const assert = require('assert');
|
10 | const path = require('path');
|
11 |
|
12 | const errors = require('./errors');
|
13 | const filterFiles = require('./filter-files');
|
14 | const getCompositeDetails = require('./get-composite-details');
|
15 | const getFileDetails = require('./get-file-details');
|
16 | const getStringDetails = require('./get-string-details');
|
17 |
|
18 | module.exports = async ({
|
19 | dontCacheBustURLsMatching,
|
20 | globDirectory,
|
21 | globFollow,
|
22 | globIgnores,
|
23 | globPatterns,
|
24 | globStrict,
|
25 | manifestTransforms,
|
26 | maximumFileSizeToCacheInBytes,
|
27 | modifyURLPrefix,
|
28 | swDest,
|
29 | templatedURLs,
|
30 | }) => {
|
31 | const warnings = [];
|
32 |
|
33 |
|
34 | let fileDetails = [];
|
35 | const fileSet = new Set();
|
36 |
|
37 | if (globDirectory) {
|
38 | if (swDest) {
|
39 |
|
40 | globIgnores.push(`**/${path.basename(swDest)}`);
|
41 | }
|
42 |
|
43 | try {
|
44 | fileDetails = globPatterns.reduce((accumulated, globPattern) => {
|
45 | const globbedFileDetails = getFileDetails({
|
46 | globDirectory,
|
47 | globFollow,
|
48 | globIgnores,
|
49 | globPattern,
|
50 | globStrict,
|
51 | });
|
52 |
|
53 | globbedFileDetails.forEach((fileDetails) => {
|
54 | if (fileSet.has(fileDetails.file)) {
|
55 | return;
|
56 | }
|
57 |
|
58 | fileSet.add(fileDetails.file);
|
59 | accumulated.push(fileDetails);
|
60 | });
|
61 | return accumulated;
|
62 | }, []);
|
63 | } catch (error) {
|
64 |
|
65 |
|
66 | warnings.push(error.message);
|
67 | }
|
68 | }
|
69 |
|
70 | if (templatedURLs) {
|
71 | for (let url of Object.keys(templatedURLs)) {
|
72 | assert(!fileSet.has(url), errors['templated-url-matches-glob']);
|
73 |
|
74 | const dependencies = templatedURLs[url];
|
75 | if (Array.isArray(dependencies)) {
|
76 | const details = dependencies.reduce((previous, globPattern) => {
|
77 | try {
|
78 | const globbedFileDetails = getFileDetails({
|
79 | globDirectory,
|
80 | globFollow,
|
81 | globIgnores,
|
82 | globPattern,
|
83 | globStrict,
|
84 | });
|
85 | return previous.concat(globbedFileDetails);
|
86 | } catch (error) {
|
87 | const debugObj = {};
|
88 | debugObj[url] = dependencies;
|
89 | throw new Error(`${errors['bad-template-urls-asset']} ` +
|
90 | `'${globPattern}' from '${JSON.stringify(debugObj)}':\n` +
|
91 | error);
|
92 | }
|
93 | }, []);
|
94 | fileDetails.push(getCompositeDetails(url, details));
|
95 | } else if (typeof dependencies === 'string') {
|
96 | fileDetails.push(getStringDetails(url, dependencies));
|
97 | }
|
98 | }
|
99 | }
|
100 |
|
101 | const filteredFiles = filterFiles({fileDetails,
|
102 | maximumFileSizeToCacheInBytes, modifyURLPrefix, dontCacheBustURLsMatching,
|
103 | manifestTransforms});
|
104 |
|
105 | if (warnings.length > 0) {
|
106 | filteredFiles.warnings.push(...warnings);
|
107 | }
|
108 |
|
109 | return filteredFiles;
|
110 | };
|