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 #143

Closed
andersekdahl opened this issue Sep 22, 2015 · 4 comments
Closed

Produce JSON file(s) with class mappings #143

andersekdahl opened this issue Sep 22, 2015 · 4 comments

Comments

@andersekdahl
Copy link

Hi,
I'm looking to use the local class names from outside of Javascript (in Razor templates on the server). In order to do that, I need to produce one or more JSON files which contains the original file names and their class mappings.

Could someone lead me in the right direction for doing this? That is, produce a file looking something like this:

{"folder/file1.scss": {
  "class1": "hcgvx657hv67",
  "class2": "uh3hj573"
}, "file2.scss": { 
  ... 
}}
@sokra
Copy link
Member

sokra commented Sep 23, 2015

The css-loader/locals does this. It only exports a mappings object for i. e. server side rendering. This way you can compile a webpack bundle which contains only the mappings.

var context = require.context(".", true, /\.scss$/);
var obj = context.keys().reduce(function(obj, key) {
  obj[key] = context(key);
  return obj;
}, {});
obj // {"./folder/file1.scss": { "class1": ...
test: /\.scss$/,
loader: "css-loader/locals!scss-loader"

@andersekdahl
Copy link
Author

Just the help I needed, thank you!

@andersekdahl
Copy link
Author

Just wanted to let you know that I've created a loader that fits my needs: https://www.npmjs.com/package/extract-css-module-classnames-loader

It doesn't use css-loader/locals but it uses css-loader/lib/processCss to extract the class names and write them to a JSON file during build.

@frenic
Copy link

frenic commented Sep 26, 2015

+1

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

No branches or pull requests

3 participants