Skip to content

Commit 4f76812

Browse files
author
David Liu
committed
feat: add exportGlobals option
1 parent 431f620 commit 4f76812

9 files changed

+107
-10
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,7 @@ module.exports = {
531531
localIdentName: '[path][name]__[local]--[hash:base64:5]',
532532
context: path.resolve(__dirname, 'src'),
533533
hashPrefix: 'my-custom-hash',
534+
exportGlobals: true,
534535
},
535536
},
536537
},
@@ -886,6 +887,33 @@ module.exports = {
886887
};
887888
```
888889

890+
### `exportGlobals`
891+
892+
Type: `Boolean`
893+
Default: `false`
894+
895+
Allow `css-loader` to export names from global class or id, so you can use that as local name.
896+
897+
**webpack.config.js**
898+
899+
```js
900+
module.exports = {
901+
module: {
902+
rules: [
903+
{
904+
test: /\.css$/i,
905+
loader: 'css-loader',
906+
options: {
907+
modules: {
908+
exportGlobals: true,
909+
},
910+
},
911+
},
912+
],
913+
},
914+
};
915+
```
916+
889917
## Examples
890918

891919
### Assets

src/options.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@
6767
"instanceof": "Function"
6868
}
6969
]
70+
},
71+
"exportGlobals": {
72+
"type": "boolean"
7073
}
7174
}
7275
}

src/utils.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ function getModulesPlugins(options, loaderContext) {
103103
getLocalIdent,
104104
hashPrefix: '',
105105
localIdentRegExp: null,
106+
exportGlobals: false,
106107
};
107108

108109
if (
@@ -147,6 +148,7 @@ function getModulesPlugins(options, loaderContext) {
147148

148149
return localIdent;
149150
},
151+
exportGlobals: modulesOptions.exportGlobals,
150152
}),
151153
];
152154
}

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10811,6 +10811,37 @@ Array [
1081110811
1081210812
exports[`"modules" option should work with case \`values-10\` (\`modules\` value is \`true)\`: warnings 1`] = `Array []`;
1081310813
10814+
exports[`"modules" option should work with exportGlobals option: errors 1`] = `Array []`;
10815+
10816+
exports[`"modules" option should work with exportGlobals option: module 1`] = `
10817+
"// Imports
10818+
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../src/runtime/api.js\\");
10819+
exports = ___CSS_LOADER_API_IMPORT___(false);
10820+
// Module
10821+
exports.push([module.id, \\".foo {\\\\n background-color: #ffffff;\\\\n}\\\\n\\", \\"\\"]);
10822+
// Exports
10823+
exports.locals = {
10824+
\\"foo\\": \\"foo\\"
10825+
};
10826+
module.exports = exports;
10827+
"
10828+
`;
10829+
10830+
exports[`"modules" option should work with exportGlobals option: result 1`] = `
10831+
Array [
10832+
Array [
10833+
"./modules/exportGlobals/exportGlobals.css",
10834+
".foo {
10835+
background-color: #ffffff;
10836+
}
10837+
",
10838+
"",
10839+
],
10840+
]
10841+
`;
10842+
10843+
exports[`"modules" option should work with exportGlobals option: warnings 1`] = `Array []`;
10844+
1081410845
exports[`"modules" option should work with the "[local]" placeholder for the "localIdentName" option: errors 1`] = `Array []`;
1081510846
1081610847
exports[`"modules" option should work with the "[local]" placeholder for the "localIdentName" option: module 1`] = `

test/__snapshots__/validate-options.test.js.snap

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,15 @@ exports[`validate options should throw an error on the "modules" option with "{"
4848
- options.modules.context should be a string."
4949
`;
5050

51+
exports[`validate options should throw an error on the "modules" option with "{"exportGlobals":"invalid"}" value 1`] = `
52+
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
53+
- options.modules.exportGlobals should be a boolean."
54+
`;
55+
5156
exports[`validate options should throw an error on the "modules" option with "{"getLocalIdent":[]}" value 1`] = `
5257
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
5358
- options.modules should be one of these:
54-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent? }
59+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent?, exportGlobals? }
5560
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
5661
Details:
5762
* options.modules.getLocalIdent should be one of these:
@@ -74,7 +79,7 @@ exports[`validate options should throw an error on the "modules" option with "{"
7479
exports[`validate options should throw an error on the "modules" option with "{"localIdentRegExp":true}" value 1`] = `
7580
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
7681
- options.modules should be one of these:
77-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent? }
82+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent?, exportGlobals? }
7883
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
7984
Details:
8085
* options.modules.localIdentRegExp should be one of these:
@@ -111,53 +116,53 @@ exports[`validate options should throw an error on the "modules" option with "{"
111116
exports[`validate options should throw an error on the "modules" option with "globals" value 1`] = `
112117
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
113118
- options.modules should be one of these:
114-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent? }
119+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent?, exportGlobals? }
115120
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
116121
Details:
117122
* options.modules should be a boolean.
118123
* options.modules should be one of these:
119124
\\"local\\" | \\"global\\" | \\"pure\\"
120125
* options.modules should be an object:
121-
object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent? }"
126+
object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent?, exportGlobals? }"
122127
`;
123128
124129
exports[`validate options should throw an error on the "modules" option with "locals" value 1`] = `
125130
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
126131
- options.modules should be one of these:
127-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent? }
132+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent?, exportGlobals? }
128133
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
129134
Details:
130135
* options.modules should be a boolean.
131136
* options.modules should be one of these:
132137
\\"local\\" | \\"global\\" | \\"pure\\"
133138
* options.modules should be an object:
134-
object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent? }"
139+
object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent?, exportGlobals? }"
135140
`;
136141
137142
exports[`validate options should throw an error on the "modules" option with "pures" value 1`] = `
138143
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
139144
- options.modules should be one of these:
140-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent? }
145+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent?, exportGlobals? }
141146
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
142147
Details:
143148
* options.modules should be a boolean.
144149
* options.modules should be one of these:
145150
\\"local\\" | \\"global\\" | \\"pure\\"
146151
* options.modules should be an object:
147-
object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent? }"
152+
object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent?, exportGlobals? }"
148153
`;
149154
150155
exports[`validate options should throw an error on the "modules" option with "true" value 1`] = `
151156
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
152157
- options.modules should be one of these:
153-
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent? }
158+
boolean | \\"local\\" | \\"global\\" | \\"pure\\" | object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent?, exportGlobals? }
154159
-> Enables/Disables CSS Modules and their configuration (https://github.com/webpack-contrib/css-loader#modules).
155160
Details:
156161
* options.modules should be a boolean.
157162
* options.modules should be one of these:
158163
\\"local\\" | \\"global\\" | \\"pure\\"
159164
* options.modules should be an object:
160-
object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent? }"
165+
object { mode?, localIdentName?, localIdentRegExp?, context?, hashPrefix?, getLocalIdent?, exportGlobals? }"
161166
`;
162167
163168
exports[`validate options should throw an error on the "onlyLocals" option with "true" value 1`] = `
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:global(.foo) {
2+
background-color: #ffffff;
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import css from './exportGlobals.css';
2+
3+
__export__ = css;
4+
5+
export default css;

test/modules-option.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,4 +565,22 @@ describe('"modules" option', () => {
565565
expect(getWarnings(stats)).toMatchSnapshot('warnings');
566566
expect(getErrors(stats)).toMatchSnapshot('errors');
567567
});
568+
569+
it('should work with exportGlobals option', async () => {
570+
const compiler = getCompiler('./modules/exportGlobals/exportGlobals.js', {
571+
modules: {
572+
exportGlobals: true,
573+
},
574+
});
575+
const stats = await compile(compiler);
576+
577+
expect(
578+
getModuleSource('./modules/exportGlobals/exportGlobals.css', stats)
579+
).toMatchSnapshot('module');
580+
expect(getExecutedCode('main.bundle.js', compiler, stats)).toMatchSnapshot(
581+
'result'
582+
);
583+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
584+
expect(getErrors(stats)).toMatchSnapshot('errors');
585+
});
568586
});

test/validate-options.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ describe('validate options', () => {
2626
{ getLocalIdent: () => {} },
2727
{ localIdentRegExp: 'page-(.*)\\.js' },
2828
{ localIdentRegExp: /page-(.*)\.js/ },
29+
{ exportGlobals: true },
2930
],
3031
failure: [
3132
'true',
@@ -41,6 +42,7 @@ describe('validate options', () => {
4142
{ hashPrefix: true },
4243
{ getLocalIdent: [] },
4344
{ localIdentRegExp: true },
45+
{ exportGlobals: 'invalid' },
4446
],
4547
},
4648
sourceMap: {

0 commit comments

Comments
 (0)