Css-loader: Inline local classNames into javascript

Created on 17 Sep 2015  路  6Comments  路  Source: webpack-contrib/css-loader

Hey @sokra,

Right now I'm writing code like this:

var styles = require 'foo/bar.css'

module.exports = class Foo extends React.Component

  render: =>
    <div className={styles.foo}></div>

The problem is that when compiled and minified I still see the original names, and there's a module that exports: {"foo": "Xwvutsr123456"}. Is there a way we could pre-process the file so that styles.foo is inlined (as though we were using webpack.DefinePlugin)?

5 (nice to have) Minor Feature

All 6 comments

@ConradIrwin Please elaborate futher what you mean by inline please? 馃檭

@michael-ciniawsky

My example currently compiles to:

# foo/bar.css.js

styles = {
  foo: 'Xwvutsr123456'
}
# main.js
var styles = __webpack_require__('foo/bar.css')

...

function render() {
    React.createElement("div", {className: styles.foo})
}

...



md5-b85d42bf2505b1ed258051a7a87a2757



# main.js
__webpack_require__('foo/bar.css')

function render() {
  React.createElement("div", {className: 'Xwvutsr123456'})
}

This avoids creating an extra variable, and an extra property acccess (which is hard to minify).

Not sure how this would work, as it seems like a more generally useful compiler phase than just limited to CSS, but thought I'd put it here and see if anyone else was inspired to fix it :)

function render () {
   React.createElement("div", { className: __webpack_require__('foo/bar.css').className })
}

@ConradIrwin ? But I don't think this is really feasible atm, the locals could be exported as constants when ES2015 Module support lands in css-loader (v1.0.0) 馃槢

export const className = 'Xwvutsr123456'

instead of a JSON-ish {Object} like it's currently the case

exports.locals = {...modules}
import { className1, className2, className3 } from './style.css'

function render () {
  React.createElement("div", { className: className1 })
}

I've also been thinking about how to achieve this. Maybe the responsibility should be on something more general like prepack to pre-evaluate and inline the object references instead of css-loader itself?

Other solution use https://github.com/kentcdodds/babel-plugin-macros, but we need implement api, it is feature, feel free to PR

Sorry, impossible solve on css-loader side, use https://github.com/gajus/babel-plugin-react-css-modules#readme

Was this page helpful?
0 / 5 - 0 ratings