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

Minify static content by transpiling it first #421

Merged
merged 8 commits into from
Mar 27, 2017
Merged

Minify static content by transpiling it first #421

merged 8 commits into from
Mar 27, 2017

Conversation

matteofigus
Copy link
Member

Fixes #409

Summary: #418 was opened and merge to fix #409 but we had to rollback as the packaging was producing an invalid bundle (not good for browsers and potentially fragile for uglify). So #420 reverted it. This should be finally the one, with more testing, more fixes etc.

To review, I recommend to go commit by commit.

@matteofigus matteofigus changed the title Minify Minify static content by transpiling it first Mar 25, 2017
Copy link
Member

@mattiaerre mattiaerre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matteofigus I added some comments, hope they make sense 😊

var CleanCss = require('clean-css');
var uglifyJs = require('uglify-js');

module.exports = function(fileType, fileContent){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be fileExt correct?

minified = minifyFile('.js', content);
});

it('should minify it', function(){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a snapshot test, in this case, would simplify what you need to assert in order to make sure that the minification happened.

Copy link
Member Author

@matteofigus matteofigus Mar 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With a snapshot I would test that

const hi = (name) => `hello ${name}`;

would always translate to

var hi=function(n){return"hello "+n};

Which is fine in this scenario, but what if at some point I upgrade babel and this starts translating to

var hi=function(a){return"hello "+a};

So, my thinking was that I wanted to test something without taking the risk of coupling it too much to the snapshot. So I guess that was by design. Perhaps I'm overthinking this - as I see the benefit of seeing the simplicity of A to B transformation for a test readability point of view.

What do you think @nickbalestra and @mattiaerre ? Perhaps I don't know so much about babel + uglify internals. I tried doing a console.log of the process for 100 times and always gave back the same result.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that the process is referentially transparent , so it should return always the same result

Copy link
Member Author

@matteofigus matteofigus Mar 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, so let me update this. We can also change it in the future if this happens to be a problem.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally think we should not test how the internal minification library works; that's why I was proposing a snapshot test. You will end up having a file containing your uglified code in one place. which is more or less what you do have now. What about a pairing session w/ you after this PR has been merged?

// cc @matteofigus @nickbalestra

@@ -64,7 +34,7 @@ var copyDir = function(params, cb){

if(params.minify && params.ocOptions.minify !== false && (fileExt === '.js' || fileExt === '.css')){
var fileContent = fs.readFileSync(filePath).toString(),
minified = minifyFile(fileExt, fileContent, params.ocOptions);
minified = minifyFile(fileExt, fileContent);
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

return new CleanCss().minify(fileContent).styles;
}

return fileContent;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you shouldn't need to return here as the file extension check is done also at a higher level.

'./package-static-files': sinon.stub().yields(null, 'ok'),
'./package-template': sinon.stub().yields(null, { type: 'jade', src: 'template.js', hashKey: '123456'})
}, { __dirname: '' });
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see a lot of arrange before running this unit test. maybe this component does too many things?

Copy link
Member Author

Choose a reason for hiding this comment

The 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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, happy to pair on this if you like.

extname: path.extname,
join: function(){
return path.join.apply(this, _.toArray(arguments)).replace(/\\/g, '/');
describe('cli : domain : package-static-files', function(){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above re the arrange phase.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree. That's why I am starting to take one piece off (minify part). Definitely gonna tackle other little pieces but I would do in separate PRs if you don't mind

@mattiaerre
Copy link
Member

@matteofigus this LGTM, happy to merge if you like.

@mattiaerre mattiaerre merged commit d3e4023 into master Mar 27, 2017
@mattiaerre mattiaerre deleted the minify branch March 27, 2017 19:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Static js needs to be transpiled before minification
3 participants