Skip to content

Commit

Permalink
Fix problem with tslint-loader (issue dividab#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita Stenin committed Nov 3, 2018
1 parent 3c5eafa commit 53541df
Showing 1 changed file with 7 additions and 46 deletions.
53 changes: 7 additions & 46 deletions src/register.ts
Expand Up @@ -2,47 +2,6 @@ import { createMatchPath } from "./match-path-sync";
import { configLoader, ExplicitParams } from "./config-loader";
import { options } from "./options";

function getCoreModules(
builtinModules: string[] | undefined
): { [key: string]: boolean } {
builtinModules = builtinModules || [
"assert",
"buffer",
"child_process",
"cluster",
"crypto",
"dgram",
"dns",
"domain",
"events",
"fs",
"http",
"https",
"net",
"os",
"path",
"punycode",
"querystring",
"readline",
"stream",
"string_decoder",
"tls",
"tty",
"url",
"util",
"v8",
"vm",
"zlib"
];

const coreModules: { [key: string]: boolean } = {};
for (let module of builtinModules) {
coreModules[module] = true;
}

return coreModules;
}

/**
* Installs a custom module load function that can adhere to paths in tsconfig.
*/
Expand All @@ -68,19 +27,21 @@ export function register(explicitParams: ExplicitParams): void {
// tslint:disable-next-line:no-require-imports variable-name
const Module = require("module");
const originalResolveFilename = Module._resolveFilename;
const coreModules = getCoreModules(Module.builtinModules);

// tslint:disable-next-line:no-any
Module._resolveFilename = function(request: string, _parent: any): string {
const isCoreModule = coreModules.hasOwnProperty(request);
if (!isCoreModule) {
try {
// tslint:disable-next-line:no-invalid-this
return originalResolveFilename.apply(this, arguments);
} catch (err) {
const found = matchPath(request);
if (found) {
const modifiedArguments = [found, ...[].slice.call(arguments, 1)]; // Passes all arguments. Even those that is not specified above.
// tslint:disable-next-line:no-invalid-this
return originalResolveFilename.apply(this, modifiedArguments);
}

throw err;
}
// tslint:disable-next-line:no-invalid-this
return originalResolveFilename.apply(this, arguments);
};
}

0 comments on commit 53541df

Please sign in to comment.