Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use stringify-object instead of JSON.stringify #1778

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -16,7 +16,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 @@ -32,8 +32,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 @@ -68,7 +72,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 @@ -79,7 +83,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 @@ -89,7 +93,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 @@ -11,6 +11,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,
});
};