Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,27 @@ You can use `:local(#someId)`, but this is not recommended. Use classes instead

You can configure the generated ident with the `localIdentName` query parameter (default `[hash:base64]`). Example: `css-loader?localIdentName=[path][name]---[local]---[hash:base64:5]` for easier debugging.

You can also specify the absolute path to your custom `getLocalIdent` function to generate classname based on a different schema. Note that this requires `webpack@2` since to be able to pass function in. For example:

```js
{
test: /\.css$/,
loaders: [
{
loader: 'css-loader',
query: {
modules: true,
importLoaders: 1,
getLocalIdent: function (loaderContext, localIdentName, localName, options) {
return 'whatever_random_class_name'
}
}
}
]
},
```


Note: For prerendering with extract-text-webpack-plugin you should use `css-loader/locals` instead of `style-loader!css-loader` **in the prerendering bundle**. It doesn't embed CSS but only exports the identifier mappings.

### CSS Modules
Expand Down
7 changes: 4 additions & 3 deletions lib/processCss.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ var parserPlugin = postcss.plugin("css-loader-parser", function(options) {
});

module.exports = function processCss(inputSource, inputMap, options, callback) {

var query = options.query;
var root = query.root;
var context = query.context;
Expand All @@ -141,6 +140,8 @@ module.exports = function processCss(inputSource, inputMap, options, callback) {
var forceMinimize = query.minimize;
var minimize = typeof forceMinimize !== "undefined" ? !!forceMinimize : options.minimize;

var customGetLocalIdent = query.getLocalIdent || getLocalIdent;

var parserOptions = {
root: root,
mode: options.mode,
Expand All @@ -166,8 +167,8 @@ module.exports = function processCss(inputSource, inputMap, options, callback) {
extractImports(),
modulesValues,
modulesScope({
generateScopedName: function(exportName) {
return getLocalIdent(options.loaderContext, localIdentName, exportName, {
generateScopedName: function generateScopedName (exportName) {
return customGetLocalIdent(options.loaderContext, localIdentName, exportName, {
regExp: localIdentRegExp,
hashPrefix: query.hashPrefix || "",
context: context
Expand Down
19 changes: 19 additions & 0 deletions test/customGetLocalIdentTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*globals describe */

var testLocals = require("./helpers").testLocals;

describe("customGetLocalIdent", function() {
testLocals("should return only locals",
".abc :local(.def) { color: red; } :local .ghi .jkl { color: blue; }",
{
def: "foo",
ghi: "foo",
jkl: "foo"
},
{
getLocalIdent: function () {
return 'foo'
}
}
);
});