UNPKG

3.19 kBJavaScriptView Raw
1/*
2 Copyright 2018 Google LLC
3
4 Use of this source code is governed by an MIT-style
5 license that can be found in the LICENSE file or at
6 https://opensource.org/licenses/MIT.
7*/
8
9const assert = require('assert');
10const path = require('path');
11
12const errors = require('./errors');
13const filterFiles = require('./filter-files');
14const getCompositeDetails = require('./get-composite-details');
15const getFileDetails = require('./get-file-details');
16const getStringDetails = require('./get-string-details');
17
18module.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 // Initialize to an empty array so that we can still pass something to
33 // filterFiles() and get a normalized output.
34 let fileDetails = [];
35 const fileSet = new Set();
36
37 if (globDirectory) {
38 if (swDest) {
39 // Ensure that we ignore the SW file we're eventually writing to disk.
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 // If there's an exception thrown while globbing, then report
65 // it back as a warning, and don't consider it fatal.
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};