Skip to content

Commit

Permalink
chore(core): use ts-node instead of swc-node for resolving nx plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Feb 25, 2022
1 parent 5b08f31 commit fbecd34
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
8 changes: 1 addition & 7 deletions packages/tao/package.json
Expand Up @@ -30,7 +30,7 @@
},
"homepage": "https://nx.dev",
"dependencies": {
"@swc-node/register": "^1.4.2",
"ts-node": "9.1.1",
"tsconfig-paths": "^3.9.0",
"chalk": "4.1.0",
"enquirer": "~2.3.6",
Expand All @@ -44,11 +44,5 @@
"tmp": "~0.2.1",
"tslib": "^2.3.0",
"yargs-parser": "20.0.0"
},
"optionalDependencies": {
"@swc/core-linux-x64-musl": "^1.2.136",
"@swc/core-linux-x64-gnu": "^1.2.136",
"@swc/core-linux-arm64-gnu": "^1.2.136",
"@swc/core-linux-arm64-musl": "^1.2.136"
}
}
29 changes: 19 additions & 10 deletions packages/tao/src/shared/nx-plugin.ts
@@ -1,8 +1,7 @@
import { sync } from 'fast-glob';
import { existsSync } from 'fs';
import * as path from 'path';
import { register } from '@swc-node/register/register';
import { readDefaultTsConfig } from '@swc-node/register/read-default-tsconfig';
import { register } from 'ts-node';

import { appRootPath } from '../utils/app-root';
import { readJsonFile } from '../utils/fileutils';
Expand Down Expand Up @@ -156,10 +155,7 @@ export function resolveLocalNxPlugin(
let tsNodeAndPathsRegistered = false;
function registerTSTranspiler() {
try {
const tsConfigOptions = readDefaultTsConfig(
path.join(appRootPath, 'tsconfig.base.json')
);
register(tsConfigOptions);
register({ typeCheck: false });

const tsconfigPaths: typeof import('tsconfig-paths') = require('tsconfig-paths');

Expand All @@ -169,7 +165,7 @@ function registerTSTranspiler() {
*/
return tsconfigPaths.register({
baseUrl: appRootPath,
paths: tsConfigOptions.paths,
paths: readTsConfigPaths(),
});
} catch (err) {}
}
Expand All @@ -196,9 +192,7 @@ function findNxProjectForImportPath(
workspace: WorkspaceJsonConfiguration,
root = appRootPath
): string | null {
const tsConfigPaths: Record<string, string[]> = readJsonFile(
path.join(root, 'tsconfig.base.json')
)?.compilerOptions?.paths;
const tsConfigPaths: Record<string, string[]> = readTsConfigPaths(root);
const possiblePaths = tsConfigPaths[importPath]?.map((p) =>
path.resolve(root, p)
);
Expand Down Expand Up @@ -227,3 +221,18 @@ function findNxProjectForImportPath(
);
}
}

let tsconfigPaths: Record<string, string[]>;
function readTsConfigPaths(root: string = appRootPath) {
if (!tsconfigPaths) {
const tsconfigPath: string | null = ['tsconfig.base.json', 'tsconfig.json']
.map((x) => path.join(root, x))
.filter((x) => existsSync(x))[0];
if (!tsconfigPath) {
throw new Error('unable to find tsconfig.base.json or tsconfig.json');
}
const { compilerOptions } = readJsonFile(tsconfigPath);
tsconfigPaths = compilerOptions?.paths;
}
return tsconfigPaths;
}

0 comments on commit fbecd34

Please sign in to comment.