Skip to content

Instantly share code, notes, and snippets.

@andriyor
Created November 21, 2022 14:26
Show Gist options
  • Save andriyor/6277f620ed79aa34d6c166234e74215d to your computer and use it in GitHub Desktop.
Save andriyor/6277f620ed79aa34d6c166234e74215d to your computer and use it in GitHub Desktop.
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