From 7cbb2825e3384f5dda34ece19de473316cf48dcd Mon Sep 17 00:00:00 2001 From: Craigory Coppola Date: Tue, 14 Feb 2023 16:49:06 -0500 Subject: [PATCH] fix(core): local plugins should work under yarn workspaces (#14995) (cherry picked from commit d7c8e03b5e2dfbd70dbfa15d9ff24897c5836cdc) --- packages/nx/src/config/workspaces.ts | 20 ++++++++++++++++---- packages/nx/src/utils/nx-plugin.ts | 8 ++++++-- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/packages/nx/src/config/workspaces.ts b/packages/nx/src/config/workspaces.ts index 0056ae41d4616..c575729885e75 100644 --- a/packages/nx/src/config/workspaces.ts +++ b/packages/nx/src/config/workspaces.ts @@ -1,14 +1,18 @@ import { sync as globSync } from 'fast-glob'; -import { existsSync, readFileSync } from 'fs'; +import { existsSync, readFileSync, statSync } from 'fs'; import ignore, { Ignore } from 'ignore'; import * as path from 'path'; -import { basename, dirname, join } from 'path'; +import { basename, dirname, extname, join } from 'path'; import { performance } from 'perf_hooks'; import { workspaceRoot } from '../utils/workspace-root'; import { readJsonFile } from '../utils/fileutils'; import { logger, NX_PREFIX } from '../utils/logger'; -import { loadNxPlugins, readPluginPackageJson } from '../utils/nx-plugin'; +import { + loadNxPlugins, + readPluginPackageJson, + registerPluginTSTranspiler, +} from '../utils/nx-plugin'; import * as yaml from 'js-yaml'; import type { NxJsonConfiguration, TargetDefaults } from './nx-json'; @@ -313,7 +317,15 @@ export class Workspaces { const [implementationModulePath, implementationExportName] = implementation.split('#'); return () => { - const module = require(path.join(directory, implementationModulePath)); + const possibleModulePath = path.join(directory, implementationModulePath); + const validImplementations = ['', '.js', '.ts'].map( + (x) => possibleModulePath + x + ); + const modulePath = validImplementations.find((f) => existsSync(f)); + if (extname(modulePath) === '.ts') { + registerPluginTSTranspiler(); + } + const module = require(modulePath); return implementationExportName ? module[implementationExportName] : module.default ?? module; diff --git a/packages/nx/src/utils/nx-plugin.ts b/packages/nx/src/utils/nx-plugin.ts index 36d20b6c8c5d8..e24d9aae9b9a6 100644 --- a/packages/nx/src/utils/nx-plugin.ts +++ b/packages/nx/src/utils/nx-plugin.ts @@ -181,7 +181,11 @@ export function resolveLocalNxPlugin( let tsNodeAndPathsRegistered = false; -function registerTSTranspiler() { +/** + * Register swc-node or ts-node if they are not currently registered + * with some default settings which work well for Nx plugins. + */ +export function registerPluginTSTranspiler() { if (!tsNodeAndPathsRegistered) { // nx-ignore-next-line const ts: typeof import('typescript') = require('typescript'); @@ -208,7 +212,7 @@ function lookupLocalPlugin(importPath: string, root = workspaceRoot) { } if (!tsNodeAndPathsRegistered) { - registerTSTranspiler(); + registerPluginTSTranspiler(); } const projectConfig = workspace.projects[plugin];