From a54e1fe84f27d25d99b353d48cc42eab5815864d Mon Sep 17 00:00:00 2001 From: Miroslav Jonas Date: Thu, 3 Mar 2022 00:50:15 +0100 Subject: [PATCH] feat(core): move swc register to tao --- docs/generated/api-nx-devkit/index.md | 25 +++++++++++++ packages/devkit/index.ts | 5 +++ packages/eslint-plugin-nx/package.json | 4 +-- .../src/resolve-workspace-rules.ts | 34 ++---------------- packages/tao/package.json | 2 ++ packages/tao/src/utils/register.ts | 35 +++++++++++++++++++ 6 files changed, 70 insertions(+), 35 deletions(-) create mode 100644 packages/tao/src/utils/register.ts diff --git a/docs/generated/api-nx-devkit/index.md b/docs/generated/api-nx-devkit/index.md index 7ed39fe768fbbf..f6e093a03e5b0b 100644 --- a/docs/generated/api-nx-devkit/index.md +++ b/docs/generated/api-nx-devkit/index.md @@ -131,6 +131,7 @@ It only uses language primitives and immutable objects - [readProjectConfiguration](../../nx-devkit/index#readprojectconfiguration) - [readTargetOptions](../../nx-devkit/index#readtargetoptions) - [readWorkspaceConfiguration](../../nx-devkit/index#readworkspaceconfiguration) +- [registerTsProject](../../nx-devkit/index#registertsproject) - [removeDependenciesFromPackageJson](../../nx-devkit/index#removedependenciesfrompackagejson) - [removeProjectConfiguration](../../nx-devkit/index#removeprojectconfiguration) - [runExecutor](../../nx-devkit/index#runexecutor) @@ -1275,6 +1276,30 @@ This does _not_ provide projects configuration, use [readProjectConfiguration](. --- +### registerTsProject + +▸ `Const` **registerTsProject**(`path`, `configFilename?`): `any` + +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). + +#### Parameters + +| Name | Type | Default value | +| :--------------- | :------- | :---------------- | +| `path` | `string` | `undefined` | +| `configFilename` | `string` | `'tsconfig.json'` | + +#### Returns + +`any` + +--- + ### removeDependenciesFromPackageJson ▸ **removeDependenciesFromPackageJson**(`tree`, `dependencies`, `devDependencies`, `packageJsonPath?`): [`GeneratorCallback`](../../nx-devkit/index#generatorcallback) diff --git a/packages/devkit/index.ts b/packages/devkit/index.ts index a31bec39828779..bebb1de419490b 100644 --- a/packages/devkit/index.ts +++ b/packages/devkit/index.ts @@ -189,6 +189,11 @@ export type { */ export { readJsonFile, writeJsonFile } from '@nrwl/tao/src/utils/fileutils'; +/** + * @category Utils + */ +export { registerTsProject } from '@nrwl/tao/src/utils/register'; + /** * @category Utils */ diff --git a/packages/eslint-plugin-nx/package.json b/packages/eslint-plugin-nx/package.json index 17b249080b48d2..7aad6286cd8b2e 100644 --- a/packages/eslint-plugin-nx/package.json +++ b/packages/eslint-plugin-nx/package.json @@ -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" } } diff --git a/packages/eslint-plugin-nx/src/resolve-workspace-rules.ts b/packages/eslint-plugin-nx/src/resolve-workspace-rules.ts index 127fda07d5843b..0d03e05b6c7b92 100644 --- a/packages/eslint-plugin-nx/src/resolve-workspace-rules.ts +++ b/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/devkit'; type ESLintRules = Record>; -/** - * 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 diff --git a/packages/tao/package.json b/packages/tao/package.json index b550e44e8dd256..3798c71a5888de 100644 --- a/packages/tao/package.json +++ b/packages/tao/package.json @@ -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", @@ -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" } } diff --git a/packages/tao/src/utils/register.ts b/packages/tao/src/utils/register.ts new file mode 100644 index 00000000000000..4c7d1895b868b9 --- /dev/null +++ b/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) {} +};