Created
November 21, 2022 14:26
-
-
Save andriyor/6277f620ed79aa34d6c166234e74215d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Project, Node } from 'ts-morph'; | |
const project = new Project({ | |
tsConfigFilePath: 'tsconfig.json' | |
}); | |
const sourceFiles = project.getSourceFiles(); | |
const toScssImports = (paths: string[]) => { | |
for (const path of paths) { | |
console.log(`@import ${path};`); | |
} | |
}; | |
const cssPaths: string[] = []; | |
for (const sourceFile of sourceFiles) { | |
if (sourceFile.getFilePath().includes('.tsx')) { | |
sourceFile.forEachChild((node) => { | |
if (Node.isImportDeclaration(node)) { | |
const nodeText = node.getText(); | |
if (nodeText.includes('@') && nodeText.includes('css')) { | |
node.forEachChild((importNode) => { | |
if (Node.isStringLiteral(importNode)) { | |
cssPaths.push(importNode.getText()); | |
} | |
}); | |
node.remove(); | |
} | |
} | |
}); | |
} | |
} | |
project.save(); | |
const uniquePaths = Array.from(new Set(cssPaths)); | |
toScssImports(uniquePaths); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment