Skip to content

Instantly share code, notes, and snippets.

@zhirzh
Created February 29, 2020 09:26
Show Gist options
  • Save zhirzh/819add4628b607d39c659f3248b9b5f1 to your computer and use it in GitHub Desktop.
Save zhirzh/819add4628b607d39c659f3248b9b5f1 to your computer and use it in GitHub Desktop.
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