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 3, 2022
1 parent a4e95de commit dd351a7
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 35 deletions.
25 changes: 25 additions & 0 deletions docs/generated/api-nx-devkit/index.md
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions packages/devkit/index.ts
Expand Up @@ -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
*/
Expand Down
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/devkit';

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 dd351a7

Please sign in to comment.