Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add exportGlobals option #1052

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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