Skip to content

Commit

Permalink
use stringify-object instead of JSON.stringify
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhauhau committed Dec 2, 2018
1 parent b146d8d commit cfd6512
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
18 changes: 12 additions & 6 deletions packages/workbox-build/src/lib/runtime-caching-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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 += `)`;

Expand All @@ -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 += `)`;

Expand All @@ -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
)})`;
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/workbox-build/src/lib/stringify-without-comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
};

0 comments on commit cfd6512

Please sign in to comment.