Skip to content

Instantly share code, notes, and snippets.

@Paradoxia
Created November 10, 2018 08:30
Show Gist options
  • Save Paradoxia/0c75ddd826469f0f615660d1e20f37d4 to your computer and use it in GitHub Desktop.
Save Paradoxia/0c75ddd826469f0f615660d1e20f37d4 to your computer and use it in GitHub Desktop.
const {resolve} = require('path');
var hjson = require('hjson'); // Allow us to use comments in json files
var fs = require('fs');
const TSModuleAlias = require("@momothepug/tsmodule-alias");
function resolveTsconfigPathsToAlias({tsconfigPath = './tsconfig.json', rootConfigBasePath: rootConfigBasePath = './'} = {}) {
const rawJson = fs.readFileSync(tsconfigPath, 'UTF-8');
const commentsStrippedJson = hjson.parse(rawJson);
const {paths, outDir} = commentsStrippedJson.compilerOptions;
const aliases = {};
Object.keys(paths).forEach((item) => {
const key = item.replace('/*', '');
// @ts-ignore
aliases[key] = resolve(rootConfigBasePath, outDir + "/" + paths[item][0].replace('/*', '')) ;
});
return aliases
}
const aliases = resolveTsconfigPathsToAlias();
TSModuleAlias.use(aliases);
require("./aliasResolver.js"); // Call this to set the aliases base on "outDir" setting
import express, {Application, Request, Response} from "express";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment