grunt-usemin icon indicating copy to clipboard operation
grunt-usemin copied to clipboard

Replace non-minified library with minified library with usemin?

Open marplesoft opened this issue 10 years ago • 40 comments

Is there a way to have your html file include non-minified versions of external libraries but during the useminPrepare/usemin phase have them substituted for their minified equivalents?

Before:

<!-- build:js js/third_party.js -->
<script src="components/jquery/jquery.js"></script>
<script src="components/lodash/lodash.js"></script>
<script src="components/angular/angular.js"></script>
<!-- endbuild -->

After:

<script src="components/jquery/jquery.min.js"></script>
<script src="components/lodash/lodash.min.js"></script>
<script src="components/angular/angular.min.js"></script>

Reasons this would be useful:

  • Able to use the non-minified version for dev build (where no usemin step exists)
  • No need to re-minify code that's already minified
    • Saves precious build time
    • Reduces risk of breaking something during uglify - i.e. let the author do that because they know the code the best

Assuming this use case isn't already covered, a suggestion for how to do this via an optional additional comment after each item, eg:

<script src="components/jquery/jquery.js"></script>
<!-- minified:components/jquery/jquery.min.js -->

Consideration would have to be taken to exclude these files from the uglify step.

Thoughts?

marplesoft avatar Aug 26 '13 17:08 marplesoft

I don't quite follow what you are trying to do? Are you just wanting reference a non minified version in your dev then build out to a minified version??

jamesscholeyweb avatar Aug 27 '13 16:08 jamesscholeyweb

Yes - that's what I'm suggesting.

marplesoft avatar Aug 27 '13 19:08 marplesoft

This is what usemin already does. Unless I'm misunderstanding what you are saying. In your example your before code would result in a concatenated minified file called third_party.js containing the 3 scripts you passed into the build block. If you want them output as per your after code then you would just put each in its own build block for example;

<!-- build:js components/jquery/jquery.min.js -->
<script src="components/jquery/jquery.js"></script>
<!-- endbuild -->

Will give you your non minified version in your dev, the when usemin is run it will output a minified version called jquery.min.js in your build

jamesscholeyweb avatar Aug 27 '13 19:08 jamesscholeyweb

Yes but then usemin is re-minifying libraries like jquery that already provide minified versions. It slows build time - that's the main thing. But also without knowledge of a library's internals, minifying it yourself could break it (eg: some libraries need to have certain options for mangle). I'd rather leave it to the library authors to produce a minified version.

marplesoft avatar Aug 27 '13 23:08 marplesoft

I see what you mean. V2 is in dev at the moment but not sure anything like this is being looked at. It can probably be achieved using some combo of grunt copy and another task perhaps. The build speed in v2 is improved quite a bit also.

jamesscholeyweb avatar Aug 28 '13 14:08 jamesscholeyweb

There is also options to control the flow in v2 which might help tackle this. Take a look at the docs on the v2 branch. You could essentially tell it not to minify those file then use a copy task perhaps.

jamesscholeyweb avatar Aug 28 '13 14:08 jamesscholeyweb

+1 to this. We could kludge it together with https://github.com/yeoman/grunt-usemin/issues/128 when that gets in, but a magical "look for xxxx.min.xxx in the same directory as xxxx.xxx, and concat that after concatting and uglifying all other scripts in the block" would be sweet. With the order of the current Yeoman tasks we'd need to add an extra concat though: useminprepare -> concat -> mini/uglify -> (concat:mins) -> rev -> usemin.

robwierzbowski avatar Sep 08 '13 16:09 robwierzbowski

:100: I'm also looking for this must to have feature.

fitzchak avatar Sep 22 '13 14:09 fitzchak

+1

edeustace avatar Sep 29 '13 17:09 edeustace

+1

I googled for this feature and ended up here, so I guess I'd like it to exist also. ;-)

StormToft avatar Oct 21 '13 13:10 StormToft

Would love to have this. Build times are slowing down as we re-minify 3rd party libraries. Even a special comment or attribute tag to note that uglify shouldn't be run on a particular file would help.

zachlowry avatar Oct 22 '13 14:10 zachlowry

I've implemented this feature as a custom flow step. For those interested: https://github.com/rubenv/grunt-usemin-uglifynew

rubenv avatar Nov 10 '13 10:11 rubenv

I would say have an option like:

<!-- build:js:usemin js/third_party.js -->
<script src="components/jquery/jquery.js"></script>
<script src="components/lodash/lodash.js"></script>
<script src="components/angular/angular.js"></script>
<!-- endbuild -->

The flag (haha see the usemin option there) would look for the name.min.js version of the file next to the non-minned version and then merely concat all the files together to the third_party.js file. So it doesnt include the uglify step or passes in custom config for uglify that turns off all the options

eddiemonge avatar Nov 10 '13 16:11 eddiemonge

Don't assume that all libraries follow the basename.min.js convention. Maybe that's the default, but you should be able to override that.

marplesoft avatar Nov 10 '13 19:11 marplesoft

Assuming the .min.js convention seems reasonable, as the user can change the file name to this easily.

fitzchak avatar Nov 11 '13 07:11 fitzchak

What if a dependency system like Bower is bring used? Then the user has do a bower install and then manually rename a bunch of files? Also makes it difficult to automate this for a CI system.

I don't think it has to be that complicated to support overridden file names. Just an optional comment that gets parsed.

marplesoft avatar Nov 11 '13 18:11 marplesoft

the optional comment actually makes it more complicated. that makes it even harder to automate.

As for it the the user is using bower install, i would think that works in favor of the .min. spec as a good bower package will have non-minified files as well as minified files (hopefully following the .min. convention)

eddiemonge avatar Nov 11 '13 18:11 eddiemonge

perhaps something like <!-- build:js:lib --> would be more appropriate?

and only concatenate when a target is set? e.g <!-- build:js:lib js/third_party.js -->

yanneves avatar Nov 20 '13 22:11 yanneves

I would be happy to assume the min naming scheme, and process all without. Fix non-conforming repos upstream :smile:.

An option with a map to minified libraries could be good too.

robwierzbowski avatar Nov 20 '13 23:11 robwierzbowski

Is anyway doing anything with this? If not I'll take a look over the weekend

jamesscholeyweb avatar Nov 27 '13 22:11 jamesscholeyweb

@zeppocreative It doesn't look like anyone has taken ownership of this one yet

timelf123 avatar Dec 17 '13 18:12 timelf123

@timelf123 Hi Tim been super busy with other projects. Not really had time to look. But I'm wondering if usemin in conjunction with grunt-inject might be a good combo to achieve this??

jamesscholeyweb avatar Dec 17 '13 20:12 jamesscholeyweb

I was thinking about using https://github.com/tactivos/grunt-htmlrefs as a workaround for now

timelf123 avatar Dec 17 '13 21:12 timelf123

As long as it is not supported in the main one you could use a fork which enables it. See this. The fork adds some new flows. Logic for adding custom flows is a bit broken if you ask me. Especially the block replacement part which added "undefined" to my html before I went into the sourcecode to "fix" it.

rauno56 avatar Jan 15 '14 17:01 rauno56

@Rauno56 Any plan to request a pull from your repo? It seems promising and would be great to have it included in official repo.

antoinepairet avatar Jun 13 '14 07:06 antoinepairet

I would be more than happy to contribute that to the module. I implemented it in somewhat "hacky" way so I didn't want to litter the official code. Besides that, the flow logic seemed broken or WIP at the time and it felt that I was only one who needed these flows or thought it would be reasonable. I'm away for some weeks but after that would gladly look into how the module have changed and how the same logic would fit into the picture now.

rauno56 avatar Jun 13 '14 07:06 rauno56

@marplesoft did you ever find a work around for this?

highvoltag3 avatar Jul 17 '14 17:07 highvoltag3

My fork does that, like I think I stated before, @highvoltag3. Look for commits for more detailed description. On Jul 17, 2014 7:50 PM, "Dario J Novoa V." [email protected] wrote:

@marplesoft https://github.com/marplesoft did you ever find a work around for this?

— Reply to this email directly or view it on GitHub https://github.com/yeoman/grunt-usemin/issues/171#issuecomment-49340549.

rauno56 avatar Jul 17 '14 18:07 rauno56

Thank @Rauno56 :smile:

highvoltag3 avatar Jul 17 '14 18:07 highvoltag3

@Rauno56 I was looking at libs2min. It seems very useful, however are there any options to have it use the .min.js version if it exists, and if not minify it itself? I'm using bower and some libs have a .min.js and others don't. Ideally, I don't want to have to manage it myself; basically want usemin to automatically use the lib's minified version if it exists and if not minify it.

From what I understand from the lib2min target, it will use the minified version if it exists, but the docs say nothing about minifying it if it doesn't.

Can you confirm please?

benze avatar Aug 07 '14 01:08 benze