UNPKG

2.3 kBJavaScriptView Raw
1/**
2 * Copyright 2017 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17const argv = require('minimist')(process.argv.slice(2));
18const swBuild = require('workbox-build');
19const fs = require('fs');
20
21const inputPath = argv['inputpath'] || './dist/server/public/';
22const overridePath = argv['overridepath'] || inputPath;
23const swInputPath = argv['input'];
24const swOutputPath = argv['output'];
25
26const patterns = ['assets/templates/*.html',
27 'assets/templates/*.json',
28 'scripts/client.js',
29 'styles/main.css'];
30
31const config = {
32 globDirectory: inputPath,
33 globPatterns: patterns,
34 modifyUrlPrefix: {'': '/'}
35};
36
37const overrideConfig = {
38 globDirectory: overridePath,
39 globPatterns: ['**/*'],
40 modifyUrlPrefix: {'': '/'}
41};
42
43const mergeGeneratedManifests = async (base, overrides) => {
44 const baseFiles = await swBuild.getManifest(base);
45 let overrideFiles = [];
46
47 overrideFiles = await swBuild.getManifest(overrides);
48
49 const baseFileMap = new Map(baseFiles.manifestEntries.map(entry => [entry.url, entry.revision]));
50 console.log(baseFileMap)
51 const overrideFileMap = new Map(overrideFiles.manifestEntries.map(entry => [entry.url, entry.revision]));
52 console.log(overrideFileMap)
53 const finalManifest = [];
54
55 new Map([...baseFileMap, ...overrideFileMap]).forEach((value, key) => finalManifest.push({url: key, revision: value}));
56 return finalManifest;
57};
58
59(async () => {
60 const files = await mergeGeneratedManifests(config, overrideConfig);
61 const sw = fs.readFileSync(swInputPath, {encoding: 'utf8'});
62 let newSw;
63
64 if (files === undefined) {
65 newSw = sw.replace(/\["insertfileshere"\]/, '');
66 }
67 else {
68 newSw = sw.replace(/\["insertfileshere"\]/, JSON.stringify(files));
69 }
70
71 fs.writeFileSync(swOutputPath, newSw);
72})();