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

Produce JSON file(s) with class mappings #988

Closed
DanielHeath opened this issue Sep 3, 2019 · 11 comments
Closed

Produce JSON file(s) with class mappings #988

DanielHeath opened this issue Sep 3, 2019 · 11 comments

Comments

@DanielHeath
Copy link

Previously discussed in #143 , but that solution has not worked for a long time.

  • Operating System: ubuntu
  • Node Version: v12.5.0
  • NPM Version: yarn 1.16.0
  • webpack Version: 4.32.2
  • css-loader Version: 2.1.1

Feature Proposal

When compiling CSS modules, export a JSON file containing the mappings, so that the generated classnames can be used by e.g. SSR setups, and even from other languages.

Feature Use Case

I really like using CSS modules as it lets me be sure that my CSS is not clobbering anything.

I'm using webpacker through rails, and I'd like to use CSS modules from my rails templates.

@alexander-akait
Copy link
Member

Can you provide case?

@DanielHeath
Copy link
Author

DanielHeath commented Sep 3, 2019

EG in a rails template, I'd like to be able do to:

<%= css_module('search-listing/article') do |cssm| %>
  <div class="<%= cssm.container %>">
    Content in an article search result container
  </div>
<% end %>

The css_module helper is mostly straightforward to write, but I don't see a stable/supported way of extracting the mapping from classname to mangled name.

@alexander-akait
Copy link
Member

Maybe we can implement getJSON option as it is done in https://github.com/css-modules/postcss-modules

@DanielHeath
Copy link
Author

That would work perfectly

@alexander-akait
Copy link
Member

PR welcome, it is very rare feature, so it is not high priority

@jcmcneal
Copy link

It's surprising this feature doesn't exist already. Seems simple enough to just dump all the mappings in a JSON file. After looking into postcss-modules I don't like how the getJSON works as it creates a .json file for each .less/.scss file. I would prefer to have a flat JSON file with every classname that's been hashed across my app.

Another use case:
We are implementing cypress testing against production builds and it would be extremely helpful to use classnames as selectors instead of adding additional attributes everywhere to select by.

@DanielHeath
Copy link
Author

@jcmcneal Consider your tone. The maintainers have already said 'PR welcome', so "It's surprising this feature doesn't exist already" sounds a little entitled.

@jcmcneal
Copy link

This package is used in 2.4M public repos and when I searched to find out how to get the mappings it did surprise me that there isn't a way yet. However, I'm glad you requested the feature and I only commented to show support that it's wanted by more than just one person.

@DanielHeath
Copy link
Author

Yeah, I figured you didn't mean it that way - just pointing out that it's worth making it clear, especially when dealing with people who don't know you.

stephenkao added a commit to stephenkao/css-loader that referenced this issue Mar 10, 2024
This changeset adds a `getJSON` option to output CSS modules mappings to JSON.  This value can be a boolean or a function, and it employs similar logic to [postcss-modules#getJSON](https://github.com/madyankin/postcss-modules?tab=readme-ov-file#saving-exported-classes) as a function.  This is particularly useful for SSR/SSG/templating languages when CSS modules mappings need to be present at build time.

Addresses [webpack-contrib#988](webpack-contrib#988).
stephenkao added a commit to stephenkao/css-loader that referenced this issue Mar 10, 2024
This changeset adds a `getJSON` option to output CSS modules mappings to JSON.  This value can be a boolean or a function, and it employs similar logic to [postcss-modules#getJSON](https://github.com/madyankin/postcss-modules?tab=readme-ov-file#saving-exported-classes) as a function.  This is particularly useful for SSR/SSG/templating languages when CSS modules mappings need to be present at build time.

Addresses [webpack-contrib#988](webpack-contrib#988).
stephenkao added a commit to stephenkao/css-loader that referenced this issue Mar 10, 2024
This changeset adds a `getJSON` option to output CSS modules mappings to JSON.
This value can be a boolean or a function, and it employs similar logic to
[postcss-modules#getJSON](https://github.com/madyankin/postcss-modules?tab=readme-ov-file#saving-exported-classes) as a function.
This is particularly useful for SSR/SSG/templating languages when CSS modules mappings need to be present at build time.

Addresses [webpack-contrib#988](webpack-contrib#988).
@stephenkao
Copy link
Contributor

Hey, everyone! I know I'm super late to the party, but I need this functionality for a project I'm working on so I've opened up a pull request here: #1577. Feel free to take a look--happy to update and correct issues as needed. Thanks in advance!

(Also, it's my first time working in this repository so please let me know if I missed something in the contribution guidelines!)

stephenkao added a commit to stephenkao/css-loader that referenced this issue Mar 10, 2024
This changeset adds a `getJSON` option to output CSS modules mappings to JSON.
This value can be a boolean or a function, and it employs similar logic to
[postcss-modules#getJSON](https://github.com/madyankin/postcss-modules?tab=readme-ov-file#saving-exported-classes) as a function.
This is particularly useful for SSR/SSG/templating languages when CSS modules mappings need to be present at build time.

Addresses [webpack-contrib#988](webpack-contrib#988).
stephenkao added a commit to stephenkao/css-loader that referenced this issue Apr 4, 2024
This changeset adds a `getJSON` option to output CSS modules mappings to JSON.
This value can be a boolean or a function, and it employs similar logic to
[postcss-modules#getJSON](https://github.com/madyankin/postcss-modules?tab=readme-ov-file#saving-exported-classes) as a function.
This is particularly useful for SSR/SSG/templating languages when CSS modules mappings need to be present at build time.

Addresses [webpack-contrib#988](webpack-contrib#988).
@alexander-akait
Copy link
Member

Fixed by #1577

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

4 participants