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

New feature: added support default export from babel #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@ const schema = {
enum: ["prettier", "none"]
},
disableLocalsExport: {
description:
"Disable the use of locals export. Defaults to `false`",
description: "Disable the use of locals export. Defaults to `false`",
type: "boolean"
},
verifyOnly: {
description:
"Validate generated `*.d.ts` files and fail if an update is needed (useful in CI). Defaults to `false`",
type: "boolean"
},
useDefaultExport: {
description:
"Enable the use of locals default export. Defaults to `false`",
type: "boolean"
}
},
additionalProperties: false
Expand Down Expand Up @@ -81,7 +85,8 @@ module.exports = function(content, ...args) {
const cssModuleDefinition = generateGenericExportInterface(
cssModuleKeys,
filenameToPascalCase(filename),
options.disableLocalsExport
options.disableLocalsExport,
options.useDefaultExport
);

applyFormattingAndOptions(cssModuleDefinition, options)
Expand Down
15 changes: 12 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,22 @@ const filenameToTypingsFilename = filename => {
/**
* @param {string[]} cssModuleKeys
* @param {string} pascalCaseFileName
* @param {string | boolean} disableLocalsExport
* @param {string | boolean} useDefaultExport
*/
const generateGenericExportInterface = (cssModuleKeys, pascalCaseFileName, disableLocalsExport) => {
const generateGenericExportInterface = (
cssModuleKeys,
pascalCaseFileName,
disableLocalsExport,
useDefaultExport
) => {
const interfaceName = `I${pascalCaseFileName}`;
const moduleName = `${pascalCaseFileName}Module`;
const namespaceName = `${pascalCaseFileName}Namespace`;

const localsExportType = disableLocalsExport ? `` : ` & {
const localsExportType = disableLocalsExport
? ``
: ` & {
/** WARNING: Only available when \`css-loader\` is used without \`style-loader\` or \`mini-css-extract-plugin\` */
locals: ${namespaceName}.${interfaceName};
}`;
Expand All @@ -69,7 +78,7 @@ ${interfaceProperties}

declare const ${moduleName}: ${namespaceName}.${interfaceName}${localsExportType};

export = ${moduleName};`;
export ${useDefaultExport ? "default" : "="} ${moduleName};`;
};

module.exports = {
Expand Down