Created
March 22, 2020 16:46
-
-
Save tieppt/9f571dc5de82145a7ec06f82ceb2097e to your computer and use it in GitHub Desktop.
tsconfig paths for nodejs app with custom paths
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
const tsConfig = require('./tsconfig.json'); | |
const tsConfigPaths = require('tsconfig-paths'); | |
const path = require('path'); | |
const outDir = path.normalize(tsConfig.compilerOptions.outDir); | |
const rootDir = path.normalize(tsConfig.compilerOptions.rootDir); | |
const baseUrl = path.join(__dirname, tsConfig.compilerOptions.baseUrl); | |
let paths = tsConfig.compilerOptions.paths; | |
paths = Object.entries(paths).map(([key, values]) => { | |
const result = values.map(item => { | |
let pathNrml = path.normalize(item); | |
if (pathNrml.startsWith(rootDir)) { | |
pathNrml = pathNrml.replace(rootDir, outDir); | |
} | |
return pathNrml; | |
}); | |
return [key, result]; | |
}).reduce((acc, curr) => { | |
acc[curr[0]] = curr[1]; | |
return acc; | |
}, {}); | |
const cleanup = tsConfigPaths.register({ | |
baseUrl, | |
paths | |
}); | |
// When path registration is no longer needed | |
// cleanup(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment