-
Notifications
You must be signed in to change notification settings - Fork 122
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
Minify static content by transpiling it first #421
Changes from all commits
d50cc5e
7083fda
d021498
0b0e461
ca995d7
9e89afb
6c08f08
d63c6e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use strict'; | ||
|
||
var babel = require('babel-core'); | ||
var babelPresetEnv = require('babel-preset-env'); | ||
var CleanCss = require('clean-css'); | ||
var uglifyJs = require('uglify-js'); | ||
|
||
module.exports = function(fileExt, fileContent){ | ||
|
||
if(fileExt === '.js'){ | ||
|
||
var presetOptions = { | ||
targets: { | ||
browsers: 'ie 8', | ||
uglify: true | ||
}, | ||
useBuiltIns: true, | ||
modules: false | ||
}; | ||
|
||
var babelOptions = { presets: [[babelPresetEnv, presetOptions]] }, | ||
es5 = babel.transform(fileContent, babelOptions).code; | ||
|
||
return uglifyJs.minify(es5, { fromString: true }).code; | ||
|
||
} else if(fileExt === '.css'){ | ||
|
||
return new CleanCss().minify(fileContent).styles; | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict'; | ||
|
||
var expect = require('chai').expect; | ||
|
||
describe('cli : domain : package-static-files : minify-file', function(){ | ||
var minifyFile = require('../../src/cli/domain/package-static-files/minify-file'); | ||
|
||
describe('when minifying .js file', function(){ | ||
|
||
describe('when file contains es6', function(){ | ||
|
||
var content = 'const hi = (name) => `hello ${name}`;'; | ||
|
||
it('should minify it', function(){ | ||
var minified = minifyFile('.js', content); | ||
expect(minified).to.equal('var hi=function(n){return"hello "+n};'); | ||
}); | ||
}); | ||
|
||
describe('when file contains not valid js', function(){ | ||
|
||
var content = 'const a=notvalid('; | ||
var execute = function(){ | ||
minifyFile('.js', content); | ||
}; | ||
|
||
it('should throw an exception', function(){ | ||
expect(execute).to.throw('Unexpected token'); | ||
}); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,96 +6,146 @@ var path = require('path'); | |
var sinon = require('sinon'); | ||
var _ = require('underscore'); | ||
|
||
var initialise = function(){ | ||
|
||
var fsMock = { | ||
existsSync: sinon.stub(), | ||
lstatSync: sinon.stub(), | ||
mkdirSync: sinon.spy(), | ||
readdirSync: sinon.stub(), | ||
readFileSync: sinon.stub(), | ||
readJson: sinon.stub(), | ||
readJsonSync: sinon.stub(), | ||
writeFile: sinon.stub().yields(null, 'ok'), | ||
writeJson: sinon.stub().yields(null, 'ok') | ||
}; | ||
|
||
var pathMock = { | ||
extname: path.extname, | ||
join: path.join, | ||
resolve: function(){ | ||
return _.toArray(arguments).join('/'); | ||
} | ||
}; | ||
|
||
var Local = injectr('../../src/cli/domain/package-components.js', { | ||
'fs-extra': fsMock, | ||
'uglify-js': { | ||
minify: function(code){ | ||
return { | ||
code: code | ||
}; | ||
describe('cli : domain : package-components', function(){ | ||
|
||
var packageStaticFilesStub; | ||
|
||
var initialise = function(){ | ||
|
||
var fsMock = { | ||
existsSync: sinon.stub(), | ||
lstatSync: sinon.stub(), | ||
mkdirSync: sinon.spy(), | ||
readdirSync: sinon.stub(), | ||
readFileSync: sinon.stub(), | ||
readJson: sinon.stub(), | ||
readJsonSync: sinon.stub(), | ||
writeFile: sinon.stub().yields(null, 'ok'), | ||
writeJson: sinon.stub().yields(null, 'ok') | ||
}; | ||
|
||
var pathMock = { | ||
extname: path.extname, | ||
join: path.join, | ||
resolve: function(){ | ||
return _.toArray(arguments).join('/'); | ||
} | ||
}, | ||
path: pathMock, | ||
'./package-static-files': sinon.stub().yields(null, 'ok'), | ||
'./package-template': sinon.stub().yields(null, { type: 'jade', src: 'template.js', hashKey: '123456'}) | ||
}, { __dirname: '' }); | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see a lot of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree. But I would separate in a different PR if you don't mind There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, happy to pair on this if you like. |
||
|
||
var local = new Local(); | ||
packageStaticFilesStub = sinon.stub().yields(null, 'ok'); | ||
|
||
return { local: local, fs: fsMock }; | ||
}; | ||
var PackageComponents = injectr('../../src/cli/domain/package-components.js', { | ||
'fs-extra': fsMock, | ||
path: pathMock, | ||
'./package-static-files': packageStaticFilesStub, | ||
'./package-template': sinon.stub().yields(null, { type: 'jade', src: 'template.js', hashKey: '123456'}) | ||
}, { __dirname: ''}); | ||
|
||
var executePackaging = function(local, callback){ | ||
return local({ | ||
componentPath: '.', | ||
minify: false, | ||
verbose: false | ||
}, callback); | ||
}; | ||
var packageComponents = new PackageComponents(); | ||
|
||
describe('cli : domain : local', function(){ | ||
return { packageComponents: packageComponents, fs: fsMock }; | ||
}; | ||
|
||
var executePackaging = function(packageComponents, minify, callback){ | ||
return packageComponents({ | ||
componentPath: '.', | ||
minify: minify, | ||
verbose: false | ||
}, callback); | ||
}; | ||
|
||
describe('when packaging', function(){ | ||
|
||
describe('when component is valid', function(){ | ||
|
||
var component; | ||
beforeEach(function(done){ | ||
describe('when minify=true', function(){ | ||
|
||
var component; | ||
beforeEach(function(done){ | ||
|
||
var data = initialise(); | ||
var data = initialise(); | ||
|
||
component = { | ||
name: 'helloworld', | ||
oc: { | ||
files: { | ||
template: { | ||
type: 'jade', | ||
src: 'template.jade' | ||
component = { | ||
name: 'helloworld', | ||
oc: { | ||
files: { | ||
static: ['css'], | ||
template: { | ||
type: 'jade', | ||
src: 'template.jade' | ||
} | ||
} | ||
} | ||
}, | ||
dependencies: {} | ||
}; | ||
}, | ||
dependencies: {} | ||
}; | ||
|
||
data.fs.existsSync.returns(true); | ||
data.fs.readJsonSync.onCall(0).returns(component); | ||
data.fs.readJsonSync.onCall(1).returns({ version: '1.2.3' }); | ||
data.fs.existsSync.returns(true); | ||
data.fs.readJsonSync.onCall(0).returns(component); | ||
data.fs.readJsonSync.onCall(1).returns({ version: '1.2.3' }); | ||
|
||
executePackaging(data.local, done); | ||
}); | ||
executePackaging(data.packageComponents, true, done); | ||
}); | ||
|
||
it('should add version to package.json file', function(){ | ||
expect(component.oc.version).to.eql('1.2.3'); | ||
}); | ||
it('should add version to package.json file', function(){ | ||
expect(component.oc.version).to.eql('1.2.3'); | ||
}); | ||
|
||
it('should mark the package.json as a packaged', function(){ | ||
expect(component.oc.packaged).to.eql(true); | ||
}); | ||
|
||
it('should mark the package.json as a packaged', function(){ | ||
expect(component.oc.packaged).to.eql(true); | ||
it('should save hash for template in package.json', function(){ | ||
expect(component.oc.files.template.hashKey).not.be.empty; | ||
}); | ||
|
||
it('should minify static resources', function(){ | ||
expect(packageStaticFilesStub.args[0][0].minify).to.eql(true); | ||
}); | ||
}); | ||
|
||
it('should save hash for template in package.json', function(){ | ||
expect(component.oc.files.template.hashKey).not.be.empty; | ||
describe('when minify=false', function(){ | ||
|
||
var component; | ||
beforeEach(function(done){ | ||
|
||
var data = initialise(); | ||
|
||
component = { | ||
name: 'helloworld', | ||
oc: { | ||
files: { | ||
static: ['css'], | ||
template: { | ||
type: 'jade', | ||
src: 'template.jade' | ||
} | ||
} | ||
}, | ||
dependencies: {} | ||
}; | ||
|
||
data.fs.existsSync.returns(true); | ||
data.fs.readJsonSync.onCall(0).returns(component); | ||
data.fs.readJsonSync.onCall(1).returns({ version: '1.2.3' }); | ||
|
||
executePackaging(data.packageComponents, false, done); | ||
}); | ||
|
||
it('should add version to package.json file', function(){ | ||
expect(component.oc.version).to.eql('1.2.3'); | ||
}); | ||
|
||
it('should mark the package.json as a packaged', function(){ | ||
expect(component.oc.packaged).to.eql(true); | ||
}); | ||
|
||
it('should save hash for template in package.json', function(){ | ||
expect(component.oc.files.template.hashKey).not.be.empty; | ||
}); | ||
|
||
it('should minify static resources', function(){ | ||
expect(packageStaticFilesStub.args[0][0].minify).to.eql(false); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the only consumer of
minifyFile
I presume.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes