-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
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
feat: implement template compiler options 'trimTextWhitespace' and 'collapseTextWhitespace' (#3934) #7598
Conversation
…ollapseTextWhitespace'
After turning on those options, my production build sizes have decreased by 4-10% without any negative side effects at all. Enabling the // before
_c('p',[_vm._v("\r\n\t\tText \t\n\t Line 1\r\n\t\t"), ...])
_c('p',[_vm._v("\r\n\t\tText Line 2\r\n\t\t"), ...])
// after
_c('p',[_vm._v("Text \t\n\t Line 1"), ...])
_c('p',[_vm._v("Text Line 2"), ...]) Enabling the // before
_c('p',[_vm._v("\r\n\t\tText \t\n\t Line 1\r\n\t\t"), ...])
_c('p',[_vm._v("\r\n\t\tText Line 2\r\n\t\t"), ...])
// after
_c('p',[_vm._v(" Text\nLine 1 "), ...])
_c('p',[_vm._v(" Text Line 2 "), ...]) Enabling both options will obviously result in the best outcome, with all whitespace reduced to a minimum: // before
_c('p',[_vm._v("\r\n\t\tText \t\n\t Line 1\r\n\t\t"), ...])
_c('p',[_vm._v("\r\n\t\tText Line 2\r\n\t\t"), ...])
// after
_c('p',[_vm._v("Text\nLine 1"), ...])
_c('p',[_vm._v("Text Line 2"), ...]) vue-loader#1151 has a PR for pulling in those options from the webpack configuration, and I have a second commit waiting, which would enable setting these two options and the already existing <template whitespaces="not-preserve,trim,collapse">
<div>
Text Line 1
<p>
Text Line 2
</p>
<p>
Text Line 3
</p>
Text Line 4
Text Line 5
</div>
</template> Ideally these options should be set to I'm looking forward to hearing what you guys think. |
Excited to see this! Great work! |
Really looking forward to it! Just one wish: |
@econic Thanks for the input. You're totally right that trimming would be fatal in that case. If you only enable the collapse setting and not the trim one though, it should work out fine, as the whitespaces around the text nodes will be collapsed down to a single ASCII-space and not be completely removed. If this PR is ever accepted, it'd probably be a safe bet to enable the collapse option by default, as this really shouldn't change behaviour, but reduce component size a lot. The trim option might break existing code and should stay off for now, and has a rather negligible effect any way; the major reduction occurs by collapsing |
Any news on this one? |
Possible temporary solution: #3934 (comment) |
Thanks for the PR and sorry for letting it hang! See #9208 (comment) for something that achieves similar result with slight differences. We do want to keep the number of options to a minimum and avoid having too many possible different combinations of behavior. The new condense mode essentially combines collapse with a more conservative trim. |
@yyx990803 its not same, coz it's doesnt remove tabulating, we dont need tabulating in compiled templates for example in frameworks. |
This overly complicated implementation seems to be based on a slight misunderstand of the default behavior of HTML rendering. Except for pre tag rendering, 2+ space characters are automatically collapsed to a single space in display. All developers know this behavior and thus format their HTML tags accordingly, e.g.;
knowning that it will render: one two three One would not expect it to render: onetwothree I wrote an HTML minifier in python many years ago; and it takes into account that some spacing between tags are significant and thus should never be completely removed. It's not perfect -- but it also does not cause rendering errors. # release build strips out HTML comments and removes insignificant space
removeHtmlComments_rx = re.compile(r'<!--.+?-->', re.MULTILINE | re.DOTALL)
removeBlankLines_rx = re.compile(r'^\s*$\n', re.MULTILINE)
removeIndents_rx = re.compile(r'^\s+', re.MULTILINE) The regexen are applied in these three sequential steps against HTML content. Done. Simplicity is a virtue. Could this be improved? Yes, by replacing newlines with a single space in a fourth step perhaps. (I recall something I recently read: "Perfect is sometimes the enemy of good.") |
@gold Your proposed implementation is exactly identical to the originally proposed "overly complicated" implementation. Even the regular expressions are basically identical. |
Implements #3934
What kind of change does this PR introduce? (check at least one)
Does this PR introduce a breaking change? (check one)
If yes, please describe the impact and migration path for existing applications:
The PR fulfills these requirements:
dev
branch for v2.x (or to a previous version branch), not themaster
branchfix #xxx[,#xxx]
, where "xxx" is the issue number)If adding a new feature, the PR's description includes:
Other information:
The added options have to be made accessible to at least vue-loader in order to enable them for our builds. I'll see if I can implement that in another PR.