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

6.0.0+ generates invalid CSS when used in development mode #1365

Closed
alan-agius4 opened this issue Aug 31, 2021 · 8 comments · Fixed by webpack-contrib/mini-css-extract-plugin#815

Comments

@alan-agius4
Copy link
Contributor

alan-agius4 commented Aug 31, 2021

  • Operating System: darwin x64
  • Node Version: 14.17.1
  • yarn 1.22.10
  • webpack Version: 5.51.1
  • css-loader Version: 6.2.0

Expected Behavior

When used with extract css mini, css-loader 6.0.0+ breaks absolute @import rules in development mode because it generates invalid CSS since these rules must precede all other types of rules, except @charset.

See: https://developer.mozilla.org/en-US/docs/Web/CSS/@import#description

Actual Behavior

Development mode (incorrect)

/*!****************************************************************************************!*\
  !*** css ./node_modules/css-loader/dist/cjs.js??ruleSet[1].rules[0].use[1]!./file.css ***!
  \****************************************************************************************/
body {
    font-family: 'Manrope';
}
/*!******************************************************************************************!*\
  !*** css ./node_modules/css-loader/dist/cjs.js??ruleSet[1].rules[0].use[1]!./file-2.css ***!
  \******************************************************************************************/
@import url(https://fonts.googleapis.com/css2?family=Manrope:wght@500;800&display=swap);
/*!**********************************************************************************************!*\
  !*** css ./node_modules/css-loader/dist/cjs.js??ruleSet[1].rules[0].use[1]!./file-2.css (1) ***!
  \**********************************************************************************************/

production (correct)

@import url(https://fonts.googleapis.com/css2?family=Manrope:wght@500;800&display=swap);
body {
    font-family: 'Manrope';
}

Code

https://github.com/alan-agius4/css-loader-issue-import

How Do We Reproduce?

git clone git@github.com:alan-agius4/css-loader-issue-import.git
cd css-loader-issue-import
yarn
yarn webpack
@alexander-akait
Copy link
Member

alexander-akait commented Aug 31, 2021

hm, it should never work as expected, you set styles: ["./file.css", "./file-2.css"],, it like you write:

body {
    font-family: 'Manrope';
}

@import url(https://fonts.googleapis.com/css2?family=Manrope:wght@500;800&display=swap);

it is invalid by CSS spec, @import should be always on top

@alexander-akait
Copy link
Member

Found a bug

@alexander-akait
Copy link
Member

Anyway I recommend to use @import on the top (we have workaround for this case)

@alexander-akait
Copy link
Member

webpack-contrib/mini-css-extract-plugin#815

@alan-agius4
Copy link
Contributor Author

alan-agius4 commented Aug 31, 2021

In some cases though you don't have control of the @import yourself. Example, when the 2 files are from 3rd party dependencies, where each of the file declares an import root, top level, when they get concatenated the output will be invalid.

@alexander-akait thanks for the quick turnaround.

@alexander-akait
Copy link
Member

In some cases though you don't have control of the @import yourself. Example, when the 2 files are from 3rd party dependencies, where each of the file declares an import root, top level, when they get concatenated the output will be invalid.

Make sense, I will add more tests to avoid regressions in future

@alan-agius4
Copy link
Contributor Author

Thanks!

@alexander-akait
Copy link
Member

https://github.com/webpack-contrib/mini-css-extract-plugin/releases/tag/v2.2.1

alan-agius4 added a commit to angular/angular-cli that referenced this issue Aug 31, 2021
…to `2.2.1`

Using `css-loader` version 6 exposed a bug in `mini-css-extract-plugin` which doesn't handle `@import` rules order correctly.

See webpack-contrib/css-loader#1365

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

Successfully merging a pull request may close this issue.

2 participants