Skip to content

Commit 519e5f4

Browse files
feat: the exportGlobals option for export global classes and ids (#1069)
1 parent 4f158cd commit 519e5f4

15 files changed

+336
-27
lines changed

README.md

+28
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,7 @@ module.exports = {
528528
options: {
529529
modules: {
530530
mode: 'local',
531+
exportGlobals: true,
531532
localIdentName: '[path][name]__[local]--[hash:base64:5]',
532533
context: path.resolve(__dirname, 'src'),
533534
hashPrefix: 'my-custom-hash',
@@ -605,6 +606,33 @@ module.exports = {
605606
};
606607
```
607608

609+
##### `exportGlobals`
610+
611+
Type: `Boolean`
612+
Default: `false`
613+
614+
Allow `css-loader` to export names from global class or id, so you can use that as local name.
615+
616+
**webpack.config.js**
617+
618+
```js
619+
module.exports = {
620+
module: {
621+
rules: [
622+
{
623+
test: /\.css$/i,
624+
loader: 'css-loader',
625+
options: {
626+
modules: {
627+
exportGlobals: true,
628+
},
629+
},
630+
},
631+
],
632+
},
633+
};
634+
```
635+
608636
##### `localIdentName`
609637

610638
Type: `String`

src/options.json

+3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@
4646
}
4747
]
4848
},
49+
"exportGlobals": {
50+
"type": "boolean"
51+
},
4952
"localIdentName": {
5053
"type": "string"
5154
},

src/utils.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ function getFilter(filter, resourcePath, defaultFilter = null) {
9999
function getModulesPlugins(options, loaderContext) {
100100
let modulesOptions = {
101101
mode: 'local',
102+
exportGlobals: false,
102103
localIdentName: '[hash:base64]',
103104
getLocalIdent,
104105
hashPrefix: '',
@@ -116,11 +117,7 @@ function getModulesPlugins(options, loaderContext) {
116117
}
117118

118119
if (typeof modulesOptions.mode === 'function') {
119-
const modeFromFunction = modulesOptions.mode(loaderContext.resourcePath);
120-
121-
if (modeFromFunction === 'local' || modeFromFunction === 'global') {
122-
modulesOptions.mode = modeFromFunction;
123-
}
120+
modulesOptions.mode = modulesOptions.mode(loaderContext.resourcePath);
124121
}
125122

126123
let plugins = [];
@@ -158,6 +155,7 @@ function getModulesPlugins(options, loaderContext) {
158155

159156
return localIdent;
160157
},
158+
exportGlobals: modulesOptions.exportGlobals,
161159
}),
162160
];
163161
} catch (error) {

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

+152-4
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,8 @@ Array [
368368
"ModuleError: Module Error (from \`replaced original path\`):
369369
options.mode must be either \\"global\\", \\"local\\" or \\"pure\\" (default \\"local\\")",
370370
"ModuleError: Module Error (from \`replaced original path\`):
371+
options.mode must be either \\"global\\", \\"local\\" or \\"pure\\" (default \\"local\\")",
372+
"ModuleError: Module Error (from \`replaced original path\`):
371373
options.mode must be either \\"global\\", \\"local\\" or \\"pure\\" (default \\"local\\")",
372374
]
373375
`;
@@ -409,14 +411,31 @@ exports[`"modules" option issue #1063 throw error: result 1`] = `
409411
:local(.otherClassGlobalFile) {
410412
color: coral;
411413
}
414+
.foo :local(.bar) {
415+
color: red;
416+
}
412417
"
413418
`;
414419
415420
exports[`"modules" option issue #1063 throw error: warnings 1`] = `Array []`;
416421
417422
exports[`"modules" option issue #1063: errors 1`] = `Array []`;
418423
419-
exports[`"modules" option issue #1063: module 1`] = `
424+
exports[`"modules" option issue #1063: module with the \`global\` mode 1`] = `
425+
"// Imports
426+
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../src/runtime/api.js\\");
427+
exports = ___CSS_LOADER_API_IMPORT___(false);
428+
// Module
429+
exports.push([module.id, \\".classNameGlobalFile {\\\\n color: black;\\\\n}\\\\n\\\\n._2rcag09JpwrP4_hfyyRmm- {\\\\n color: coral;\\\\n}\\\\n\\", \\"\\"]);
430+
// Exports
431+
exports.locals = {
432+
\\"otherClassGlobalFile\\": \\"_2rcag09JpwrP4_hfyyRmm-\\"
433+
};
434+
module.exports = exports;
435+
"
436+
`;
437+
438+
exports[`"modules" option issue #1063: module with the \`local\` mode 1`] = `
420439
"// Imports
421440
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../src/runtime/api.js\\");
422441
exports = ___CSS_LOADER_API_IMPORT___(false);
@@ -430,15 +449,16 @@ module.exports = exports;
430449
"
431450
`;
432451
433-
exports[`"modules" option issue #1063: module 2`] = `
452+
exports[`"modules" option issue #1063: module with the \`pure\` mode 1`] = `
434453
"// Imports
435454
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../src/runtime/api.js\\");
436455
exports = ___CSS_LOADER_API_IMPORT___(false);
437456
// Module
438-
exports.push([module.id, \\".classNameGlobalFile {\\\\n color: black;\\\\n}\\\\n\\\\n._2rcag09JpwrP4_hfyyRmm- {\\\\n color: coral;\\\\n}\\\\n\\", \\"\\"]);
457+
exports.push([module.id, \\"._1rycxa6QkLdgO7vayuTDvk ._3Otdq1jay-xaQGguOXb-0X {\\\\n color: red;\\\\n}\\\\n\\", \\"\\"]);
439458
// Exports
440459
exports.locals = {
441-
\\"otherClassGlobalFile\\": \\"_2rcag09JpwrP4_hfyyRmm-\\"
460+
\\"foo\\": \\"_1rycxa6QkLdgO7vayuTDvk\\",
461+
\\"bar\\": \\"_3Otdq1jay-xaQGguOXb-0X\\"
442462
};
443463
module.exports = exports;
444464
"
@@ -459,6 +479,9 @@ exports[`"modules" option issue #1063: result 1`] = `
459479
._2rcag09JpwrP4_hfyyRmm- {
460480
color: coral;
461481
}
482+
._1rycxa6QkLdgO7vayuTDvk ._3Otdq1jay-xaQGguOXb-0X {
483+
color: red;
484+
}
462485
"
463486
`;
464487
@@ -11098,3 +11121,128 @@ Array [
1109811121
`;
1109911122
1110011123
exports[`"modules" option should work with the "[local]" placeholder for the "localIdentName" option: warnings 1`] = `Array []`;
11124+
11125+
exports[`"modules" option should work with the \`exportGlobals\` option (the \`mode\` option is \`global\`): errors 1`] = `Array []`;
11126+
11127+
exports[`"modules" option should work with the \`exportGlobals\` option (the \`mode\` option is \`global\`): module 1`] = `
11128+
"// Imports
11129+
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../src/runtime/api.js\\");
11130+
exports = ___CSS_LOADER_API_IMPORT___(false);
11131+
// Module
11132+
exports.push([module.id, \\".QLhabfC4HjcIBmobkGW_v {\\\\n background-color: red;\\\\n}\\\\n\\\\n._1fCLE6vkQLICB5aEeEaLgd {\\\\n background-color: green;\\\\n}\\\\n\\\\n.baz {\\\\n background-color: blue;\\\\n}\\\\n\\", \\"\\"]);
11133+
// Exports
11134+
exports.locals = {
11135+
\\"foo\\": \\"QLhabfC4HjcIBmobkGW_v\\",
11136+
\\"bar\\": \\"_1fCLE6vkQLICB5aEeEaLgd\\",
11137+
\\"baz\\": \\"baz\\"
11138+
};
11139+
module.exports = exports;
11140+
"
11141+
`;
11142+
11143+
exports[`"modules" option should work with the \`exportGlobals\` option (the \`mode\` option is \`global\`): result 1`] = `
11144+
Array [
11145+
Array [
11146+
"./modules/exportGlobals-global/exportGlobals.css",
11147+
".QLhabfC4HjcIBmobkGW_v {
11148+
background-color: red;
11149+
}
11150+
11151+
._1fCLE6vkQLICB5aEeEaLgd {
11152+
background-color: green;
11153+
}
11154+
11155+
.baz {
11156+
background-color: blue;
11157+
}
11158+
",
11159+
"",
11160+
],
11161+
]
11162+
`;
11163+
11164+
exports[`"modules" option should work with the \`exportGlobals\` option (the \`mode\` option is \`global\`): warnings 1`] = `Array []`;
11165+
11166+
exports[`"modules" option should work with the \`exportGlobals\` option (the \`mode\` option is \`local\`): errors 1`] = `Array []`;
11167+
11168+
exports[`"modules" option should work with the \`exportGlobals\` option (the \`mode\` option is \`local\`): module 1`] = `
11169+
"// Imports
11170+
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../src/runtime/api.js\\");
11171+
exports = ___CSS_LOADER_API_IMPORT___(false);
11172+
// Module
11173+
exports.push([module.id, \\".foo {\\\\n background-color: red;\\\\n}\\\\n\\\\n._3loyPgwYlZ-RBGWccx71es {\\\\n background-color: green;\\\\n}\\\\n\\\\n.baz {\\\\n background-color: blue;\\\\n}\\\\n\\", \\"\\"]);
11174+
// Exports
11175+
exports.locals = {
11176+
\\"foo\\": \\"foo\\",
11177+
\\"bar\\": \\"_3loyPgwYlZ-RBGWccx71es\\",
11178+
\\"baz\\": \\"baz\\"
11179+
};
11180+
module.exports = exports;
11181+
"
11182+
`;
11183+
11184+
exports[`"modules" option should work with the \`exportGlobals\` option (the \`mode\` option is \`local\`): result 1`] = `
11185+
Array [
11186+
Array [
11187+
"./modules/exportGlobals-local/exportGlobals.css",
11188+
".foo {
11189+
background-color: red;
11190+
}
11191+
11192+
._3loyPgwYlZ-RBGWccx71es {
11193+
background-color: green;
11194+
}
11195+
11196+
.baz {
11197+
background-color: blue;
11198+
}
11199+
",
11200+
"",
11201+
],
11202+
]
11203+
`;
11204+
11205+
exports[`"modules" option should work with the \`exportGlobals\` option (the \`mode\` option is \`local\`): warnings 1`] = `Array []`;
11206+
11207+
exports[`"modules" option should work with the \`exportGlobals\` option (the \`mode\` option is \`pure\`): errors 1`] = `Array []`;
11208+
11209+
exports[`"modules" option should work with the \`exportGlobals\` option (the \`mode\` option is \`pure\`): module 1`] = `
11210+
"// Imports
11211+
var ___CSS_LOADER_API_IMPORT___ = require(\\"../../../../src/runtime/api.js\\");
11212+
exports = ___CSS_LOADER_API_IMPORT___(false);
11213+
// Module
11214+
exports.push([module.id, \\"._3OALfsKrVzw8QnGzfteWls {\\\\n background-color: red;\\\\n}\\\\n\\\\n._3w1OTKuiiZvf8WY1tCPaZH ._1cmRmgT7HI056zoTdHDOVh {\\\\n background-color: green;\\\\n}\\\\n\\\\n._3pB9KhpUqVA1pPH1Y8ei3y .baz {\\\\n background-color: blue;\\\\n}\\\\n\\", \\"\\"]);
11215+
// Exports
11216+
exports.locals = {
11217+
\\"foo\\": \\"_3OALfsKrVzw8QnGzfteWls\\",
11218+
\\"one\\": \\"_3w1OTKuiiZvf8WY1tCPaZH\\",
11219+
\\"bar\\": \\"_1cmRmgT7HI056zoTdHDOVh\\",
11220+
\\"two\\": \\"_3pB9KhpUqVA1pPH1Y8ei3y\\",
11221+
\\"baz\\": \\"baz\\"
11222+
};
11223+
module.exports = exports;
11224+
"
11225+
`;
11226+
11227+
exports[`"modules" option should work with the \`exportGlobals\` option (the \`mode\` option is \`pure\`): result 1`] = `
11228+
Array [
11229+
Array [
11230+
"./modules/exportGlobals-pure/exportGlobals.css",
11231+
"._3OALfsKrVzw8QnGzfteWls {
11232+
background-color: red;
11233+
}
11234+
11235+
._3w1OTKuiiZvf8WY1tCPaZH ._1cmRmgT7HI056zoTdHDOVh {
11236+
background-color: green;
11237+
}
11238+
11239+
._3pB9KhpUqVA1pPH1Y8ei3y .baz {
11240+
background-color: blue;
11241+
}
11242+
",
11243+
"",
11244+
],
11245+
]
11246+
`;
11247+
11248+
exports[`"modules" option should work with the \`exportGlobals\` option (the \`mode\` option is \`pure\`): warnings 1`] = `Array []`;

0 commit comments

Comments
 (0)