Skip to content

Commit f14a4f4

Browse files
committed
fix: getClassName now works as expected when multiple file use runtime resolution
1 parent 6be1c6a commit f14a4f4

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

src/index.js

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,22 @@ export default ({
1616
}: {
1717
types: BabelTypes
1818
}) => {
19-
let styleModuleImportMap;
20-
let importedHelperIndentifier;
21-
let styleModuleImportMapIdentifier;
19+
const filenameMap = {};
2220

23-
const setupFileForRuntimeResolution = (path) => {
21+
const setupFileForRuntimeResolution = (path, filename) => {
2422
const programPath = path.findParent((parentPath) => {
2523
return parentPath.isProgram();
2624
});
2725

28-
importedHelperIndentifier = programPath.scope.generateUidIdentifier('getClassName');
29-
styleModuleImportMapIdentifier = programPath.scope.generateUidIdentifier('styleModuleImportMap');
26+
filenameMap[filename].importedHelperIndentifier = programPath.scope.generateUidIdentifier('getClassName');
27+
filenameMap[filename].styleModuleImportMapIdentifier = programPath.scope.generateUidIdentifier('styleModuleImportMap');
3028

3129
programPath.unshiftContainer(
3230
'body',
3331
t.importDeclaration(
3432
[
3533
t.importDefaultSpecifier(
36-
importedHelperIndentifier
34+
filenameMap[filename].importedHelperIndentifier
3735
)
3836
],
3937
t.stringLiteral('babel-plugin-react-css-modules/dist/browser/getClassName')
@@ -49,12 +47,14 @@ export default ({
4947
'const',
5048
[
5149
t.variableDeclarator(
52-
styleModuleImportMapIdentifier,
53-
createObjectExpression(t, styleModuleImportMap)
50+
filenameMap[filename].styleModuleImportMapIdentifier,
51+
createObjectExpression(t, filenameMap[filename].styleModuleImportMap)
5452
)
5553
]
5654
)
5755
);
56+
// eslint-disable-next-line
57+
// console.log('setting up', filename, util.inspect(filenameMap,{depth: 5}))
5858
};
5959

6060
return {
@@ -65,6 +65,7 @@ export default ({
6565
return;
6666
}
6767

68+
const filename = stats.file.opts.filename;
6869
const targetFileDirectoryPath = dirname(stats.file.opts.filename);
6970
const targetResourcePath = resolve(targetFileDirectoryPath, path.node.source.value);
7071

@@ -82,11 +83,12 @@ export default ({
8283
throw new Error('Unexpected use case.');
8384
}
8485

85-
styleModuleImportMap[styleImportName] = requireCssModule(targetResourcePath, {
86+
filenameMap[filename].styleModuleImportMap[styleImportName] = requireCssModule(targetResourcePath, {
8687
generateScopedName: stats.opts.generateScopedName
8788
});
8889
},
89-
JSXElement (path: Object): void {
90+
JSXElement (path: Object, stats: Object): void {
91+
const filename = stats.file.opts.filename;
9092
const styleNameAttribute = path.node.openingElement.attributes
9193
.find((attribute) => {
9294
return typeof attribute.name !== 'undefined' && attribute.name.name === 'styleName';
@@ -97,21 +99,33 @@ export default ({
9799
}
98100

99101
if (t.isStringLiteral(styleNameAttribute.value)) {
100-
resolveStringLiteral(path, styleModuleImportMap, styleNameAttribute);
102+
resolveStringLiteral(
103+
path,
104+
filenameMap[filename].styleModuleImportMap,
105+
styleNameAttribute
106+
);
101107

102108
return;
103109
}
104110

105111
if (t.isJSXExpressionContainer(styleNameAttribute.value)) {
106-
if (!importedHelperIndentifier) {
107-
setupFileForRuntimeResolution(path);
112+
if (!filenameMap[filename].importedHelperIndentifier) {
113+
setupFileForRuntimeResolution(path, filename);
108114
}
109-
110-
replaceJsxExpressionContainer(t, styleNameAttribute, importedHelperIndentifier, styleModuleImportMapIdentifier);
115+
replaceJsxExpressionContainer(
116+
t,
117+
styleNameAttribute,
118+
filenameMap[filename].importedHelperIndentifier,
119+
filenameMap[filename].styleModuleImportMapIdentifier
120+
);
111121
}
112122
},
113-
Program () {
114-
styleModuleImportMap = {};
123+
Program (path: Object, stats: Object): void {
124+
const filename = stats.file.opts.filename;
125+
126+
filenameMap[filename] = {
127+
styleModuleImportMap: {}
128+
};
115129
}
116130
}
117131
};

0 commit comments

Comments
 (0)