Skip to content

Commit 156a641

Browse files
committed
extract helper method for building ObjectExpression mapping original classnames to scoped classnames
1 parent 734bc67 commit 156a641

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/index.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ export default function transformCssModules({ types: t }) {
5151
return new RegExp(`(${extensionsPattern})$`, 'i');
5252
}
5353

54+
function buildClassNameToScopeNameMap(tokens) {
55+
/* eslint-disable new-cap */
56+
return t.ObjectExpression(
57+
Object.keys(tokens).map(token =>
58+
t.ObjectProperty(
59+
t.StringLiteral(token),
60+
t.StringLiteral(tokens[token])
61+
)
62+
)
63+
);
64+
}
65+
5466
return {
5567
visitor: {
5668
Program(path, state) {
@@ -134,15 +146,7 @@ export default function transformCssModules({ types: t }) {
134146
// if parent expression is not a Program, replace expression with tokens
135147
// Otherwise remove require from file, we just want to get generated css for our output
136148
if (!t.isExpressionStatement(path.parent)) {
137-
/* eslint-disable new-cap */
138-
path.replaceWith(t.ObjectExpression(
139-
Object.keys(tokens).map(
140-
token => t.ObjectProperty(
141-
t.StringLiteral(token),
142-
t.StringLiteral(tokens[token])
143-
)
144-
)
145-
));
149+
path.replaceWith(buildClassNameToScopeNameMap(tokens));
146150
} else {
147151
path.remove();
148152
}

0 commit comments

Comments
 (0)