File size: 1,790 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
import nodePath from 'path'
import type { API, FileInfo, Options } from 'jscodeshift'
import { createParserFromPath } from '../parser'
export const globalCssContext = {
cssImports: new Set<string>(),
reactSvgImports: new Set<string>(),
}
const globalStylesRegex = /(?<!\.module)\.(css|scss|sass)$/i
export default function transformer(
file: FileInfo,
_api: API,
options: Options
) {
const j = createParserFromPath(file.path)
const root = j(file.source)
let hasModifications = false
root
.find(j.ImportDeclaration)
.filter((path) => {
const {
node: {
source: { value },
},
} = path
if (typeof value === 'string') {
if (globalStylesRegex.test(value)) {
let resolvedPath = value
if (value.startsWith('.')) {
resolvedPath = nodePath.resolve(nodePath.dirname(file.path), value)
}
globalCssContext.cssImports.add(resolvedPath)
const { start, end } = path.node as any
if (!path.parentPath.node.comments) {
path.parentPath.node.comments = []
}
path.parentPath.node.comments = [
j.commentLine(' ' + file.source.substring(start, end)),
]
hasModifications = true
return true
} else if (value.endsWith('.svg')) {
const isComponentImport = path.node.specifiers.some((specifier) => {
return (specifier as any).imported?.name === 'ReactComponent'
})
if (isComponentImport) {
globalCssContext.reactSvgImports.add(file.path)
}
}
}
return false
})
.remove()
return hasModifications && globalCssContext.reactSvgImports.size === 0
? root.toSource(options)
: null
}
|