Skip to content

Commit e9327c0

Browse files
feat: exportOnlyLocals option (#824)
BREAKING CHANGE: `css-loader/locals` was dropped in favor `exportOnlyLocals` option
1 parent 2e62346 commit e9327c0

7 files changed

+529
-549
lines changed

README.md

+17-2
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ It's useful when you, for instance, need to post process the CSS as a string.
102102
|**[`sourceMap`](#sourcemap)**|`{Boolean}`|`false`|Enable/Disable Sourcemaps|
103103
|**[`camelCase`](#camelcase)**|`{Boolean\|String}`|`false`|Export Classnames in CamelCase|
104104
|**[`importLoaders`](#importloaders)**|`{Number}`|`0`|Number of loaders applied before CSS loader|
105+
|**[`exportOnlyLocals`](#exportonlylocals)**|`{Boolean}`|`false`|Export only locals|
105106

106107
### `url`
107108

@@ -277,8 +278,6 @@ You can also specify the absolute path to your custom `getLocalIdent` function t
277278
}
278279
```
279280

280-
> ℹ️ 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.
281-
282281
### `sourceMap`
283282

284283
To include source maps set the `sourceMap` option.
@@ -352,6 +351,22 @@ The query parameter `importLoaders` allows you to configure how many loaders bef
352351

353352
This may change in the future when the module system (i. e. webpack) supports loader matching by origin.
354353

354+
### `exportOnlyLocals`
355+
356+
Export only locals (**useful** when you use **css modules**).
357+
For pre-rendering with `mini-css-extract-plugin` you should use this option instead of `style-loader!css-loader` **in the pre-rendering bundle**.
358+
It doesn't embed CSS but only exports the identifier mappings.
359+
360+
**webpack.config.js**
361+
```js
362+
{
363+
loader: 'css-loader',
364+
options: {
365+
exportOnlyLocals: true
366+
}
367+
}
368+
```
369+
355370
<h2 align="center">Examples</h2>
356371

357372
### Assets

lib/loader.js

+21-7
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,30 @@ module.exports = function loader(content, map) {
4747
return callback(err);
4848
}
4949

50-
let cssAsString = JSON.stringify(result.source);
51-
5250
// for importing CSS
5351
const importUrlPrefix = getImportPrefix(this, options);
5452

53+
let exportJs = compileExports(
54+
result,
55+
placeholderImportItemReplacer(
56+
this,
57+
result,
58+
importUrlPrefix,
59+
options.exportOnlyLocals
60+
),
61+
options.camelCase
62+
);
63+
64+
if (options.exportOnlyLocals) {
65+
if (exportJs) {
66+
exportJs = `module.exports = ${exportJs};`;
67+
}
68+
69+
return callback(null, exportJs);
70+
}
71+
72+
let cssAsString = JSON.stringify(result.source);
73+
5574
const alreadyImported = {};
5675
const importJs = result.importItems
5776
.filter((imp) => {
@@ -127,11 +146,6 @@ module.exports = function loader(content, map) {
127146
});
128147
}
129148

130-
let exportJs = compileExports(
131-
result,
132-
placeholderImportItemReplacer(this, result, importUrlPrefix),
133-
options.camelCase
134-
);
135149
if (exportJs) {
136150
exportJs = `exports.locals = ${exportJs};`;
137151
}

lib/localsLoader.js

-45
This file was deleted.

locals.js

-5
This file was deleted.

0 commit comments

Comments
 (0)