Skip to content

Instantly share code, notes, and snippets.

@deejayy
Last active September 21, 2022 20:26
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save deejayy/aa5f0cde76dc29f6b4127e60e74be2f2 to your computer and use it in GitHub Desktop.
Save deejayy/aa5f0cde76dc29f6b4127e60e74be2f2 to your computer and use it in GitHub Desktop.
Solves the problem with typescript->javascript compilation and module path aliases (compilerOptions.paths).
import customModuleLoader = require('module');
export class CustomModuleLoader {
public cOptions: any = require('../tsconfig.json').compilerOptions;
public replacePaths: any = {};
constructor() {
Object.keys(this.cOptions.paths).forEach(alias => {
this.replacePaths[alias.replace(/\*.?/, '(.*)')] = this.cOptions.paths[alias][0].replace(/\*.?/, '$1');
});
(<any>customModuleLoader)._originalResolveFilename = (<any>customModuleLoader)._resolveFilename;
(<any>customModuleLoader)._resolveFilename = (request: string, parent: customModuleLoader, isMain: boolean) => {
Object.keys(this.replacePaths).forEach(matchString => {
let regex = new RegExp(matchString);
if (request.match(regex)) {
request = [process.cwd(), this.cOptions.outDir, request.replace(regex, this.replacePaths[matchString])].join('/');
}
})
return (<any>customModuleLoader)._originalResolveFilename(request, parent, isMain);
}
}
};
@deejayy
Copy link
Author

deejayy commented Sep 13, 2018

What's this?

This is little snippet you can use to overwrite the _resolveFilename method from "module" module, which is responsible for loading modules via "require" in node.js. Comes handy with tsconfig's module path aliases (like @whatever directory). Read more here:
How to use module path aliases in TypeScript.

Why?

I've run into a special use case when I tried to work within a typescript project which compiles to js and runs by node. In this project, I wanted to use module path alias (compilerOptions.paths in tsconfig.json). Since the typescript compiler does not resolve this when creates the javascript files, node fails because plain javascript could not find the alias paths.

But there are plenty of solutions for this

I tried many node modules which stated to solve this problem but none worked, so I decided to write this bare minimum, which works, so this is it.

How to use?

Simple, your app surely has an entry point, like "index.ts" or something.
The first 2 lines should be:

import { CustomModuleLoader } from './CustomModuleLoader';
let moduleLoader = new CustomModuleLoader();

Source not looks so good

Yeah, you can say it can be more elegant, cleaner, higher quality, whatever. I encourage you to contribute if you feel something is not done well.
Also, it can be a node module as well, which take place in the node repository, etc. Do it, fork it, extend it, I won't feel bad if you steal the show ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment