-
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
Conversation
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.
@matteofigus I added some comments, hope they make sense 😊
var CleanCss = require('clean-css'); | ||
var uglifyJs = require('uglify-js'); | ||
|
||
module.exports = function(fileType, fileContent){ |
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 should be fileExt
correct?
minified = minifyFile('.js', content); | ||
}); | ||
|
||
it('should minify it', function(){ |
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.
a snapshot test, in this case, would simplify what you need to assert in order to make sure that the minification happened.
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.
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.
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.
I think that the process is referentially transparent , so it should return always the same result
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.
ok, so let me update this. We can also change it in the future if this happens to be a problem.
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.
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); |
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
return new CleanCss().minify(fileContent).styles; | ||
} | ||
|
||
return fileContent; |
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.
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: '' }); | ||
}; |
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.
I see a lot of arrange
before running this unit test. maybe this component does too many things?
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.
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 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(){ |
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.
same as above re the arrange
phase.
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.
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
@matteofigus this LGTM, happy to merge if you like. |
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.