Skip to content
This repository has been archived by the owner on Sep 9, 2021. It is now read-only.

Add comment strip option #65

Merged
merged 14 commits into from
Jan 21, 2021
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_modules
temp
examples/*/dist
*.log
package-lock.json
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
11 changes: 10 additions & 1 deletion snaps-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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),
)
Expand Down
19 changes: 14 additions & 5 deletions src/build.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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) {

Expand Down Expand Up @@ -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}'!`);
Expand Down Expand Up @@ -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;
}
Expand All @@ -97,16 +100,22 @@ 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;
}

let processedString = bundleString.trim();

if (options.stripComments) {
processedString = stripComments(processedString);
}

// .import( => ["import"](
processedString = processedString.replace(/\.import\(/gu, '["import"](');

Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down