From 762fb1654c15305525faedf682eb2edaa4a10d88 Mon Sep 17 00:00:00 2001 From: April Arcus Date: Mon, 4 Oct 2021 09:12:14 -0700 Subject: [PATCH] support named exports --- src/index.js | 6 +++++- src/utils.js | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 04b6b99..222a92f 100644 --- a/src/index.js +++ b/src/index.js @@ -3,6 +3,7 @@ const { filenameToPascalCase, filenameToTypingsFilename, getCssModuleKeys, + getNamedExports, generateGenericExportInterface, } = require("./utils"); const persist = require("./persist"); @@ -63,9 +64,12 @@ module.exports = function (content, ...args) { // let's only check `exports.locals` for keys to avoid getting keys from the sourcemap when it's enabled // if we cannot find locals, then the module only contains global styles + const isModule = /(?:^|\n)export\s+(?:var|let|const)\s+\S+\s*=/g.test(content) const indexOfLocals = content.indexOf(".locals"); const cssModuleKeys = - indexOfLocals === -1 + isModule + ? getNamedExports(content) + : indexOfLocals === -1 ? [] : getCssModuleKeys(content.substring(indexOfLocals)); diff --git a/src/utils.js b/src/utils.js index 8cc032c..332cfe4 100644 --- a/src/utils.js +++ b/src/utils.js @@ -19,6 +19,19 @@ const getCssModuleKeys = (content) => { return cssModuleKeys; }; +const getNamedExports = (content) => { + const exportRegex = /(?:^|\n)export\s+(?:var|let|const)\s+(\S+?)\s*=/g; + let match; + const namedExports = []; + + while ((match = exportRegex.exec(content))) { + if (namedExports.indexOf(match[1]) < 0) { + namedExports.push(match[1]); + } + } + return namedExports; +}; + /** * @param {string} filename */ @@ -80,6 +93,7 @@ export = ${moduleName};`; module.exports = { getCssModuleKeys, + getNamedExports, filenameToPascalCase, filenameToTypingsFilename, generateGenericExportInterface,