UNPKG

1.63 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 fse = require('fs-extra');
10const path = require('path');
11
12const errors = require('./errors');
13const populateSWTemplate = require('./populate-sw-template');
14
15module.exports = async ({
16 cacheId,
17 cleanupOutdatedCaches,
18 clientsClaim,
19 directoryIndex,
20 handleFetch,
21 ignoreURLParametersMatching,
22 importScripts,
23 manifestEntries,
24 modulePathPrefix,
25 navigateFallback,
26 navigateFallbackBlacklist,
27 navigateFallbackWhitelist,
28 offlineGoogleAnalytics,
29 runtimeCaching,
30 skipWaiting,
31 swDest,
32 workboxSWImport,
33}) => {
34 try {
35 await fse.mkdirp(path.dirname(swDest));
36 } catch (error) {
37 throw new Error(`${errors['unable-to-make-sw-directory']}. ` +
38 `'${error.message}'`);
39 }
40
41 const populatedTemplate = populateSWTemplate({
42 cacheId,
43 cleanupOutdatedCaches,
44 clientsClaim,
45 directoryIndex,
46 handleFetch,
47 ignoreURLParametersMatching,
48 importScripts,
49 manifestEntries,
50 modulePathPrefix,
51 navigateFallback,
52 navigateFallbackBlacklist,
53 navigateFallbackWhitelist,
54 offlineGoogleAnalytics,
55 runtimeCaching,
56 skipWaiting,
57 workboxSWImport,
58 });
59
60 try {
61 await fse.writeFile(swDest, populatedTemplate);
62 } catch (error) {
63 if (error.code === 'EISDIR') {
64 // See https://github.com/GoogleChrome/workbox/issues/612
65 throw new Error(errors['sw-write-failure-directory']);
66 }
67 throw new Error(`${errors['sw-write-failure']}. '${error.message}'`);
68 }
69};