1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | const fse = require('fs-extra');
|
10 | const path = require('path');
|
11 |
|
12 | const errors = require('./errors');
|
13 | const populateSWTemplate = require('./populate-sw-template');
|
14 |
|
15 | module.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 |
|
65 | throw new Error(errors['sw-write-failure-directory']);
|
66 | }
|
67 | throw new Error(`${errors['sw-write-failure']}. '${error.message}'`);
|
68 | }
|
69 | };
|