Skip to content

Instantly share code, notes, and snippets.

@tieppt
Created March 22, 2020 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tieppt/9f571dc5de82145a7ec06f82ceb2097e to your computer and use it in GitHub Desktop.
Save tieppt/9f571dc5de82145a7ec06f82ceb2097e to your computer and use it in GitHub Desktop.
tsconfig paths for nodejs app with custom paths
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