{"version":3,"sources":["utils/resolve-imports.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,YAAY,CAAC;AAQ5B,wBAAgB,eAAe,CAAC,SAAS,EAAE,EAAE,CAAC,OAAO,EAAE,eAAe,EAAE,EAAE,CAAC,eAAe,EAAE,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,EAAE,CAAC,UAAU,CAAC,EAAE,eAAe,EAAE,MAAM,GAAG,GAAG,CAAC,MAAM,EAAE,EAAE,CAAC,UAAU,CAAC,CAsBlL","file":"resolve-imports.d.ts","sourcesContent":["import ts from \"typescript\";\nimport { statSync } from 'fs';\nimport { resolve, dirname, relative, sep as PathSep } from 'path';\nimport { errorFile } from \"./error\";\nconst isWindows = PathSep === '\\\\';\n\n/**\n * Resolve imports to be usable by js\n */\nexport function _resolveImports(\n\ttsPrinter: ts.Printer,\n\tcompilerOptions: ts.CompilerOptions,\n\tfiles: Map<string, ts.SourceFile>,\n\ttargetExtension: string\n): Map<string, ts.SourceFile> {\n\t// Base dir\n\tconst baseDir = resolve(typeof compilerOptions.baseUrl === 'string' ? compilerOptions.baseUrl : '.');\n\t// Load mapped paths from compiler options\n\tconst mappedPaths: Map<string, string> = new Map();\n\tconst paths = compilerOptions.paths ?? {};\n\tfor (let k in paths) {\n\t\tvar v = paths[k];\n\t\tif (v.length != 1)\n\t\t\tthrow new Error(`Typescript options>> Expected one entry for each path, found ${v.length} at ${k}`);\n\t\t// remove trailing slash\n\t\tk = k.replace(/\\/\\*?$/, '');\n\t\tmappedPaths.set(k, resolve(baseDir, v[0].replace(/\\/\\*?$/, '')));\n\t}\n\t// Resolve imports\n\tfiles.forEach((srcFile, filePath) => {\n\t\tsrcFile = ts.transform(srcFile, [function (ctx: ts.TransformationContext) {\n\t\t\treturn _importTransform(tsPrinter, ctx, srcFile, filePath, targetExtension, mappedPaths)\n\t\t}]).transformed[0] as ts.SourceFile;\n\t\tfiles.set(filePath, srcFile);\n\t});\n\treturn files;\n}\n\nfunction _importTransform(\n\ttsPrinter: ts.Printer,\n\tctx: ts.TransformationContext,\n\tsrcFile: ts.SourceFile,\n\tfilePath: string,\n\ttargetExtension: string,\n\tmappedPaths: Map<string, string>\n) {\n\tconst f = ctx.factory;\n\tconst _dirname = dirname(filePath);\n\tconst MAPPED_IMPORT_MATCHER = /^(@[^\\/\\\\'\"`]+)(.*)/;\n\treturn _visitor;\n\tfunction _visitor(node: ts.Node): ts.Node {\n\t\tif (ts.isImportDeclaration(node) && !node.importClause?.isTypeOnly) {\n\t\t\t//* Import declaration\n\t\t\treturn f.updateImportDeclaration(\n\t\t\t\tnode, node.decorators, node.modifiers, node.importClause,\n\t\t\t\tf.createStringLiteral(_resolvePath(node.moduleSpecifier)), undefined\n\t\t\t);\n\t\t} else if (ts.isExportDeclaration(node) && node.moduleSpecifier) {\n\t\t\t//* Export declaration\n\t\t\treturn f.updateExportDeclaration(\n\t\t\t\tnode, node.decorators, node.modifiers, node.isTypeOnly, node.exportClause,\n\t\t\t\tf.createStringLiteral(_resolvePath(node.moduleSpecifier)), undefined\n\t\t\t);\n\t\t} else if (ts.isCallExpression(node) && node.expression.kind === ts.SyntaxKind.ImportKeyword) {\n\t\t\t//* Dynamic import\n\t\t\tif (node.arguments.length !== 1)\n\t\t\t\tthrow new Error(`Dynamic import must have one specifier as an argument at ${errorFile(srcFile, node)}`);\n\t\t\tvar expr: ts.Expression = node.arguments[0];\n\t\t\tif (ts.isStringLiteral(expr)) {\n\t\t\t\texpr = f.createStringLiteral(_resolvePath(node.arguments[0]));\n\t\t\t} else {\n\t\t\t\texpr = ts.visitEachChild<ts.Expression>(\n\t\t\t\t\texpr,\n\t\t\t\t\tfunction (n: ts.Node) {\n\t\t\t\t\t\tif (ts.isStringLiteral(n))\n\t\t\t\t\t\t\tn = f.createStringLiteral(_resolvePath(n));\n\t\t\t\t\t\treturn n;\n\t\t\t\t\t},\n\t\t\t\t\tctx\n\t\t\t\t);\n\t\t\t}\n\t\t\treturn f.updateCallExpression(\n\t\t\t\tnode, node.expression, node.typeArguments, [expr]\n\t\t\t);\n\t\t}\n\t\treturn ts.visitEachChild(node, _visitor, ctx);\n\t}\n\t/** Resolve path */\n\tfunction _resolvePath(node: ts.Expression) {\n\t\t// Remove quotes, parsing using JSON.parse fails on simple quoted strings\n\t\t//TODO find better solution to parse string\n\t\tvar path = tsPrinter.printNode(ts.EmitHint.Unspecified, node, srcFile);\n\t\tpath = path.slice(1, -1);\n\t\t//* Check if this @ is mappable\n\t\tlet mappedImport = path.match(MAPPED_IMPORT_MATCHER);\n\t\tlet mappedKey: string | undefined;\n\t\tif (mappedImport != null && (mappedKey = mappedPaths.get(mappedImport[1])))\n\t\t\tpath = `${mappedKey}${mappedImport[2]}`;\n\t\telse if (path.charAt(0) === '.' && !path.endsWith(targetExtension))\n\t\t\tpath = resolve(_dirname, path);\n\t\telse return path;\n\t\t//* Resolve path extension\n\t\t// check file exists\n\t\tpath = _resolveFilePath(path);\n\t\t// create relative path to current file\n\t\tpath = relative(_dirname, path);\n\t\t// Replace windows anti-slashes\n\t\tif (isWindows) path = path.replace(/\\\\/g, '/');\n\t\t// Add prefix \"./\"\n\t\tif (path.charAt(0) === '/') path = '.' + path;\n\t\telse if (path.charAt(0) !== '.') path = './' + path;\n\t\treturn path;\n\t}\n\t// Resolve file path\n\tfunction _resolveFilePath(path: string) {\n\t\t// Check if directory\n\t\ttry {\n\t\t\t// If isn't directory, we will not change it's extension\n\t\t\tif (statSync(path).isDirectory())\n\t\t\t\tpath = resolve(path, 'index' + targetExtension);\n\t\t} catch (err) {\n\t\t\ttry {\n\t\t\t\tif (statSync(path + '.ts').isFile()) path += targetExtension;\n\t\t\t} catch (e) {\n\t\t\t\t// try {\n\t\t\t\t// \tif (\n\t\t\t\t// \t\t-!path.endsWith('.js') &&\n\t\t\t\t// \t\tstatSync(path + '.js').isFile()\n\t\t\t\t// \t)\n\t\t\t\t// \t\tpath += '.js';\n\t\t\t\t// } catch (e) {\n\t\t\t\tconsole.error(err);\n\t\t\t\t// }\n\t\t\t}\n\t\t}\n\t\treturn path;\n\t}\n}\n\n\n"]}