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

support named exports #66

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
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const {
filenameToPascalCase,
filenameToTypingsFilename,
getCssModuleKeys,
getNamedExports,
generateGenericExportInterface,
} = require("./utils");
const persist = require("./persist");
Expand Down Expand Up @@ -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));

Expand Down
14 changes: 14 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -80,6 +93,7 @@ export = ${moduleName};`;

module.exports = {
getCssModuleKeys,
getNamedExports,
filenameToPascalCase,
filenameToTypingsFilename,
generateGenericExportInterface,
Expand Down