1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | const template = require('lodash.template');
|
10 | const swTemplate = require('../templates/sw-template');
|
11 |
|
12 | const errors = require('./errors');
|
13 | const runtimeCachingConverter = require('./runtime-caching-converter');
|
14 | const stringifyWithoutComments = require('./stringify-without-comments');
|
15 |
|
16 | module.exports = ({
|
17 | cacheId,
|
18 | cleanupOutdatedCaches,
|
19 | clientsClaim,
|
20 | directoryIndex,
|
21 | ignoreURLParametersMatching,
|
22 | importScripts,
|
23 | manifestEntries,
|
24 | modulePathPrefix,
|
25 | navigateFallback,
|
26 | navigateFallbackBlacklist,
|
27 | navigateFallbackWhitelist,
|
28 | offlineGoogleAnalytics,
|
29 | runtimeCaching,
|
30 | skipWaiting,
|
31 | workboxSWImport,
|
32 | }) => {
|
33 |
|
34 | const precacheOptions = {
|
35 | directoryIndex,
|
36 |
|
37 |
|
38 | ignoreURLParametersMatching: ignoreURLParametersMatching ?
|
39 | [] :
|
40 | undefined,
|
41 | };
|
42 |
|
43 | let precacheOptionsString = JSON.stringify(precacheOptions, null, 2);
|
44 | if (ignoreURLParametersMatching) {
|
45 | precacheOptionsString = precacheOptionsString.replace(
|
46 | `"ignoreURLParametersMatching": []`,
|
47 | `"ignoreURLParametersMatching": [` +
|
48 | `${ignoreURLParametersMatching.join(', ')}]`
|
49 | );
|
50 | }
|
51 |
|
52 | let offlineAnalyticsConfigString;
|
53 | if (offlineGoogleAnalytics) {
|
54 |
|
55 |
|
56 | offlineAnalyticsConfigString = offlineGoogleAnalytics === true ?
|
57 |
|
58 | '{}' :
|
59 |
|
60 |
|
61 | stringifyWithoutComments(offlineGoogleAnalytics);
|
62 | }
|
63 |
|
64 | try {
|
65 | const populatedTemplate = template(swTemplate)({
|
66 | cacheId,
|
67 | cleanupOutdatedCaches,
|
68 | clientsClaim,
|
69 | importScripts,
|
70 | manifestEntries,
|
71 | modulePathPrefix,
|
72 | navigateFallback,
|
73 | navigateFallbackBlacklist,
|
74 | navigateFallbackWhitelist,
|
75 | offlineAnalyticsConfigString,
|
76 | precacheOptionsString,
|
77 | skipWaiting,
|
78 | runtimeCaching: runtimeCachingConverter(runtimeCaching),
|
79 | workboxSWImport,
|
80 | });
|
81 |
|
82 |
|
83 | return populatedTemplate.replace(/\n{3,}/g, '\n\n').trim() + '\n';
|
84 | } catch (error) {
|
85 | throw new Error(
|
86 | `${errors['populating-sw-tmpl-failed']} '${error.message}'`);
|
87 | }
|
88 | };
|