@dr.pogodin/babel-plugin-react-css-modules
Advanced tools
Comparing version 6.13.0 to 6.13.1
@@ -176,3 +176,7 @@ "use strict"; | ||
}, | ||
// All these are supposed to be supported by this visitor: | ||
// import styles from './style.css'; | ||
// import * as styles from './style.css'; | ||
// import { className } from './style.css'; | ||
// import Style, { className } from './style.css'; | ||
ImportDeclaration(path, stats) { | ||
@@ -183,20 +187,67 @@ const importedPath = path.node.source.value; | ||
let styleImportName; | ||
if (path.node.specifiers.length === 0) { | ||
// use imported file path as import name | ||
styleImportName = importedPath; | ||
} else if (path.node.specifiers.length === 1) { | ||
styleImportName = path.node.specifiers[0].local.name; | ||
} else { | ||
// eslint-disable-next-line no-console | ||
console.warn('Please report your use case. https://github.com/birdofpreyru/babel-plugin-react-css-modules/issues/new?title=Unexpected+use+case.'); | ||
throw new Error('Unexpected use case.'); | ||
const { | ||
specifiers | ||
} = path.node; | ||
const guardStyleImportNameIsNotSet = () => { | ||
if (styleImportName) { | ||
// If this throws, it means we are missing something in our logic | ||
// below, and although it might look functional, it does not produce | ||
// determenistic style import selection. | ||
console.warn('Please report your use case. https://github.com/birdofpreyru/babel-plugin-react-css-modules/issues/new?title=Unexpected+use+case.'); | ||
throw Error('Style import name is already selected'); | ||
} | ||
}; | ||
for (let i = 0; i < specifiers.length; ++i) { | ||
const specifier = specifiers[i]; | ||
switch (specifier.type) { | ||
// import Style from './style.css'; | ||
case 'ImportDefaultSpecifier': | ||
guardStyleImportNameIsNotSet(); | ||
styleImportName = specifier.local.name; | ||
break; | ||
// import * as Style from './style.css'; | ||
case 'ImportNamespaceSpecifier': | ||
guardStyleImportNameIsNotSet(); | ||
styleImportName = specifier.local.name; | ||
break; | ||
// These are individual class names in the named import: | ||
// import { className } from './style.css'; | ||
// we just ignore them, falling back to either the default | ||
// import, or the imported path. | ||
case 'ImportSpecifier': | ||
break; | ||
default: | ||
// eslint-disable-next-line no-console | ||
console.warn('Please report your use case. https://github.com/birdofpreyru/babel-plugin-react-css-modules/issues/new?title=Unexpected+use+case.'); | ||
throw new Error('Unexpected use case.'); | ||
} | ||
} | ||
// Fallback for anonymous style import: | ||
// import './style.css'; | ||
if (styleImportName === undefined) styleImportName = importedPath; | ||
const styleMap = loadStyleMap(styleImportName, importedPath, targetResourcePath, path, stats); | ||
if (stats.opts.replaceImport) { | ||
const { | ||
specifiers | ||
} = path.node; | ||
if (specifiers.length) { | ||
if (specifiers.length > 1 || specifiers[0].type !== 'ImportDefaultSpecifier') throw Error('Unsupported kind of import'); | ||
path.replaceWith(types.variableDeclaration('const', [types.variableDeclarator(types.identifier(specifiers[0].local.name), (0, _createObjectExpression.default)(types, styleMap))])); | ||
const variables = []; | ||
for (let i = 0; i < specifiers.length; ++i) { | ||
const specifier = specifiers[i]; | ||
switch (specifier.type) { | ||
case 'ImportDefaultSpecifier': | ||
case 'ImportNamespaceSpecifier': | ||
variables.push(types.variableDeclarator(types.identifier(specifier.local.name), (0, _createObjectExpression.default)(types, styleMap))); | ||
break; | ||
case 'ImportSpecifier': | ||
{ | ||
const value = styleMap[specifier.imported.name]; | ||
variables.push(types.variableDeclarator(types.identifier(specifier.local.name), value === undefined ? undefined : types.stringLiteral(value))); | ||
break; | ||
} | ||
default: | ||
throw Error('Unsupported kind of import'); | ||
} | ||
} | ||
if (variables.length) { | ||
path.replaceWith(types.variableDeclaration('const', variables)); | ||
} else path.remove(); | ||
@@ -203,0 +254,0 @@ } else if (stats.opts.removeImport) { |
{ | ||
"version": "6.13.0", | ||
"version": "6.13.1", | ||
"dependencies": { | ||
@@ -4,0 +4,0 @@ "@babel/core": "^7.24.4", |
@@ -463,18 +463,29 @@ # Babel Plugin: React CSS Modules | ||
```js | ||
// Anonymous imports, like the following, are removed from the code. | ||
import '/path/to/styles.css'; | ||
// Anonymous imports are removed from the code: | ||
import 'path/to/style.css'; | ||
// Default imports are replaced with mappings between original and generated | ||
// class names. | ||
// Default and named imports are replaced in the following manner: | ||
// Original import: | ||
import styles from '/path/to/styles.css'; | ||
// Before: | ||
import styles, { | ||
className, | ||
otherClassName as alias, | ||
} from 'path/to/style.css'; | ||
// Replaced by: | ||
// After: | ||
const styles = { | ||
originalClassName: 'generatedClassName', | ||
// etc. | ||
} | ||
className: 'generatedClassName', | ||
otherClassName: 'otherGeneratedClassName', | ||
}, | ||
className = 'generatedClassName', | ||
alias = 'otherGeneratedClassName'; | ||
// Other kinds of imports are not supported yet. | ||
// Also this kind of import: | ||
import * as style from 'path/to/style.css'; | ||
// is replaced by: | ||
const style = { | ||
className: 'generatedClassName', | ||
otherClassName: 'otherGeneratedClassName', | ||
}; | ||
``` | ||
@@ -538,3 +549,3 @@ | ||
| ------------------------- | ------------------------- | | ||
| `7.0.0` (latest) | `6.13.0` (latest) | | ||
| `7.0.0` (latest) | `6.13.0` – `6.13.1` (latest) | | ||
| `6.7.1` – `6.11.0` | `6.7.0` – `6.12.0` | | ||
@@ -541,0 +552,0 @@ | `6.5.0` – `6.7.0` | `6.5.1` – `6.6.1` | |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
188124
1225
578