@fejs/babel-plugin-auto-css-modules
Version:
@fejs/babel-plugin-auto-css-modules
30 lines (26 loc) • 702 B
text/typescript
import { NodePath, Visitor } from '@babel/traverse';
import { t } from '@fejs/utils';
import { extname } from 'path';
export interface IOpts {
flag?: string;
}
const CSS_EXTNAMES = ['.css', '.less', '.sass', '.scss', '.stylus', '.styl'];
export default function () {
return {
visitor: {
ImportDeclaration(
path: NodePath<t.ImportDeclaration>,
{ opts }: { opts: IOpts },
) {
const {
specifiers,
source,
source: { value },
} = path.node;
if (specifiers.length && CSS_EXTNAMES.includes(extname(value))) {
source.value = `${value}?${opts.flag || 'modules'}`;
}
},
} as Visitor,
};
}