Skip to content

Commit

Permalink
chore(core): use shared utility to register swc-node
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Mar 4, 2022
1 parent 5449ac0 commit 1536e31
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
43 changes: 21 additions & 22 deletions packages/tao/src/shared/nx-plugin.ts
@@ -1,11 +1,10 @@
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 { appRootPath } from '../utils/app-root';
import { readJsonFile } from '../utils/fileutils';
import { registerTsProject } from '../utils/register';
import { PackageJson } from './package-json';
import { ProjectGraphProcessor } from './project-graph';
import { Workspaces } from './workspace';
Expand Down Expand Up @@ -155,23 +154,10 @@ export function resolveLocalNxPlugin(

let tsNodeAndPathsRegistered = false;
function registerTSTranspiler() {
try {
const tsConfigOptions = readDefaultTsConfig(
path.join(appRootPath, 'tsconfig.base.json')
);
register(tsConfigOptions);

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

/**
* Register the custom workspace path mappings with node so that workspace libraries
* can be imported and used within custom workspace lint rules.
*/
return tsconfigPaths.register({
baseUrl: appRootPath,
paths: tsConfigOptions.paths,
});
} catch (err) {}
if (!tsNodeAndPathsRegistered) {
registerTsProject(appRootPath, 'tsconfig.base.json');
}
tsNodeAndPathsRegistered = true;
}

function lookupLocalPlugin(importPath: string, root = appRootPath) {
Expand All @@ -196,9 +182,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 +211,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;
}
2 changes: 1 addition & 1 deletion packages/tao/src/shared/workspace.ts
Expand Up @@ -483,7 +483,7 @@ export function globForProjectFiles(
];

if (!ignorePluginInference) {
projectGlobPatterns.push(...getGlobPatternsFromPlugins(nxJson))
projectGlobPatterns.push(...getGlobPatternsFromPlugins(nxJson));
}

const combinedProjectGlobPattern = '{' + projectGlobPatterns.join(',') + '}';
Expand Down
17 changes: 11 additions & 6 deletions packages/tao/src/utils/register.ts
Expand Up @@ -15,21 +15,26 @@ export const registerTsProject = (
configFilename = 'tsconfig.json'
) => {
try {
const tsConfig = readDefaultTsConfig(join(path, configFilename));
const tsConfigPath = join(path, configFilename);
process.env['TS_NODE_PROJECT'] = tsConfigPath;

const tsConfig = readDefaultTsConfig(tsConfigPath);
register(tsConfig);

/**
* Load the ts config from the source project
*/
const tsconfigPaths = require('tsconfig-paths');
const tsconfigPaths: typeof import('tsconfig-paths') = require('tsconfig-paths');
const tsConfigResult = tsconfigPaths.loadConfig(path);
/**
* Register the custom workspace path mappings with node so that workspace libraries
* can be imported and used within project
*/
return tsconfigPaths.register({
baseUrl: tsConfigResult.absoluteBaseUrl,
paths: tsConfigResult.paths,
});
if (tsConfigResult.resultType === 'success') {
tsconfigPaths.register({
baseUrl: tsConfigResult.absoluteBaseUrl,
paths: tsConfigResult.paths,
});
}
} catch (err) {}
};

0 comments on commit 1536e31

Please sign in to comment.