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

Transpile static js before minification #418

Merged
merged 2 commits into from
Mar 24, 2017
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
16 changes: 15 additions & 1 deletion src/cli/domain/package-static-files.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

var async = require('async');
var babel = require('babel-core');
var babelPresetEnv = require('babel-preset-env');
var CleanCss = require('clean-css');
var format = require('stringformat');
var fs = require('fs-extra');
Expand All @@ -14,7 +16,19 @@ var strings = require('../../resources');
var minifyFile = function(fileType, fileContent, ocOptions){

if(fileType === '.js'){
return uglifyJs.minify(fileContent, { fromString: true }).code;

var presetOptions = {
targets: {
browsers: 'not ie <= 8'
}
};

var babelOptions = {
presets: [[babelPresetEnv, presetOptions]]
};

var es5 = babel.transform(fileContent, babelOptions).code;
return uglifyJs.minify(es5, { fromString: true }).code;
} else if(fileType === '.css'){
return new CleanCss().minify(fileContent).styles;
}
Expand Down
16 changes: 12 additions & 4 deletions test/unit/cli-domain-package-static-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ var initialise = function(mocks, params, cb){
var cleanup = function(){
error = null;
mocks = {
'babel-core': {
transform: sinon.stub().returns({
code: 'this-is-transpiled'
})
},
'clean-css': sinon.stub().returns({
minify: function(){
return { styles: 'this-is-minified'};
Expand Down Expand Up @@ -53,9 +58,11 @@ var cleanup = function(){
return _.toArray(arguments).join('/');
}
},
'uglify-js': { minify: sinon.stub().returns({
code: 'this-is-minified'
})}
'uglify-js': {
minify: sinon.stub().returns({
code: 'this-is-minified'
})
}
};
};

Expand Down Expand Up @@ -242,8 +249,9 @@ describe('cli : domain : packageStaticFiles', function(){
expect(error).to.be.null;
});

it('should first minify the file', function(){
it('should first transpile and minify the file', function(){
expect(mocks['fs-extra'].readFileSync.calledOnce).to.be.true;
expect(mocks['babel-core'].transform.calledOnce).to.be.true;
expect(mocks['uglify-js'].minify.calledOnce).to.be.true;
});

Expand Down