Skip to content

Commit e9327c0

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

File tree

7 files changed

+529
-549
lines changed

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.

‎test/__snapshots__/modules-option.test.js.snap

+480-480
Large diffs are not rendered by default.

‎test/helpers.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@ const moduleConfig = (config) => {
8585
test: (config.loader && config.loader.test) || /\.css$/,
8686
use: [
8787
{
88-
loader: config.localsLoader
89-
? path.resolve(__dirname, '../locals.js')
90-
: path.resolve(__dirname, '../index.js'),
88+
loader: path.resolve(__dirname, '../index.js'),
9189
options: (config.loader && config.loader.options) || {},
9290
},
9391
].concat(

‎test/modules-option.test.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,21 @@ const { webpack, evaluated } = require('./helpers');
66
const testCasesPath = path.join(__dirname, 'fixtures/modules/tests-cases');
77
const testCases = fs.readdirSync(testCasesPath);
88

9-
describe('modules option', () => {
10-
[false, true].forEach((isLocalsLoader) => {
9+
describe('modules', () => {
10+
[false, true].forEach((exportOnlyLocalsValue) => {
1111
[false, true].forEach((modulesValue) => {
1212
testCases.forEach((name) => {
13-
it(`case name \`${name}\`: (use \`${
14-
isLocalsLoader ? 'localsLoader.js' : 'loader.js'
15-
}\`) (\`modules\` option is ${modulesValue})`, async () => {
13+
it(`case \`${name}\`: (export \`${
14+
exportOnlyLocalsValue ? 'only locals' : 'all'
15+
}\`) (\`modules\` value is \`${modulesValue})\``, async () => {
1616
const config = {
1717
loader: {
18-
options: { modules: modulesValue, localIdentName: '_[local]' },
18+
options: {
19+
modules: modulesValue,
20+
exportOnlyLocals: exportOnlyLocalsValue,
21+
localIdentName: '_[local]',
22+
},
1923
},
20-
localsLoader: isLocalsLoader,
2124
};
2225
const testId = `./modules/tests-cases/${name}/source.css`;
2326
const stats = await webpack(testId, config);

0 commit comments

Comments
 (0)
Please sign in to comment.