Skip to content

Commit

Permalink
feat(package): Make bundle minification configurable
Browse files Browse the repository at this point in the history
fixes #998, fixes #997
  • Loading branch information
tripodsan authored Jun 17, 2019
1 parent 5222277 commit 037e90a
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 10 deletions.
8 changes: 3 additions & 5 deletions src/deploy.cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -335,10 +335,8 @@ Alternatively you can auto-add one using the {grey --add <name>} 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();
}

Expand Down
8 changes: 7 additions & 1 deletion src/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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
Expand Down Expand Up @@ -154,6 +159,7 @@ module.exports = function deploy() {
.withCreatePackages(argv.package)
.withAddStrain(argv.add)
.withStatic(argv.static)
.withMinify(argv.minify)
.run();
},

Expand Down
14 changes: 11 additions & 3 deletions src/package.cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
});

Expand Down
8 changes: 7 additions & 1 deletion src/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -53,6 +58,7 @@ module.exports = function deploy() {
.withTarget(argv.target)
.withOnlyModified(!argv.force)
.withStatic(argv.static)
.withMinify(argv.minify)
.run();
},
};
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/all.env
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions test/testDeployCli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
});

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

Expand Down Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions test/testPackageCli.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe('hlx package', () => {
mockPackage.withTarget.returnsThis();
mockPackage.withOnlyModified.returnsThis();
mockPackage.withStatic.returnsThis();
mockPackage.withMinify.returnsThis();
mockPackage.run.returnsThis();
});

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

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

Expand All @@ -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);
});
});
1 change: 1 addition & 0 deletions test/testPackageCmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ describe('hlx package (Integration)', () => {
.withTarget(buildDir)
.withOnlyModified(false)
.withStatic('both')
.withMinify(false)
.on('create-package', (info) => {
created[info.name] = true;
})
Expand Down

0 comments on commit 037e90a

Please sign in to comment.