Skip to content

Commit

Permalink
refactor: rename the onlyLocals option (#1116)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: the `onlyLocals` option was renamed to the `exportOnlyLocals` option and moved to the `module` option
  • Loading branch information
evilebottnawi committed Jul 21, 2020
1 parent ac5f413 commit e1c55e4
Show file tree
Hide file tree
Showing 10 changed files with 355 additions and 359 deletions.
62 changes: 32 additions & 30 deletions README.md
Expand Up @@ -116,7 +116,6 @@ module.exports = {
| **[`modules`](#modules)** | `{Boolean\|String\|Object}` | `false` | Enables/Disables CSS Modules and their configuration |
| **[`sourceMap`](#sourcemap)** | `{Boolean}` | `compiler.devtool` | Enables/Disables generation of source maps |
| **[`importLoaders`](#importloaders)** | `{Number}` | `0` | Enables/Disables or setups number of loaders applied before CSS loader |
| **[`onlyLocals`](#onlylocals)** | `{Boolean}` | `false` | Export only locals |
| **[`esModule`](#esmodule)** | `{Boolean}` | `false` | Use ES modules syntax |

### `url`
Expand Down Expand Up @@ -535,6 +534,7 @@ module.exports = {
context: path.resolve(__dirname, 'src'),
hashPrefix: 'my-custom-hash',
namedExport: true,
exportOnlyLocals: false,
},
},
},
Expand Down Expand Up @@ -968,6 +968,37 @@ module.exports = {
};
```

### `exportOnlyLocals`

Type: `Boolean`
Default: `false`

Export only locals.

**Useful** when you use **css modules** for pre-rendering (for example SSR).
For pre-rendering with `mini-css-extract-plugin` you should use this option instead of `style-loader!css-loader` **in the pre-rendering bundle**.
It doesn't embed CSS but only exports the identifier mappings.

**webpack.config.js**

```js
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
loader: 'css-loader',
options: {
modules: {
exportOnlyLocals: true,
},
},
},
],
},
};
```

### `sourceMap`

Type: `Boolean`
Expand Down Expand Up @@ -1032,35 +1063,6 @@ module.exports = {

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

### `onlyLocals`

Type: `Boolean`
Default: `false`

Export only locals.

**Useful** when you use **css modules** for pre-rendering (for example SSR).
For pre-rendering with `mini-css-extract-plugin` you should use this option instead of `style-loader!css-loader` **in the pre-rendering bundle**.
It doesn't embed CSS but only exports the identifier mappings.

**webpack.config.js**

```js
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
loader: 'css-loader',
options: {
onlyLocals: true,
},
},
],
},
};
```

### `esModule`

Type: `Boolean`
Expand Down
8 changes: 4 additions & 4 deletions src/options.json
Expand Up @@ -104,6 +104,10 @@
"namedExport": {
"description": "Use the named export ES modules.",
"type": "boolean"
},
"exportOnlyLocals": {
"description": "Export only locals (https://github.com/webpack-contrib/css-loader#exportonlylocals).",
"type": "boolean"
}
}
}
Expand All @@ -124,10 +128,6 @@
}
]
},
"onlyLocals": {
"description": "Export only locals (https://github.com/webpack-contrib/css-loader#onlylocals).",
"type": "boolean"
},
"esModule": {
"description": "Use the ES modules syntax (https://github.com/webpack-contrib/css-loader#esmodule).",
"type": "boolean"
Expand Down
13 changes: 5 additions & 8 deletions src/utils.js
Expand Up @@ -123,6 +123,7 @@ function getModulesOptions(rawOptions, loaderContext) {
hashPrefix: '',
exportGlobals: false,
namedExport: false,
exportOnlyLocals: false,
};

if (
Expand Down Expand Up @@ -183,17 +184,13 @@ function normalizeOptions(rawOptions, loaderContext) {
? rawOptions.sourceMap
: loaderContext.sourceMap,
importLoaders: rawOptions.importLoaders,
onlyLocals:
typeof rawOptions.onlyLocals !== 'undefined'
? rawOptions.onlyLocals
: false,
esModule:
typeof rawOptions.esModule === 'undefined' ? true : rawOptions.esModule,
};
}

function shouldUseImportPlugin(options) {
if (options.onlyLocals) {
if (options.modules.exportOnlyLocals) {
return false;
}

Expand All @@ -205,7 +202,7 @@ function shouldUseImportPlugin(options) {
}

function shouldUseURLPlugin(options) {
if (options.onlyLocals) {
if (options.modules.exportOnlyLocals) {
return false;
}

Expand Down Expand Up @@ -326,7 +323,7 @@ function getPreRequester({ loaders, loaderIndex }) {
function getImportCode(loaderContext, imports, options) {
let code = '';

if (options.onlyLocals !== true) {
if (options.modules.exportOnlyLocals !== true) {
const apiUrl = stringifyRequest(
loaderContext,
require.resolve('./runtime/api')
Expand Down Expand Up @@ -357,7 +354,7 @@ function getModuleCode(
icssReplacements,
options
) {
if (options.onlyLocals === true) {
if (options.modules.exportOnlyLocals === true) {
return 'var ___CSS_LOADER_EXPORT___ = {};\n';
}

Expand Down

0 comments on commit e1c55e4

Please sign in to comment.