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

[Question]Will Webpack support CSS module scripts? #14063

Closed
AkifumiSato opened this issue Aug 25, 2021 · 18 comments
Closed

[Question]Will Webpack support CSS module scripts? #14063

AkifumiSato opened this issue Aug 25, 2021 · 18 comments

Comments

@AkifumiSato
Copy link

AkifumiSato commented Aug 25, 2021

It seems that Chrome implements CSS module scripts which imports CSS using import assertion.
https://web.dev/css-module-scripts/

import sheet from './styles.css' assert { type: 'css' };
document.adoptedStyleSheets = [sheet];
shadowRoot.adoptedStyleSheets = [sheet];

This seems to have different specifications than when using css-loader.
I'm wondering if I need to be aware of the difference between css-loader and CSS module scripts in the future.

// CSS module scripts
import sheet from './styles.css' assert { type: 'css' };
// with css-loader
import sheet from './styles.css';

Are there any plans to support this on the Webpack side?
Is this a problem that another library should address?

@sokra
Copy link
Member

sokra commented Aug 25, 2021

Yes, we want to support that, but no plans have been made to implement it yet

@alexander-akait
Copy link
Member

@AkifumiSato

I'm wondering if I need to be aware of the difference between css-loader and CSS module scripts in the future.

We can improve css-loader and have the same behavior

@alexander-akait
Copy link
Member

alexander-akait commented Aug 25, 2021

@AkifumiSato Anyway what is the problem with css-loader and import sheet from './styles.css' assert { type: 'css' };?

@AkifumiSato
Copy link
Author

Anyway what is the problem with css-loader and import sheet from './styles.css' assert { type: 'css' };?

Import results are different for css-loader and css module script.

// CSS module scripts
import sheet from './styles.css' assert { type: 'css' };
console.log(sheet instanceof CSSStyleSheet) // true

// with css-loader
import sheet from './styles.css';
console.log(sheet instanceof CSSStyleSheet) // false, `sheet` is Record<string, string>.

My understanding is that if you import it with css module script, it will not be available for className of ReactElement.

/* index.css */
.title {
  font-size: 20px;
}
import React from 'react'
import styles from './index.css' assert { type: 'css' }

export const Title: React.FC = ({ children }) => (
  // `styles.title` is undefined.
  <h1 className={styles.title}>{children}</h1>
)

@alexander-akait
Copy link
Member

hm, we can fix it, if assert { type: "css" } setted we can return CSSStyleSheet

@alexander-akait
Copy link
Member

And you can use classes as named export https://github.com/webpack-contrib/css-loader#namedexport

@AkifumiSato
Copy link
Author

AkifumiSato commented Aug 25, 2021

hm, we can fix it, if assert { type: "css" } setted we can return CSSStyleSheet

With the support of css module scripts, I think it's difficult for users to be aware that the return value changes depending on the presence or absence of assert {type:'css'}, but can't it be helped?
For example, how about implementing an assert that returns a record similar to css-loader, such as assert {type: " cssModules"}?

@alexander-akait
Copy link
Member

alexander-akait commented Aug 25, 2021

Yep, we can improve this too, it is not hard, I will think how better to do it

@AkifumiSato
Copy link
Author

@alexander-akait thx a lot!
Should I keep this issue open?

@alexander-akait
Copy link
Member

Yep, first we improve css-loader, then in future we will move logic inside webpack core

@sokra
Copy link
Member

sokra commented Aug 30, 2021

assert handling is already in the core. You can do that:

module.rules: [ {
  test: /\.css$/,
  oneOf: [
    { assert: { type: "css" }, loader: "css-loader", options: { exportStylesheet: true } },
    { loader: "css-loader", options: { exportStylesheet: false } }
  ]
} ]

Assuming css-loader supports exportStylesheet, which it currently don't

@alexander-akait
Copy link
Member

@AkifumiSato webpack-contrib/css-loader#1368

@AkifumiSato
Copy link
Author

@alexander-akait It looks like the fix is complete, should I close the Issue?

@alexander-akait
Copy link
Member

@AkifumiSato Not fully finished

@webpack-bot
Copy link
Contributor

This issue had no activity for at least three months.

It's subject to automatic issue closing if there is no activity in the next 15 days.

@vankop
Copy link
Member

vankop commented Feb 25, 2022

bump

@webpack-bot
Copy link
Contributor

Issue was closed because of inactivity.

If you think this is still a valid issue, please file a new issue with additional information.

@alexander-akait
Copy link
Member

let's close in favor #14893

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

5 participants