Skip to content

Commit 61acac2

Browse files
refactor!: modules.namedExport is try by default
1 parent b6ec7fe commit 61acac2

File tree

98 files changed

+4904
-3478
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+4904
-3478
lines changed

src/utils.js

+22-13
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ function getValidLocalName(localName, exportLocalsConvention) {
490490
const IS_MODULES = /\.module(s)?\.\w+$/i;
491491
const IS_ICSS = /\.icss\.\w+$/i;
492492

493-
function getModulesOptions(rawOptions, exportType, loaderContext) {
493+
function getModulesOptions(rawOptions, esModule, exportType, loaderContext) {
494494
if (typeof rawOptions.modules === "boolean" && rawOptions.modules === false) {
495495
return false;
496496
}
@@ -519,6 +519,16 @@ function getModulesOptions(rawOptions, exportType, loaderContext) {
519519
const { outputOptions } = loaderContext._compilation;
520520
const needNamedExport =
521521
exportType === "css-style-sheet" || exportType === "string";
522+
const namedExport =
523+
typeof rawModulesOptions.namedExport !== "undefined"
524+
? rawModulesOptions.namedExport
525+
: needNamedExport || esModule;
526+
const exportLocalsConvention =
527+
typeof rawModulesOptions.exportLocalsConvention !== "undefined"
528+
? rawModulesOptions.exportLocalsConvention
529+
: namedExport
530+
? "asIs"
531+
: "camelCaseOnly";
522532
const modulesOptions = {
523533
auto,
524534
mode: "local",
@@ -533,22 +543,19 @@ function getModulesOptions(rawOptions, exportType, loaderContext) {
533543
localIdentRegExp: undefined,
534544
// eslint-disable-next-line no-undefined
535545
getLocalIdent: undefined,
536-
namedExport: needNamedExport || false,
537-
exportLocalsConvention:
538-
(rawModulesOptions.namedExport === true || needNamedExport) &&
539-
typeof rawModulesOptions.exportLocalsConvention === "undefined"
540-
? "camelCaseOnly"
541-
: "asIs",
546+
// TODO improve me and enable by default
542547
exportOnlyLocals: false,
543548
...rawModulesOptions,
544-
useExportsAs: rawModulesOptions.exportLocalsConvention === "asIs",
549+
exportLocalsConvention,
550+
namedExport,
545551
};
546552

547553
let exportLocalsConventionType;
548554

549555
if (typeof modulesOptions.exportLocalsConvention === "string") {
550556
exportLocalsConventionType = modulesOptions.exportLocalsConvention;
551557

558+
modulesOptions.useExportsAs = exportLocalsConventionType === "asIs";
552559
modulesOptions.exportLocalsConvention = (name) => {
553560
switch (exportLocalsConventionType) {
554561
case "camelCase": {
@@ -614,7 +621,7 @@ function getModulesOptions(rawOptions, exportType, loaderContext) {
614621
}
615622

616623
if (needNamedExport) {
617-
if (rawOptions.esModule === false) {
624+
if (esModule === false) {
618625
throw new Error(
619626
"The 'exportType' option with the 'css-style-sheet' or 'string' value requires the 'esModule' option to be enabled",
620627
);
@@ -628,7 +635,7 @@ function getModulesOptions(rawOptions, exportType, loaderContext) {
628635
}
629636

630637
if (modulesOptions.namedExport === true) {
631-
if (rawOptions.esModule === false) {
638+
if (esModule === false) {
632639
throw new Error(
633640
"The 'modules.namedExport' option requires the 'esModule' option to be enabled",
634641
);
@@ -641,7 +648,7 @@ function getModulesOptions(rawOptions, exportType, loaderContext) {
641648
exportLocalsConventionType !== "dashesOnly"
642649
) {
643650
throw new Error(
644-
'The "modules.namedExport" option requires the "modules.exportLocalsConvention" option to be "camelCaseOnly" or "dashesOnly"',
651+
'The "modules.namedExport" option requires the "modules.exportLocalsConvention" option to be "asIs", "camelCaseOnly" or "dashesOnly"',
645652
);
646653
}
647654
}
@@ -654,8 +661,11 @@ function normalizeOptions(rawOptions, loaderContext) {
654661
typeof rawOptions.exportType === "undefined"
655662
? "array"
656663
: rawOptions.exportType;
664+
const esModule =
665+
typeof rawOptions.esModule === "undefined" ? true : rawOptions.esModule;
657666
const modulesOptions = getModulesOptions(
658667
rawOptions,
668+
esModule,
659669
exportType,
660670
loaderContext,
661671
);
@@ -672,8 +682,7 @@ function normalizeOptions(rawOptions, loaderContext) {
672682
typeof rawOptions.importLoaders === "string"
673683
? parseInt(rawOptions.importLoaders, 10)
674684
: rawOptions.importLoaders,
675-
esModule:
676-
typeof rawOptions.esModule === "undefined" ? true : rawOptions.esModule,
685+
esModule,
677686
exportType,
678687
};
679688
}

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

+4-7
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ ___CSS_LOADER_EXPORT___.push([module.id, \`@charset "UTF-8";
133133
}
134134
\`, ""]);
135135
// Exports
136-
___CSS_LOADER_EXPORT___.locals = {};
137136
export default ___CSS_LOADER_EXPORT___;
138137
"
139138
`;
@@ -189,9 +188,8 @@ ___CSS_LOADER_EXPORT___.push([module.id, \`@charset "UTF-8";
189188
}
190189
\`, ""]);
191190
// Exports
192-
___CSS_LOADER_EXPORT___.locals = {
193-
"class": \`OZJqogC5EaF_wROug7zE\`
194-
};
191+
var _1 = \`OZJqogC5EaF_wROug7zE\`;
192+
export { _1 as "class" };
195193
export default ___CSS_LOADER_EXPORT___;
196194
"
197195
`;
@@ -247,9 +245,8 @@ ___CSS_LOADER_EXPORT___.push([module.id, \`@charset "UTF-8";
247245
}
248246
\`, ""]);
249247
// Exports
250-
___CSS_LOADER_EXPORT___.locals = {
251-
"class": \`OZJqogC5EaF_wROug7zE\`
252-
};
248+
var _1 = \`OZJqogC5EaF_wROug7zE\`;
249+
export { _1 as "class" };
253250
export default ___CSS_LOADER_EXPORT___;
254251
"
255252
`;

0 commit comments

Comments
 (0)