Created
February 29, 2020 09:26
-
-
Save zhirzh/819add4628b607d39c659f3248b9b5f1 to your computer and use it in GitHub Desktop.
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 { join, resolve } = require('path') | |
const tsconfig = require('../tsconfig.json') | |
const { outDir, paths } = tsconfig.compilerOptions | |
const _load = module.constructor._load | |
module.constructor._load = function(request, ...args) { | |
for (let alias in paths) { | |
const aliasRegExp = new RegExp('^' + alias.replace('*', '(.*)') + '$') | |
const matches = request.match(aliasRegExp) | |
if (matches === null) { | |
continue | |
} | |
const relativePath = matches[1] | |
for (const aliasTemplate of paths[alias]) { | |
const path = resolve(join(outDir, aliasTemplate.replace('*', relativePath))) | |
try { | |
return _load(path, ...args) | |
} catch (e) { | |
throw e | |
} | |
} | |
} | |
return _load(request, ...args) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment