diff --git a/.gitignore b/.gitignore index f668bee13..e9a007647 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ node_modules temp examples/*/dist *.log +package-lock.json diff --git a/README.md b/README.md index ac8131945..a5a325382 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,8 @@ Options (build): [string] [default: "bundle.js"] --sourceMaps Whether building outputs sourcemaps [boolean] [default: false] + --stripComments, --strip Whether to remove code comments from bundle + [boolean] [default: false] --port, -p Local server port for testing [number] [required] [default: 8081] --eval, -e Attempt to evaluate Snap bundle in SES diff --git a/package.json b/package.json index 87f3b8476..e0e90f8f4 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,7 @@ "rfdc": "^1.1.4", "serve-handler": "^6.1.1", "ses": "^0.11.0", + "strip-comments": "^2.0.1", "terser": "^4.3.1", "yargs": "^14.0.0" }, diff --git a/snaps-cli.js b/snaps-cli.js index 65b976ff2..ad44e0257 100755 --- a/snaps-cli.js +++ b/snaps-cli.js @@ -67,6 +67,13 @@ const builders = { default: false, }, + stripComments: { + alias: 'strip', + describe: 'Whether to remove code comments from the build output', + type: 'boolean', + default: false, + }, + outfileName: { alias: 'n', describe: 'Output file name', @@ -155,6 +162,7 @@ async function main() { .option('dist', builders.dist) .option('outfileName', builders.outfileName) .option('sourceMaps', builders.sourceMaps) + .option('stripComments', builders.stripComments) .option('port', builders.port) .option('eval', builders.eval) .option('manifest', builders.manifest) @@ -208,7 +216,8 @@ async function main() { .option('dist', builders.dist) .option('outfileName', builders.outfileName) .option('sourceMaps', builders.sourceMaps) - .option('environment', builders.environment); + .option('environment', builders.environment) + .option('stripComments', builders.stripComments); }, (argv) => watch(argv), ) diff --git a/src/build.js b/src/build.js index 2d8f5767d..e1b4fe08f 100644 --- a/src/build.js +++ b/src/build.js @@ -1,5 +1,6 @@ const { promises: fs, createWriteStream } = require('fs'); const browserify = require('browserify'); +const stripComments = require('strip-comments'); // const terser = require('terser') const { logError } = require('./utils'); @@ -15,7 +16,7 @@ module.exports = { * @param {string} dest - The destination file path * @param {object} argv - argv from Yargs * @param {boolean} argv.sourceMaps - Whether to output sourcemaps - * @param {boolean} argv.sourceMaps - Whether to output sourcemaps + * @param {boolean} argv.stripComments - Whether to remove comments from code */ function bundle(src, dest, argv) { @@ -45,7 +46,7 @@ function bundle(src, dest, argv) { // } // closeBundleStream(bundleStream, code.toString()) - closeBundleStream(bundleStream, bundleBuffer ? bundleBuffer.toString() : null) + closeBundleStream(bundleStream, bundleBuffer ? bundleBuffer.toString() : null, { stripComments: argv.stripComments }) .then(() => { if (bundleBuffer) { console.log(`Build success: '${src}' bundled as '${dest}'!`); @@ -79,9 +80,11 @@ function createBundleStream(dest) { * * @param {object} stream - The write stream * @param {string} bundleString - The bundle string + * @param {object} options - post process options + * @param {boolean} options.stripComments */ -async function closeBundleStream(stream, bundleString) { - stream.end(postProcess(bundleString), (err) => { +async function closeBundleStream(stream, bundleString, options) { + stream.end(postProcess(bundleString, options), (err) => { if (err) { throw err; } @@ -97,9 +100,11 @@ async function closeBundleStream(stream, bundleString) { * - handles certain Babel-related edge cases * * @param {string} bundleString - The bundle string + * @param {object} options - post process options + * @param {boolean} options.stripComments * @returns {string} - The postprocessed bundle string */ -function postProcess(bundleString) { +function postProcess(bundleString, options) { if (typeof bundleString !== 'string') { return null; @@ -107,6 +112,10 @@ function postProcess(bundleString) { let processedString = bundleString.trim(); + if (options.stripComments) { + processedString = stripComments(processedString); + } + // .import( => ["import"]( processedString = processedString.replace(/\.import\(/gu, '["import"]('); diff --git a/yarn.lock b/yarn.lock index 975dadd58..078725f1b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3162,6 +3162,11 @@ strip-bom@^3.0.0: resolved "https://registry.yarnpkg.com/strip-bom/-/strip-bom-3.0.0.tgz#2334c18e9c759f7bdd56fdef7e9ae3d588e68ed3" integrity sha1-IzTBjpx1n3vdVv3vfprj1YjmjtM= +strip-comments@^2.0.1: + version "2.0.1" + resolved "https://registry.yarnpkg.com/strip-comments/-/strip-comments-2.0.1.tgz#4ad11c3fbcac177a67a40ac224ca339ca1c1ba9b" + integrity sha512-ZprKx+bBLXv067WTCALv8SSz5l2+XhpYCsVtSqlMnkAXMWDq+/ekVbl1ghqP9rUHTzv6sm/DwCOiYutU/yp1fw== + strip-json-comments@^3.1.0, strip-json-comments@^3.1.1: version "3.1.1" resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-3.1.1.tgz#31f1281b3832630434831c310c01cccda8cbe006"