Skip to content

Commit

Permalink
feat: add exportGlobals option
Browse files Browse the repository at this point in the history
  • Loading branch information
David Liu committed Mar 26, 2020
1 parent 431f620 commit 4f76812
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 10 deletions.
28 changes: 28 additions & 0 deletions README.md
Expand Up @@ -531,6 +531,7 @@ module.exports = {
localIdentName: '[path][name]__[local]--[hash:base64:5]',
context: path.resolve(__dirname, 'src'),
hashPrefix: 'my-custom-hash',
exportGlobals: true,
},
},
},
Expand Down Expand Up @@ -886,6 +887,33 @@ module.exports = {
};
```

### `exportGlobals`

Type: `Boolean`
Default: `false`

Allow `css-loader` to export names from global class or id, so you can use that as local name.

**webpack.config.js**

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

## Examples

### Assets
Expand Down
3 changes: 3 additions & 0 deletions src/options.json
Expand Up @@ -67,6 +67,9 @@
"instanceof": "Function"
}
]
},
"exportGlobals": {
"type": "boolean"
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/utils.js
Expand Up @@ -103,6 +103,7 @@ function getModulesPlugins(options, loaderContext) {
getLocalIdent,
hashPrefix: '',
localIdentRegExp: null,
exportGlobals: false,
};

if (
Expand Down Expand Up @@ -147,6 +148,7 @@ function getModulesPlugins(options, loaderContext) {

return localIdent;
},
exportGlobals: modulesOptions.exportGlobals,
}),
];
}
Expand Down
31 changes: 31 additions & 0 deletions test/__snapshots__/modules-option.test.js.snap
Expand Up @@ -10811,6 +10811,37 @@ Array [
exports[`"modules" option should work with case \`values-10\` (\`modules\` value is \`true)\`: warnings 1`] = `Array []`;
exports[`"modules" option should work with exportGlobals option: errors 1`] = `Array []`;
exports[`"modules" option should work with exportGlobals option: module 1`] = `
"// Imports
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../src/runtime/api.js\\");
exports = ___CSS_LOADER_API_IMPORT___(false);
// Module
exports.push([module.id, \\".foo {\\\\n background-color: #ffffff;\\\\n}\\\\n\\", \\"\\"]);
// Exports
exports.locals = {
\\"foo\\": \\"foo\\"
};
module.exports = exports;
"
`;
exports[`"modules" option should work with exportGlobals option: result 1`] = `
Array [
Array [
"./modules/exportGlobals/exportGlobals.css",
".foo {
background-color: #ffffff;
}
",
"",
],
]
`;
exports[`"modules" option should work with exportGlobals option: warnings 1`] = `Array []`;
exports[`"modules" option should work with the "[local]" placeholder for the "localIdentName" option: errors 1`] = `Array []`;
exports[`"modules" option should work with the "[local]" placeholder for the "localIdentName" option: module 1`] = `
Expand Down
25 changes: 15 additions & 10 deletions test/__snapshots__/validate-options.test.js.snap
Expand Up @@ -48,10 +48,15 @@ exports[`validate options should throw an error on the "modules" option with "{"
- options.modules.context should be a string."
`;

exports[`validate options should throw an error on the "modules" option with "{"exportGlobals":"invalid"}" value 1`] = `
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
- options.modules.exportGlobals should be a boolean."
`;

exports[`validate options should throw an error on the "modules" option with "{"getLocalIdent":[]}" value 1`] = `
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
- options.modules should be one of these:
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent? }
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent?, exportGlobals? }
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
Details:
* options.modules.getLocalIdent should be one of these:
Expand All @@ -74,7 +79,7 @@ exports[`validate options should throw an error on the "modules" option with "{"
exports[`validate options should throw an error on the "modules" option with "{"localIdentRegExp":true}" value 1`] = `
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
- options.modules should be one of these:
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent? }
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent?, exportGlobals? }
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
Details:
* options.modules.localIdentRegExp should be one of these:
Expand Down Expand Up @@ -111,53 +116,53 @@ exports[`validate options should throw an error on the "modules" option with "{"
exports[`validate options should throw an error on the "modules" option with "globals" value 1`] = `
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
- options.modules should be one of these:
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent? }
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent?, exportGlobals? }
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
Details:
* options.modules should be a boolean.
* options.modules should be one of these:
\\"local\\" | \\"global\\" | \\"pure\\"
* options.modules should be an object:
object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent? }"
object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent?, exportGlobals? }"
`;
exports[`validate options should throw an error on the "modules" option with "locals" value 1`] = `
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
- options.modules should be one of these:
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent? }
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent?, exportGlobals? }
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
Details:
* options.modules should be a boolean.
* options.modules should be one of these:
\\"local\\" | \\"global\\" | \\"pure\\"
* options.modules should be an object:
object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent? }"
object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent?, exportGlobals? }"
`;
exports[`validate options should throw an error on the "modules" option with "pures" value 1`] = `
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
- options.modules should be one of these:
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent? }
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent?, exportGlobals? }
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
Details:
* options.modules should be a boolean.
* options.modules should be one of these:
\\"local\\" | \\"global\\" | \\"pure\\"
* options.modules should be an object:
object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent? }"
object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent?, exportGlobals? }"
`;
exports[`validate options should throw an error on the "modules" option with "true" value 1`] = `
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
- options.modules should be one of these:
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent? }
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent?, exportGlobals? }
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
Details:
* options.modules should be a boolean.
* options.modules should be one of these:
\\"local\\" | \\"global\\" | \\"pure\\"
* options.modules should be an object:
object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent? }"
object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent?, exportGlobals? }"
`;
exports[`validate options should throw an error on the "onlyLocals" option with "true" value 1`] = `
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/modules/exportGlobals/exportGlobals.css
@@ -0,0 +1,3 @@
:global(.foo) {
background-color: #ffffff;
}
5 changes: 5 additions & 0 deletions test/fixtures/modules/exportGlobals/exportGlobals.js
@@ -0,0 +1,5 @@
import css from './exportGlobals.css';

__export__ = css;

export default css;
18 changes: 18 additions & 0 deletions test/modules-option.test.js
Expand Up @@ -565,4 +565,22 @@ describe('"modules" option', () => {
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it('should work with exportGlobals option', async () => {
const compiler = getCompiler('./modules/exportGlobals/exportGlobals.js', {
modules: {
exportGlobals: true,
},
});
const stats = await compile(compiler);

expect(
getModuleSource('./modules/exportGlobals/exportGlobals.css', stats)
).toMatchSnapshot('module');
expect(getExecutedCode('main.bundle.js', compiler, stats)).toMatchSnapshot(
'result'
);
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});
});
2 changes: 2 additions & 0 deletions test/validate-options.test.js
Expand Up @@ -26,6 +26,7 @@ describe('validate options', () => {
{ getLocalIdent: () => {} },
{ localIdentRegExp: 'page-(.*)\\.js' },
{ localIdentRegExp: /page-(.*)\.js/ },
{ exportGlobals: true },
],
failure: [
'true',
Expand All @@ -41,6 +42,7 @@ describe('validate options', () => {
{ hashPrefix: true },
{ getLocalIdent: [] },
{ localIdentRegExp: true },
{ exportGlobals: 'invalid' },
],
},
sourceMap: {
Expand Down

0 comments on commit 4f76812

Please sign in to comment.