-
-
Notifications
You must be signed in to change notification settings - Fork 604
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
Interoperability across tools and support plain JS modules imports #1050
Comments
Sorry, it is out of scope CSS modules spec, In the near future we want to deprecate CSS modules, there are a lot of other solutions - BEM, CSS-in-JS, CSS-in-JS without runtime https://github.com/callstack/linaria, future CSS module (WICG/webcomponents#759), do not confuse with current CSS modules, Web Components and shadow DOM. CSS modules is maintenance stage (only fixes), it is really old technology and very controversial. We will support them for a while so that all developers can migrate, but no new features, sorry. You can write an own loader based on the current module code. |
👍 Thanks for the response @alexander-akait . I will avoid this pattern for now and promote a migration away from CSS modules within my teams. |
@evilebottnawi |
@gaaamii I think for historical reasons - endlessly 😄 The only thing that can happen - we move them from |
Thanks for your quick reply 😄 |
@alexander-akait Just thought I'd check in on this. My team hasn't been able to prioritize moving away from CSS Modules, and we may have an opportunity in the near future to re-assess our modular CSS solution for packages in a design system/pattern library. Is what you said about CSS Modules being discouraged still true? I haven't seen anything suggesting this in the README of the package, also would like to know whether this should be a viable option for us. so just wanted to double check 😄 |
@GeorgeTaveras1231 I think this is more of a fundamental technology question. What do you think that CSS will be in future? CSS modules are trying to solve problems - scope, compositions, modularity. This is not some fundamental approach, it is an attempt to get rid of accumulated problems. As I said earlier, we will support CSS modules endlessly. But I recommended looking towards new CSS solutions:
And other approaches. The only thing I want to warn about is, do not complicate it once again, so that in the future migration to new technologies will be easier |
Feature Proposal
The feature I'm proposing is essentially to maintain and use a consistent API when exporting and handling CSS modules at all stages of the common webpack+css_modules workflow.
Currently, there is a disparity between the APIs exposed by a CSS Module and used by the webpack tools when it is compiled by
css-loader
,style-loader
,mini-css-extract-plugin
(and legacyextract-text-webpack-plugin
).In more detail, these are the exported APIs.
css-loader
exports and expects this. The other tools expect this API from a CSS Module.All other tools export this. However they expect the API above when handling modules (in order to re-export the locals)
I was affected by this problem while working on my company's UI library which is implemented as a set of loosely coupled packages -- many of which have their own CSS modules. I wanted to facilitate the import of deeply nested CSS files by introducing JS module proxies such as (Context: We set
importLoaders
option tofalse
to allowcss-loader
to import thejs
file.)in
my-package/styles.css.js
In order to support imports in CSS files that would change as such
Introducing the patch above would cause a compile-time error. The problem is that the file in
my-package/path/to/deeply/nested/styles.css
would go through the whole compilation pipeline before it is exported bymy-package/styles.css.js
(which will include an extract rule [style-loader
ormini-css-extract-plugin
]). Because of this, what gets exported bymy-package/styles.css.js
isAnd, when
css-loader
tries to usesomeClass
frommy-package/styles.css.js
to compose.consumer-class
, it will look forcssModule.locals.someClass
, which is not available as I demonstrated above.Feature Use Case
Maintaining and using a consistent API across all stages of the compilation process will unlock the use-case I described above (creating dumb JS module proxies to CSS exports). However, it will also facilitate other cases - such as making truly interoperable JS modules. For example, it would be helpful to do the following:
In
interoperable-dynamic-colors.js
Then be able to consume this from a JS module as such
AND be able to consume this from a CSS module as such
Currernt workarounds
In order to do what I demonstrated above - we have to re-export the module via
locals
so thatcss-loader
allows the reference to be resolved:In
interoperable-dynamic-colors.js
Or, simply ONLY use the
locals
export asIn
interoperable-dynamic-colors.js
And changing the JS code consuming this module as such
Conclusion
While there are workarounds for the use cases I enumerated, they encourage 2 things that have the potential to be "bad"
locals
property)Without diving further into the internals of
css-loader
, I think it would be relatively simple to collapse the local values to the top-level of the exported object (and require all tools to use this API).Additionally, I volunteer myself as a contributor if the webpack team is receptive to this proposal.
The text was updated successfully, but these errors were encountered: