Skip to content

Commit

Permalink
fix() use tsconfig-paths instead of path-to-regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Nov 1, 2019
1 parent 1b02cde commit dc4e147
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 23 deletions.
24 changes: 8 additions & 16 deletions lib/compiler/hooks/tsconfig-paths.hook.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
import { dirname, join, relative } from 'path';
import * as pathToRegexp from 'path-to-regexp';
import { dirname, relative } from 'path';
import tsPaths = require('tsconfig-paths');
import * as ts from 'typescript';

export function tsconfigPathsBeforeHookFactory(
compilerOptions: ts.CompilerOptions,
) {
const { paths = {}, baseUrl } = compilerOptions;
const pathsToInspect = Object.keys(paths).map(path => ({
matcher: pathToRegexp(path),
originalPath: path,
}));
const matcher = tsPaths.createMatchPath(baseUrl!, paths, ['main']);

return (ctx: ts.TransformationContext): ts.Transformer<any> => {
const head = (arr: [string]): string => arr && arr[0];
return (sf: ts.SourceFile) => {
const imports: ts.StringLiteral[] = (sf as any).imports || [];
imports.forEach(item => {
const result = pathsToInspect.find(({ matcher }) =>
matcher.test(item.text),
);
const result = matcher(item.text, undefined, undefined, ['.ts', '.js']);
if (!result) {
return;
}
const resolvedPath =
relative(
dirname(sf.fileName),
join(baseUrl!, head(paths[result.originalPath] as [string])),
) || './';
item.text = item.text.replace(result.matcher, resolvedPath);
let resolvedPath = relative(dirname(sf.fileName), result) || './';
resolvedPath =
resolvedPath[0] === '.' ? resolvedPath : './' + resolvedPath;
item.text = resolvedPath;
});
return sf;
};
Expand Down
5 changes: 0 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nestjs/cli",
"version": "6.11.2",
"version": "6.11.3",
"description": "Nest - modern, fast, powerful node.js web framework (@cli)",
"publishConfig": {
"access": "public"
Expand Down Expand Up @@ -50,9 +50,9 @@
"node-emoji": "1.10.0",
"ora": "4.0.2",
"os-name": "3.1.0",
"path-to-regexp": "3.1.0",
"rimraf": "3.0.0",
"tree-kill": "1.2.1",
"tsconfig-paths": "3.9.0",
"tsconfig-paths-webpack-plugin": "3.2.0",
"webpack": "4.41.2",
"webpack-node-externals": "1.7.2"
Expand Down

0 comments on commit dc4e147

Please sign in to comment.