Skip to content

Commit

Permalink
refactor!: modules.namedExport is try by default
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Apr 4, 2024
1 parent b6ec7fe commit 61acac2
Show file tree
Hide file tree
Showing 98 changed files with 4,904 additions and 3,478 deletions.
35 changes: 22 additions & 13 deletions src/utils.js
Expand Up @@ -490,7 +490,7 @@ function getValidLocalName(localName, exportLocalsConvention) {
const IS_MODULES = /\.module(s)?\.\w+$/i;
const IS_ICSS = /\.icss\.\w+$/i;

function getModulesOptions(rawOptions, exportType, loaderContext) {
function getModulesOptions(rawOptions, esModule, exportType, loaderContext) {
if (typeof rawOptions.modules === "boolean" && rawOptions.modules === false) {
return false;
}
Expand Down Expand Up @@ -519,6 +519,16 @@ function getModulesOptions(rawOptions, exportType, loaderContext) {
const { outputOptions } = loaderContext._compilation;
const needNamedExport =
exportType === "css-style-sheet" || exportType === "string";
const namedExport =
typeof rawModulesOptions.namedExport !== "undefined"
? rawModulesOptions.namedExport
: needNamedExport || esModule;
const exportLocalsConvention =
typeof rawModulesOptions.exportLocalsConvention !== "undefined"
? rawModulesOptions.exportLocalsConvention
: namedExport
? "asIs"
: "camelCaseOnly";
const modulesOptions = {
auto,
mode: "local",
Expand All @@ -533,22 +543,19 @@ function getModulesOptions(rawOptions, exportType, loaderContext) {
localIdentRegExp: undefined,
// eslint-disable-next-line no-undefined
getLocalIdent: undefined,
namedExport: needNamedExport || false,
exportLocalsConvention:
(rawModulesOptions.namedExport === true || needNamedExport) &&
typeof rawModulesOptions.exportLocalsConvention === "undefined"
? "camelCaseOnly"
: "asIs",
// TODO improve me and enable by default
exportOnlyLocals: false,
...rawModulesOptions,
useExportsAs: rawModulesOptions.exportLocalsConvention === "asIs",
exportLocalsConvention,
namedExport,
};

let exportLocalsConventionType;

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

modulesOptions.useExportsAs = exportLocalsConventionType === "asIs";
modulesOptions.exportLocalsConvention = (name) => {
switch (exportLocalsConventionType) {
case "camelCase": {
Expand Down Expand Up @@ -614,7 +621,7 @@ function getModulesOptions(rawOptions, exportType, loaderContext) {
}

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

if (modulesOptions.namedExport === true) {
if (rawOptions.esModule === false) {
if (esModule === false) {
throw new Error(
"The 'modules.namedExport' option requires the 'esModule' option to be enabled",
);
Expand All @@ -641,7 +648,7 @@ function getModulesOptions(rawOptions, exportType, loaderContext) {
exportLocalsConventionType !== "dashesOnly"
) {
throw new Error(
'The "modules.namedExport" option requires the "modules.exportLocalsConvention" option to be "camelCaseOnly" or "dashesOnly"',
'The "modules.namedExport" option requires the "modules.exportLocalsConvention" option to be "asIs", "camelCaseOnly" or "dashesOnly"',
);
}
}
Expand All @@ -654,8 +661,11 @@ function normalizeOptions(rawOptions, loaderContext) {
typeof rawOptions.exportType === "undefined"
? "array"
: rawOptions.exportType;
const esModule =
typeof rawOptions.esModule === "undefined" ? true : rawOptions.esModule;
const modulesOptions = getModulesOptions(
rawOptions,
esModule,
exportType,
loaderContext,
);
Expand All @@ -672,8 +682,7 @@ function normalizeOptions(rawOptions, loaderContext) {
typeof rawOptions.importLoaders === "string"
? parseInt(rawOptions.importLoaders, 10)
: rawOptions.importLoaders,
esModule:
typeof rawOptions.esModule === "undefined" ? true : rawOptions.esModule,
esModule,
exportType,
};
}
Expand Down
11 changes: 4 additions & 7 deletions test/__snapshots__/esModule-option.test.js.snap
Expand Up @@ -133,7 +133,6 @@ ___CSS_LOADER_EXPORT___.push([module.id, \`@charset "UTF-8";
}
\`, ""]);
// Exports
___CSS_LOADER_EXPORT___.locals = {};
export default ___CSS_LOADER_EXPORT___;
"
`;
Expand Down Expand Up @@ -189,9 +188,8 @@ ___CSS_LOADER_EXPORT___.push([module.id, \`@charset "UTF-8";
}
\`, ""]);
// Exports
___CSS_LOADER_EXPORT___.locals = {
"class": \`OZJqogC5EaF_wROug7zE\`
};
var _1 = \`OZJqogC5EaF_wROug7zE\`;
export { _1 as "class" };
export default ___CSS_LOADER_EXPORT___;
"
`;
Expand Down Expand Up @@ -247,9 +245,8 @@ ___CSS_LOADER_EXPORT___.push([module.id, \`@charset "UTF-8";
}
\`, ""]);
// Exports
___CSS_LOADER_EXPORT___.locals = {
"class": \`OZJqogC5EaF_wROug7zE\`
};
var _1 = \`OZJqogC5EaF_wROug7zE\`;
export { _1 as "class" };
export default ___CSS_LOADER_EXPORT___;
"
`;
Expand Down

0 comments on commit 61acac2

Please sign in to comment.