Skip to content

Commit

Permalink
feat(core): move swc register to tao
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav committed Mar 4, 2022
1 parent 0742dd9 commit 57760a3
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 35 deletions.
4 changes: 1 addition & 3 deletions packages/eslint-plugin-nx/package.json
Expand Up @@ -34,10 +34,8 @@
"dependencies": {
"@nrwl/devkit": "*",
"@nrwl/workspace": "*",
"@swc-node/register": "^1.4.2",
"@typescript-eslint/experimental-utils": "~5.10.0",
"chalk": "4.1.0",
"confusing-browser-globals": "^1.0.9",
"tsconfig-paths": "^3.9.0"
"confusing-browser-globals": "^1.0.9"
}
}
34 changes: 2 additions & 32 deletions packages/eslint-plugin-nx/src/resolve-workspace-rules.ts
@@ -1,47 +1,17 @@
import type { TSESLint } from '@typescript-eslint/experimental-utils';
import { existsSync } from 'fs';
import { join } from 'path';
import { WORKSPACE_PLUGIN_DIR, WORKSPACE_RULE_NAMESPACE } from './constants';
import { readDefaultTsConfig } from '@swc-node/register/read-default-tsconfig';
import { register } from '@swc-node/register/register';
import { registerTsProject } from '@nrwl/tao/src/utils/register';

type ESLintRules = Record<string, TSESLint.RuleModule<string, unknown[]>>;

/**
* Optionally, if ts-node and tsconfig-paths are available in the current workspace, apply the require
* register hooks so that .ts files can be used for writing workspace lint rules.
*
* If ts-node and tsconfig-paths are not available, the user can still provide an index.js file in
* tools/eslint-rules and write their rules in JavaScript and the fundamentals will still work (but
* workspace path mapping will not, for example).
*/
function registerTSWorkspaceLint() {
try {
register(readDefaultTsConfig(join(WORKSPACE_PLUGIN_DIR, 'tsconfig.json')));

const tsconfigPaths = require('tsconfig-paths');

// Load the tsconfig from tools/eslint-rules/tsconfig.json
const tsConfigResult = tsconfigPaths.loadConfig(WORKSPACE_PLUGIN_DIR);

/**
* 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: tsConfigResult.absoluteBaseUrl,
paths: tsConfigResult.paths,
});
} catch (err) {}
}

export const workspaceRules = ((): ESLintRules => {
// If `tools/eslint-rules` folder doesn't exist, there is no point trying to register and load it
if (!existsSync(WORKSPACE_PLUGIN_DIR)) {
return {};
}
// Register `tools/eslint-rules` for TS transpilation
const registrationCleanup = registerTSWorkspaceLint();
const registrationCleanup = registerTsProject(WORKSPACE_PLUGIN_DIR);
try {
/**
* Currently we only support applying the rules from the user's workspace plugin object
Expand Down
2 changes: 2 additions & 0 deletions packages/tao/package.json
Expand Up @@ -31,6 +31,7 @@
"homepage": "https://nx.dev",
"dependencies": {
"@swc/core": "^1.2.146",
"@swc-node/register": "^1.4.2",
"chalk": "4.1.0",
"enquirer": "~2.3.6",
"fast-glob": "3.2.7",
Expand All @@ -42,6 +43,7 @@
"semver": "7.3.4",
"tmp": "~0.2.1",
"tslib": "^2.3.0",
"tsconfig-paths": "^3.9.0",
"yargs-parser": "20.0.0"
}
}
35 changes: 35 additions & 0 deletions packages/tao/src/utils/register.ts
@@ -0,0 +1,35 @@
import { readDefaultTsConfig } from '@swc-node/register/read-default-tsconfig';
import { register } from '@swc-node/register/register';
import { join } from 'path';

/**
* Optionally, if swc-node and tsconfig-paths are available in the current workspace, apply the require
* register hooks so that .ts files can be used for writing custom workspace projects.
*
* If ts-node and tsconfig-paths are not available, the user can still provide an index.js file in
* the root of their project and the fundamentals will still work (but
* workspace path mapping will not, for example).
*/
export const registerTsProject = (
path: string,
configFilename = 'tsconfig.json'
) => {
try {
const tsConfig = readDefaultTsConfig(join(path, configFilename));
register(tsConfig);

/**
* Load the ts config from the source project
*/
const tsconfigPaths = 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,
});
} catch (err) {}
};

0 comments on commit 57760a3

Please sign in to comment.