Problem/Motivation
As it stands, Drupal 8 contrib modules that add a JS minifier would have to GUESS based on heuristics (file name, first 100 bytes, …) whether a JS file is already minified or not. The only reason it's not a problem in HEAD is because we still don't have a JS minifier!
(Noticed thanks to #2258313-24: Add license information to aggregated assets.)
Proposed resolution
There are valid reasons to have minified JS assets (see #1, #4, #7). In fact, we already have minified JS assets in core.
Therefore the solution is simple: make Drupal aware of minified JS assets, so that it doesn't have to guess, and doesn't have to reminify already minified JS.
This issue/patch proposes to add a minified
flag for JS assets. This flag would default to false
(i.e. Drupal assumes JS assets are unminified).
Remaining tasks
None.
User interface changes
None.
API changes
Introduced a minified
flag for JS assets. This flag defaults to false
(i.e. Drupal assumes JS assets are unminified).
Comment | File | Size | Author |
---|---|---|---|
#23 | interdiff.txt | 735 bytes | micbar |
#23 | 2307717-23.patch | 5.76 KB | micbar |
Comments
Comment #1
RainbowArrayIt seems strange to decide that themes are unable to use minification in their build process. Maybe D8 ships with a great minification system. What if something better becomes available in the next 3-5 years after D8 ships?
If core wants to ship with no unminified JS, that seems to make sense. But it should be possible for themes to add in minified JS. It's possible somebody downloads a library for use in a theme, and only a minified version is available.
There are others who know far more about this than me, but it seems like there could be use cases where it would be preferable to be able to declare that JS has already been minified.
Comment #2
Damien Tournoud CreditAttribution: Damien Tournoud commentedIs this really a problem? What bad would it do to re-minify something already minified?
Comment #3
Wim LeersCleaned up the issue summary.
#1:
D8 already ships with an abstraction that allows you to replace the current JS minifier (which is: none) with any other one.
(attiks has expressed his +1 for this comment via Twitter.)
#2: I agree with you. I don't think it's a problem at all. hass thinks it is — from #2258313-22: Add license information to aggregated assets: . I do think that it's preferable to ship with unminified JS files for debugging purposes.
Comment #4
RainbowArrayMost libraries provide unminified js, but in theory is it possible that one wouldn't? Probably.
I think there may be cases where one is linking to an external library hosted on a CDN, but that shouldn't be affected by aggregation I would think.
A workflow that's becoming more common for frontend work is to use Grunt or Gulp to do build work before moving to production. Those commonly have minification as part of the default build.
Setting up D8 in a way that you can't have minified assets in your themes seems pretty pushy on the part of Drupal. Maybe Drupal has an awesome minification system, or you can plug in a minification system that works wonders. That may even be the best way to do things.
But I get leery of anything that limits what front end techniques can be used when who knows what the heck might happen during the lifecycle of Drupal 8, which could be 5-10 years.
Comment #5
Wim Leers#4: I can see that. So perhaps this would be the appropriate policy then:
This is conceptually the simplest (Drupal assumes every asset is unminified), the least restrictive (you still can use minified assets if you want), and the least demanding (no need for a special flag on libraries that have minified assets).
What do you think?
Comment #6
RainbowArrayI have no problem with that.
If people are really concerned with minifying minified assets, I don't think it's the end of the world to tag an asset as already minified in libraries.yml. But probably would be bad if things broke if it wasn't tagged.
So maybe just minifying everything is the way to go.
Comment #7
hass CreditAttribution: hass commentedWe have a lot of minified code in core... Just one example ckeditor. There are more...
Comment #8
nod_The flag needs to be at the file level, not library level and I'd go with a "minified: true/false" on the file properties.
If we look at a higher level that means asset plugins would define and use their own option on files properties to know when to process it (or not). Or at least we should make sure we're thinking it through making a precedent with this.
We can't disallow the use of minified file in core/contrib so 2) is not an option.
From the other issue: double escaping. I had problems years ago with yui compressor but i'm pretty sure it's not a problem anymore. I'd like to avoid having processing already minified files but it's probably not the end of the world.
Comment #9
catchThis is a feature addition at worst, unless we're going to require this is set (which seems unlikely, it should just default one way or the other), so no need to be beta deadline.
Comment #10
catchAlso we require that external libraries used in core are minified, i.e. #1341792: [meta] Ship minified versions of external JavaScript libraries. The first and only time we've had unminified external libraries in core was this release cycle to make development easier, all previous releases used the minified versions throughout. So it's not a policy change to allow it if we already require it in core.
Comment #11
hass CreditAttribution: hass commentedEDIT: Forget it. Answered in the linked case I had not read yet.
Comment #12
sunHow reliable are
.min.js
file suffixes in 2014?(…as well as
/min/
subdirectories?)re: #10: I think we should commit both original sources and minified versions, if both variants are normally part of the upstream dist archive/package. Unless we find a smart logic like the above (which must be reliable), we should think about declaring variants in libraries.yml files.
Comment #13
Wim LeersAgreed. But that's for a separate issue.
So, now that we have all this feedback, I think the following are clear:
Therefore, I propose to simply add a new
optimized
flag to JS assets. ("optimized", not "minified", because that's in sync withAssetOptimizerInterface
, but if people preferminified
adamantly, then I'll change it.)What do you guys think about a patch like this?
Comment #14
Wim LeersNote that #13 also uses this new flag to prevent double minification.
Comment #16
nod_the problem with optimized is that it doesn't say what "optimized" means. So far optimized === minified but will that stay true? what if we have people putting asm'ed files? If we go with optimized we need to define it and say it's only about minification, in which case, "minified" is a better key to be using.
Comment #17
Wim LeersAlright, so let's change to
minified
. Done. Also added tests.Any remaining concerns?
Comment #18
Wim LeersComment #20
RainbowArrayThis looks nice and simple. +1 from me.
Comment #21
Wim LeersShould be green now.
Thanks for the review, mdrummond!
Comment #22
RainbowArrayPretty minor doc fix needed. Somebody with more expertise than me might want to review the tests. Rest looks fine. Any thoughts on who may need to review this so we can RTBC?
Change to: Set the 'minified' flag on JS file assets, default to FALSE.
Comment #23
micbar CreditAttribution: micbar commentedMade the suggested changes.
Comment #24
moshe weitzman CreditAttribution: moshe weitzman commentedCode and tests are good to go.
Comment #25
alexpottAfaics there is absolutely no documentation anywhere about the format of libraries.yml file. Can someone file an issue to create some documentation? Could go in core.api.php
Also we need an issue summary update to document which solution we've go for and why.
Comment #26
Wim LeersIndeed :( Filed an issue: #2323895: [Meta] Document format/content of various YML files.
Done.
Comment #27
alexpottThe isset() here is unnecessary since we ensure it is present in LibraryDiscoveryParser
Comment #28
Wim Leers#27: it is in fact necessary, because you can still use
#attached
without a library to attach JS assets:(Hopefully we can get to a point where we *only* have libraries, but whether that's feasible to achieve within this cycle remains to be seen.)
Comment #30
alexpottCommitted dd4fd1a and pushed to 8.0.x. Thanks!