Skip to content

Allow multiple anonymous imports if className is only in one of them. #186

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

Closed
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
34 changes: 21 additions & 13 deletions src/getClassName.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,23 @@ export default (styleNameValue: string, styleModuleImportMap: StyleModuleImportM
'\' without importing at least one stylesheet.');
}

if (styleModuleImportMapKeys.length > 1) {
throw new Error('Cannot use anonymous style name \'' + styleName +
'\' with more than one stylesheet import.');
}

const styleModuleMap: StyleModuleMapType = styleModuleImportMap[styleModuleImportMapKeys[0]];

if (!styleModuleMap[styleName]) {
const mappedClassNames = styleModuleImportMapKeys.map((importKey) => {
return styleModuleImportMap[importKey][styleName];
}).filter(Boolean);

if (mappedClassNames.length > 1) {
const importKeysWithMatches = styleModuleImportMapKeys.map((importKey) => {
return styleModuleImportMap[importKey][styleName] && importKey;
}).filter(Boolean);

throw new Error('Cannot resolve styleName "' + styleName + '" ' +
'because it is present in multiple imports:' +
'\n\n\t' + importKeysWithMatches.join('\n\t') +
'\n\nYou can resolve this by using a named import, e.g:' +
'\n\n\timport foo from "' + importKeysWithMatches[0] + '";' +
'\n\t<div styleName="foo.' + styleName + '" />' +
'\n\n');
} else if (mappedClassNames.length === 0) {
if (handleMissingStyleName === 'throw') {
throw new Error('Could not resolve the styleName \'' + styleName + '\'.');
}
Expand All @@ -104,11 +113,10 @@ export default (styleNameValue: string, styleModuleImportMap: StyleModuleImportM
}
}

return styleModuleMap[styleName];
})
.filter((className) => {
// Remove any styles which could not be found (if handleMissingStyleName === 'ignore')
return className;
return mappedClassNames[0];
})

// Remove any styles which could not be found (if handleMissingStyleName === 'ignore')
.filter(Boolean)
.join(' ');
};