Skip to content

Commit

Permalink
fix(core): local plugins should work under yarn workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Feb 14, 2023
1 parent 041121e commit 4cec9ce
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
20 changes: 16 additions & 4 deletions 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';
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 6 additions & 2 deletions packages/nx/src/utils/nx-plugin.ts
Expand Up @@ -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');
Expand All @@ -208,7 +212,7 @@ function lookupLocalPlugin(importPath: string, root = workspaceRoot) {
}

if (!tsNodeAndPathsRegistered) {
registerTSTranspiler();
registerPluginTSTranspiler();
}

const projectConfig = workspace.projects[plugin];
Expand Down

0 comments on commit 4cec9ce

Please sign in to comment.