From 037e90ac5a76f7c40e094cbf767899f0241f2ade Mon Sep 17 00:00:00 2001 From: Tobias Bocanegra Date: Mon, 17 Jun 2019 11:31:50 +0900 Subject: [PATCH] feat(package): Make bundle minification configurable fixes #998, fixes #997 --- src/deploy.cmd.js | 8 +++----- src/deploy.js | 8 +++++++- src/package.cmd.js | 14 +++++++++++--- src/package.js | 8 +++++++- test/fixtures/all.env | 3 +++ test/testDeployCli.js | 16 ++++++++++++++++ test/testPackageCli.js | 17 +++++++++++++++++ test/testPackageCmd.js | 1 + 8 files changed, 65 insertions(+), 10 deletions(-) diff --git a/src/deploy.cmd.js b/src/deploy.cmd.js index 855026cdc..cefabb2dd 100644 --- a/src/deploy.cmd.js +++ b/src/deploy.cmd.js @@ -52,7 +52,7 @@ class DeployCommand extends StaticCommand { this._dryRun = false; this._createPackages = 'auto'; this._addStrain = null; - this._enableMinify = null; + this._enableMinify = false; } get requireConfigFile() { @@ -335,10 +335,8 @@ Alternatively you can auto-add one using the {grey --add } option.`); const pgkCommand = new PackageCommand(this.log) .withTarget(this._target) .withDirectory(this.directory) - .withOnlyModified(this._createPackages === 'auto'); - if (this._enableMinify !== null) { - pgkCommand.withMinify(this._enableMinify); - } + .withOnlyModified(this._createPackages === 'auto') + .withMinify(this._enableMinify); await pgkCommand.run(); } diff --git a/src/deploy.js b/src/deploy.js index 70fdd3aaf..3636d9dc9 100644 --- a/src/deploy.js +++ b/src/deploy.js @@ -86,6 +86,11 @@ module.exports = function deploy() { choices: ['auto', 'ignore', 'always'], default: 'auto', }) + .option('minify', { + describe: 'Enables minification of the final action bundle.', + type: 'boolean', + default: false, + }) .array('default') .nargs('default', 2) .coerce('default', arg => arg.reduce((result, value, index, array) => { @@ -97,7 +102,7 @@ module.exports = function deploy() { }, {})) .group(['auto', 'wsk-auth', 'wsk-namespace', 'default', 'dirty'], 'Deployment Options') .group(['wsk-host', 'loggly-host', 'loggly-auth', 'target'], 'Advanced Options') - .group(['package', 'target'], 'Package options') + .group(['package', 'minify', 'target'], 'Package options') .check((args) => { if (!args.auto) { // single-shot deployment is easy @@ -154,6 +159,7 @@ module.exports = function deploy() { .withCreatePackages(argv.package) .withAddStrain(argv.add) .withStatic(argv.static) + .withMinify(argv.minify) .run(); }, diff --git a/src/package.cmd.js b/src/package.cmd.js index 5faabc87e..c62ee485a 100644 --- a/src/package.cmd.js +++ b/src/package.cmd.js @@ -24,7 +24,7 @@ class PackageCommand extends StaticCommand { super(logger); this._target = null; this._onlyModified = false; - this._enableMinify = true; + this._enableMinify = false; } // eslint-disable-next-line class-methods-use-this @@ -134,8 +134,16 @@ class PackageCommand extends StaticCommand { */ async createBundles(scripts, bar) { const progressHandler = (percent, msg, ...args) => { - const action = msg === 'building' ? `bundling ${args[0]}` : msg; + /* eslint-disable no-param-reassign */ + const action = args.length > 0 ? `${msg} ${args[0]}` : msg; + const rt = bar.renderThrottle; + if (msg !== 'bundling') { + // this is kind of a hack to force redraw for non-bundling steps. + bar.renderThrottle = 0; + } bar.update(percent * 0.8, { action }); + bar.renderThrottle = rt; + /* eslint-enable no-param-reassign */ }; // create the bundles @@ -211,7 +219,7 @@ class PackageCommand extends StaticCommand { const bar = new ProgressBar('[:bar] :action :elapseds', { total: scripts.length * 2 * 5, width: 50, - renderThrottle: 1, + renderThrottle: 0, stream: process.stdout, }); diff --git a/src/package.js b/src/package.js index 890904099..c83bb0da6 100644 --- a/src/package.js +++ b/src/package.js @@ -33,13 +33,18 @@ module.exports = function deploy() { type: 'boolean', default: false, }) + .option('minify', { + describe: 'Enables minification of the final action bundle.', + type: 'boolean', + default: false, + }) .option('target', { alias: 'o', default: '.hlx/build', type: 'string', describe: 'Target directory for packaged actions', }) - .group(['force', 'target'], 'Package options') + .group(['force', 'minify', 'target'], 'Package options') .help(); }, handler: async (argv) => { @@ -53,6 +58,7 @@ module.exports = function deploy() { .withTarget(argv.target) .withOnlyModified(!argv.force) .withStatic(argv.static) + .withMinify(argv.minify) .run(); }, }; diff --git a/test/fixtures/all.env b/test/fixtures/all.env index 9efecaa8f..1b73e4412 100644 --- a/test/fixtures/all.env +++ b/test/fixtures/all.env @@ -22,6 +22,9 @@ HLX_LOCAL_REPO = ., ../foo-content, ../bar-content # package HLX_FORCE = true +# package + deploy +HLX_MINIFY = true + # deploy + publish + perf HLX_FASTLY_NAMESPACE = 1234 HLX_FASTLY_AUTH = foobar diff --git a/test/testDeployCli.js b/test/testDeployCli.js index 77b474da2..c219a3de4 100644 --- a/test/testDeployCli.js +++ b/test/testDeployCli.js @@ -47,6 +47,7 @@ describe('hlx deploy', () => { mockDeploy.withCreatePackages.returnsThis(); mockDeploy.withAddStrain.returnsThis(); mockDeploy.withStatic.returnsThis(); + mockDeploy.withMinify.returnsThis(); mockDeploy.run.returnsThis(); // disable static functions as well to avoid shelljs executions. @@ -126,6 +127,7 @@ OpenWhisk Namespace is required`); sinon.assert.calledWith(mockDeploy.withCreatePackages, 'auto'); sinon.assert.calledWith(mockDeploy.withCircleciAuth, ''); sinon.assert.calledWith(mockDeploy.withDryRun, false); + sinon.assert.calledWith(mockDeploy.withMinify, false); sinon.assert.calledOnce(mockDeploy.run); }); @@ -148,6 +150,7 @@ OpenWhisk Namespace is required`); sinon.assert.calledWith(mockDeploy.withDefault, undefined); sinon.assert.calledWith(mockDeploy.withCircleciAuth, 'foobar'); sinon.assert.calledWith(mockDeploy.withDryRun, true); + sinon.assert.calledWith(mockDeploy.withMinify, true); sinon.assert.calledOnce(mockDeploy.run); }); @@ -328,6 +331,19 @@ OpenWhisk Namespace is required`); sinon.assert.calledOnce(mockDeploy.run); }); + it('hlx deploy can enable minify', () => { + new CLI() + .withCommandExecutor('deploy', mockDeploy) + .run(['deploy', + '--wsk-auth', 'secret-key', + '--wsk-namespace', 'hlx', + '--minify', + ]); + + sinon.assert.calledWith(mockDeploy.withMinify, true); + sinon.assert.calledOnce(mockDeploy.run); + }); + it('hlx deploy can add strain', () => { new CLI() .withCommandExecutor('deploy', mockDeploy) diff --git a/test/testPackageCli.js b/test/testPackageCli.js index b01b1ce82..3aa423800 100644 --- a/test/testPackageCli.js +++ b/test/testPackageCli.js @@ -31,6 +31,7 @@ describe('hlx package', () => { mockPackage.withTarget.returnsThis(); mockPackage.withOnlyModified.returnsThis(); mockPackage.withStatic.returnsThis(); + mockPackage.withMinify.returnsThis(); mockPackage.run.returnsThis(); }); @@ -45,6 +46,7 @@ describe('hlx package', () => { sinon.assert.calledWith(mockPackage.withOnlyModified, true); sinon.assert.calledWith(mockPackage.withTarget, '.hlx/build'); + sinon.assert.calledWith(mockPackage.withMinify, false); sinon.assert.calledOnce(mockPackage.run); }); @@ -55,6 +57,7 @@ describe('hlx package', () => { .run(['package']); sinon.assert.calledWith(mockPackage.withTarget, 'foo'); sinon.assert.calledWith(mockPackage.withOnlyModified, false); + sinon.assert.calledWith(mockPackage.withMinify, true); sinon.assert.calledOnce(mockPackage.run); }); @@ -67,6 +70,20 @@ describe('hlx package', () => { sinon.assert.calledWith(mockPackage.withOnlyModified, false); sinon.assert.calledWith(mockPackage.withTarget, '.hlx/build'); + sinon.assert.calledWith(mockPackage.withMinify, false); + sinon.assert.calledOnce(mockPackage.run); + }); + + it('hlx package can enable minify', () => { + new CLI() + .withCommandExecutor('package', mockPackage) + .run(['package', + '--minify', + ]); + + sinon.assert.calledWith(mockPackage.withOnlyModified, true); + sinon.assert.calledWith(mockPackage.withTarget, '.hlx/build'); + sinon.assert.calledWith(mockPackage.withMinify, true); sinon.assert.calledOnce(mockPackage.run); }); }); diff --git a/test/testPackageCmd.js b/test/testPackageCmd.js index 1b4f289ab..f23e0e7cf 100644 --- a/test/testPackageCmd.js +++ b/test/testPackageCmd.js @@ -54,6 +54,7 @@ describe('hlx package (Integration)', () => { .withTarget(buildDir) .withOnlyModified(false) .withStatic('both') + .withMinify(false) .on('create-package', (info) => { created[info.name] = true; })