From cfd65129c5330d63e11808e20693ae3a4b141770 Mon Sep 17 00:00:00 2001 From: Tan Li Hau Date: Sun, 2 Dec 2018 16:44:49 +0800 Subject: [PATCH] use stringify-object instead of JSON.stringify --- .../src/lib/runtime-caching-converter.js | 18 ++++++++++++------ .../src/lib/stringify-without-comments.js | 3 ++- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/packages/workbox-build/src/lib/runtime-caching-converter.js b/packages/workbox-build/src/lib/runtime-caching-converter.js index 7082379ee9..9e9c047080 100644 --- a/packages/workbox-build/src/lib/runtime-caching-converter.js +++ b/packages/workbox-build/src/lib/runtime-caching-converter.js @@ -24,7 +24,7 @@ const stringifyWithoutComments = require('./stringify-without-comments'); * into a string that would configure equivalent `workbox-sw` behavior. * * @param {Object} options See - * https://googlechrome.github.io/sw-toolbox/api.html#options + * https://googlechromelabs.github.io/sw-toolbox/api.html#options * @return {string} A JSON string representing the equivalent options. * * @private @@ -40,8 +40,12 @@ function getOptionsString(options = {}) { // Pull handler-specific config from the options object, since they are // not directly used to construct a Plugin instance. If set, need to be // passed as options to the handler constructor instead. - const handlerOptionKeys = - ['cacheName', 'networkTimeoutSeconds', 'fetchOptions', 'matchOptions']; + const handlerOptionKeys = [ + 'cacheName', + 'networkTimeoutSeconds', + 'fetchOptions', + 'matchOptions', + ]; const handlerOptions = {}; for (const key of handlerOptionKeys) { if (key in options) { @@ -76,7 +80,7 @@ function getOptionsString(options = {}) { const name = pluginConfig.name; pluginCode = `new ${pluginString}(${JSON.stringify(name)}`; if ('options' in pluginConfig) { - pluginCode += `, ${JSON.stringify(pluginConfig.options)}`; + pluginCode += `, ${stringifyWithoutComments(pluginConfig.options)}`; } pluginCode += `)`; @@ -87,7 +91,7 @@ function getOptionsString(options = {}) { const channelName = pluginConfig.channelName; pluginCode = `new ${pluginString}(${JSON.stringify(channelName)}`; if ('options' in pluginConfig) { - pluginCode += `, ${JSON.stringify(pluginConfig.options)}`; + pluginCode += `, ${stringifyWithoutComments(pluginConfig.options)}`; } pluginCode += `)`; @@ -97,7 +101,9 @@ function getOptionsString(options = {}) { // For plugins that just pass in an Object to the constructor, like // expiration and cacheableResponse default: { - pluginCode = `new ${pluginString}(${JSON.stringify(pluginConfig)})`; + pluginCode = `new ${pluginString}(${stringifyWithoutComments( + pluginConfig + )})`; } } diff --git a/packages/workbox-build/src/lib/stringify-without-comments.js b/packages/workbox-build/src/lib/stringify-without-comments.js index 8429ff7d2a..fc16dd3b25 100644 --- a/packages/workbox-build/src/lib/stringify-without-comments.js +++ b/packages/workbox-build/src/lib/stringify-without-comments.js @@ -19,6 +19,7 @@ const stripComments = require('strip-comments'); module.exports = (obj) => { return objectStringify(obj, { - transform: (_obj, _prop, str) => stripComments(str), + transform: (_obj, _prop, str) => + typeof _obj[_prop] === 'function' ? stripComments(str) : str, }); };