mirror of
https://github.com/Effect-TS/effect.git
synced 2026-12-03 03:25:40 +08:00
intro moduleMaps
This commit is contained in:
@@ -37,7 +37,7 @@ export default function tracer(
|
||||
flow?: boolean
|
||||
identity?: boolean
|
||||
dataFirst?: boolean
|
||||
replaceMap?: Record<string, string>
|
||||
moduleMap?: Record<string, string>
|
||||
}
|
||||
) {
|
||||
const tracingOn = !(_opts?.tracing === false)
|
||||
@@ -47,6 +47,9 @@ export default function tracer(
|
||||
const dataFirstOn = !(_opts?.dataFirst === false)
|
||||
const checker = _program.getTypeChecker()
|
||||
|
||||
const moduleMap = _opts?.moduleMap || {}
|
||||
const moduleMapKeys = Object.keys(moduleMap).map((k) => [k, new RegExp(k)] as const)
|
||||
|
||||
return {
|
||||
before(ctx: ts.TransformationContext) {
|
||||
const factory = ctx.factory
|
||||
@@ -457,6 +460,20 @@ export default function tracer(
|
||||
|
||||
const { fileName } = sourceFile
|
||||
|
||||
let finalName = path.relative(process.cwd(), fileName)
|
||||
|
||||
for (const k of moduleMapKeys) {
|
||||
const matches = finalName.match(k[1])
|
||||
if (matches) {
|
||||
let patchedName = moduleMap[k[0]]
|
||||
for (let j = 1; j < matches.length; j += 1) {
|
||||
patchedName = patchedName.replace("$" + j, matches[j])
|
||||
}
|
||||
finalName = patchedName
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
const fileNode = factory.createVariableStatement(
|
||||
undefined,
|
||||
factory.createVariableDeclarationList(
|
||||
@@ -465,7 +482,7 @@ export default function tracer(
|
||||
fileVar,
|
||||
undefined,
|
||||
undefined,
|
||||
factory.createStringLiteral(path.relative(process.cwd(), fileName))
|
||||
factory.createStringLiteral(finalName)
|
||||
)
|
||||
],
|
||||
ts.NodeFlags.Const
|
||||
|
||||
Reference in New Issue
Block a user