From 26ad8eb14217bd78519d45447aaecfb866b1efb4 Mon Sep 17 00:00:00 2001 From: Jason Jean Date: Mon, 24 Apr 2023 15:05:25 -0400 Subject: [PATCH] =?UTF-8?q?feat(core):=20add=20a=20monkey-patch=20for=20re?= =?UTF-8?q?quire=20to=20use=20@nx=20packages=20instea=E2=80=A6=20(#16511)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/nx/bin/init-local.ts | 43 +++++++++++++++++++ .../{ => plugins/js}/utils/register.spec.ts | 2 +- packages/nx/src/utils/register.ts | 2 + 3 files changed, 46 insertions(+), 1 deletion(-) rename packages/nx/src/{ => plugins/js}/utils/register.spec.ts (92%) create mode 100644 packages/nx/src/utils/register.ts diff --git a/packages/nx/bin/init-local.ts b/packages/nx/bin/init-local.ts index 2159fbc5283f6..069393ec6e50d 100644 --- a/packages/nx/bin/init-local.ts +++ b/packages/nx/bin/init-local.ts @@ -7,6 +7,8 @@ import { commandsObject } from '../src/command-line/nx-commands'; import { WorkspaceTypeAndRoot } from '../src/utils/find-workspace-root'; import { stripIndents } from '../src/utils/strip-indents'; +import * as Mod from 'module'; + /** * Nx is being run inside a workspace. * @@ -20,6 +22,8 @@ export function initLocal(workspace: WorkspaceTypeAndRoot) { performance.mark('init-local'); require('nx/src/utils/perf-logging'); + monkeyPatchRequire(); + if (workspace.type !== 'nx' && shouldDelegateToAngularCLI()) { console.warn( stripIndents`Using Nx to run Angular CLI commands is deprecated and will be removed in a future version. @@ -218,3 +222,42 @@ function handleAngularCLIFallbacks(workspace: WorkspaceTypeAndRoot) { } } } + +// TODO(v17): Remove this once the @nrwl/* packages are not +function monkeyPatchRequire() { + const originalRequire = Mod.prototype.require; + + (Mod.prototype.require as any) = function (...args) { + const modulePath = args[0]; + if (!modulePath.startsWith('@nrwl/')) { + return originalRequire.apply(this, args); + } else { + try { + // Try the original require + return originalRequire.apply(this, args); + } catch (e) { + if (e.code !== 'MODULE_NOT_FOUND') { + throw e; + } + + try { + // Retry the require with the @nx package + return originalRequire.apply( + this, + args.map((value, i) => { + if (i !== 0) { + return value; + } else { + return value.replace('@nrwl/', '@nx/'); + } + }) + ); + } catch { + // Throw the original error + throw e; + } + } + } + // do some side-effect of your own + }; +} diff --git a/packages/nx/src/utils/register.spec.ts b/packages/nx/src/plugins/js/utils/register.spec.ts similarity index 92% rename from packages/nx/src/utils/register.spec.ts rename to packages/nx/src/plugins/js/utils/register.spec.ts index 52e9127d0330d..3cdba50c48d15 100644 --- a/packages/nx/src/utils/register.spec.ts +++ b/packages/nx/src/plugins/js/utils/register.spec.ts @@ -1,5 +1,5 @@ import { JsxEmit, ModuleKind, ScriptTarget } from 'typescript'; -import { getTsNodeCompilerOptions } from '../plugins/js/utils/register'; +import { getTsNodeCompilerOptions } from './register'; describe('getTsNodeCompilerOptions', () => { it('should replace enum value with enum key for module', () => { diff --git a/packages/nx/src/utils/register.ts b/packages/nx/src/utils/register.ts new file mode 100644 index 0000000000000..823c5f5056b9b --- /dev/null +++ b/packages/nx/src/utils/register.ts @@ -0,0 +1,2 @@ +// TODO(v17): remove this re-export +export * from '../plugins/js/utils/register';