-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b0ec0db
commit 7d0afbb
Showing
6 changed files
with
58 additions
and
22 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import {readFileSync} from 'fs'; | ||
import {basename, dirname, join, resolve, relative} from 'path'; | ||
import { | ||
NodeFlags, | ||
isImportDeclaration, | ||
visitEachChild, | ||
visitNode, | ||
isStringLiteral, | ||
} from 'typescript'; | ||
|
||
/** | ||
* Custom SVG transformer to handle .svg imports. | ||
*/ | ||
export default function pathTransformer(context) { | ||
const {factory} = context; | ||
|
||
function visit(node, sourceFile) { | ||
if (isImportDeclaration(node) && isStringLiteral(node.moduleSpecifier)) { | ||
const importPath = node.moduleSpecifier.text; | ||
|
||
if (importPath.startsWith('@/')) { | ||
const relativePath = getRelativeImportPath( | ||
sourceFile.fileName, | ||
importPath | ||
); | ||
|
||
console.log(relativePath); | ||
|
||
return factory.updateImportDeclaration( | ||
node, | ||
node.modifiers, | ||
node.importClause, | ||
factory.createStringLiteral(relativePath), | ||
node.assertClause | ||
); | ||
} | ||
} | ||
return visitEachChild(node, (child) => visit(child, sourceFile), context); | ||
} | ||
|
||
return (sourceFile) => | ||
visitNode(sourceFile, (node) => visit(node, sourceFile)); | ||
} | ||
|
||
function getRelativeImportPath(sourceFilePath, importPath) { | ||
const basePath = resolve(process.cwd()); | ||
const absoluteImportPath = resolve(basePath, importPath.replace('@/', '')); | ||
const relativePath = relative(dirname(sourceFilePath), absoluteImportPath); | ||
return relativePath.startsWith('.') ? relativePath : `./${relativePath}`; | ||
} |
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
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