From 42ab392433e9585ab1354385c7db175f4cdcb8b4 Mon Sep 17 00:00:00 2001 From: Aleksandr Berezin Date: Wed, 24 Jun 2020 18:10:05 +0300 Subject: [PATCH] Added support default export from babel --- src/index.js | 11 ++++++++--- src/utils.js | 15 ++++++++++++--- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/index.js b/src/index.js index 9f2af79..8af596b 100644 --- a/src/index.js +++ b/src/index.js @@ -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 @@ -81,7 +85,8 @@ module.exports = function(content, ...args) { const cssModuleDefinition = generateGenericExportInterface( cssModuleKeys, filenameToPascalCase(filename), - options.disableLocalsExport + options.disableLocalsExport, + options.useDefaultExport ); applyFormattingAndOptions(cssModuleDefinition, options) diff --git a/src/utils.js b/src/utils.js index c593fb1..261c784 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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}; }`; @@ -69,7 +78,7 @@ ${interfaceProperties} declare const ${moduleName}: ${namespaceName}.${interfaceName}${localsExportType}; -export = ${moduleName};`; +export ${useDefaultExport ? "default" : "="} ${moduleName};`; }; module.exports = {