fix(core): local plugins should work under yarn workspaces (#14995)

This commit is contained in:
Craigory Coppola 2023-02-14 16:49:06 -05:00 committed by GitHub
parent 8165459568
commit d7c8e03b5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 6 deletions

View File

@ -1,14 +1,18 @@
import { sync as globSync } from 'fast-glob'; import { sync as globSync } from 'fast-glob';
import { existsSync, readFileSync } from 'fs'; import { existsSync, readFileSync, statSync } from 'fs';
import ignore, { Ignore } from 'ignore'; import ignore, { Ignore } from 'ignore';
import * as path from 'path'; import * as path from 'path';
import { basename, dirname, join } from 'path'; import { basename, dirname, extname, join } from 'path';
import { performance } from 'perf_hooks'; import { performance } from 'perf_hooks';
import { workspaceRoot } from '../utils/workspace-root'; import { workspaceRoot } from '../utils/workspace-root';
import { readJsonFile } from '../utils/fileutils'; import { readJsonFile } from '../utils/fileutils';
import { logger, NX_PREFIX } from '../utils/logger'; 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 * as yaml from 'js-yaml';
import type { NxJsonConfiguration, TargetDefaults } from './nx-json'; import type { NxJsonConfiguration, TargetDefaults } from './nx-json';
@ -313,7 +317,15 @@ export class Workspaces {
const [implementationModulePath, implementationExportName] = const [implementationModulePath, implementationExportName] =
implementation.split('#'); implementation.split('#');
return () => { 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 return implementationExportName
? module[implementationExportName] ? module[implementationExportName]
: module.default ?? module; : module.default ?? module;

View File

@ -181,7 +181,11 @@ export function resolveLocalNxPlugin(
let tsNodeAndPathsRegistered = false; 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) { if (!tsNodeAndPathsRegistered) {
// nx-ignore-next-line // nx-ignore-next-line
const ts: typeof import('typescript') = require('typescript'); const ts: typeof import('typescript') = require('typescript');
@ -208,7 +212,7 @@ function lookupLocalPlugin(importPath: string, root = workspaceRoot) {
} }
if (!tsNodeAndPathsRegistered) { if (!tsNodeAndPathsRegistered) {
registerTSTranspiler(); registerPluginTSTranspiler();
} }
const projectConfig = workspace.projects[plugin]; const projectConfig = workspace.projects[plugin];