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

minimize=true is stripping invalid css #213

Open
Zauberfisch opened this issue Jan 17, 2020 · 12 comments
Open

minimize=true is stripping invalid css #213

Zauberfisch opened this issue Jan 17, 2020 · 12 comments

Comments

@Zauberfisch
Copy link

  • Operating System: arch linux
  • Node Version: 13.6.0
  • NPM Version: 6.13.6
  • webpack Version: 4.41.2
  • html-loader Version: unknown

I understand that the minimizer is trying to optimise for size, but in the process it strips invalid css from a style tag. I believe that this is undesirable behaviour.
In a normal case that might actually be the right thing to do, but some libraries/frameworks/loaders might depend on a not-valid-css string to be there to perform custom transformations or custom behaviour.

Expected Output for {attrs: false, minimize: true}

<div style="invalid css here">

Actual Output for {attrs: false, minimize: true}

<div style="">.

Expected Output for {attrs: false, minimize: false}

<div style="invalid css here">

Actual Output for {attrs: false, minimize: false}

<div style="invalid css here">

Code

// webpack.config.js
{
				test: /\.customTemplateExtension$/,
				use: [
					{
						loader: 'file-loader',
						options: {
							name: '[name].otherCustomTemplateExtension',
							esModule: false,
							outputPath: '../../app/templates',
						},
					},
					'extract-loader',
					{
						loader: 'html-loader',
						options: {
							attrs: false,
							minimize: true,
						}
					}
				],
			}
// foobar.customTemplateExtension
<div style="invalid value here"></div>
@alexander-akait
Copy link
Member

Please open issue in html-minifier repo

@Zauberfisch
Copy link
Author

I'm not sure it's exactly a bug in html-minifier. There is an option minifyCSS on html-minifier.

@alexander-akait
Copy link
Member

Workaround - set minifyCSS to false. The issue terser/html-minifier-terser#27

@DanielRuf
Copy link

Solution

There is a way to prevent that:
https://github.com/jakubpawlowicz/clean-css#how-to-keep-a-css-fragment-intact

This is already supported through clean-css.
Use the /* clean-css ignore:start */ and /* clean-css ignore:end */ comments.

<div style="/* clean-css ignore:start */invalid css here/* clean-css ignore:end */">test</div>

Bildschirmfoto 2020-01-22 um 00 50 36

@DanielRuf
Copy link

So actually not a bug ;-)

@alexander-akait
Copy link
Member

@DanielRuf Unfortunately this is not the best behavior, in theory in future developers can use new CSS syntax and due clean-css do not support that it is break code, so it is bug, will be great to keep style tags as is if something wrong with syntax

@DanielRuf
Copy link

@DanielRuf Unfortunately this is not the best behavior, in theory in future developers can use new CSS syntax and due clean-css do not support that it is break code, so it is bug, will be great to keep style tags as is if something wrong with syntax

I don't agree with this.

Not possible without breaking the current implementation. Also wrapping everything in a style attribute into these comments prevents minifying css at all in the style attributes which makes not much sense. This should be controllable and it is controllable with these comments. It is just not documented.

@alexander-akait
Copy link
Member

alexander-akait commented Jan 22, 2020

@DanielRuf

I don't agree with this.

Really? Would you expect from a babel or typescript empty string on invalid syntax or you wait an exception and keep a code in files as is? Or maybe prettier should write an empty string too when you have invalid syntax 😄

As i written in the original issue, we should got an exception when a syntax is broken, so when we got an exception we return original string instead return empty string.

@DanielRuf
Copy link

Afaik html-minifier and clean-css accept and generate only valid code.

@DanielRuf
Copy link

As i written in the original issue, we should got an exception when a syntax is broken, so when we got an exception we return original string instead return empty string.

See my reply in the linked issue. It should normally. This is an upstream issue in clean-css as far as I can see.

@alexander-akait
Copy link
Member

Afaik html-minifier and clean-css accept and generate only valid code.

The problem is that clean-css does not recognize the new syntax what support new browsers and return broken code, so it is bug.

@DanielRuf
Copy link

DanielRuf commented Jan 22, 2020

does not recognize the new syntax what support new browsers and return broken code, so it is bug.

The issue was about something else:

<div style="invalid css here">

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants