Skip to content

Commit

Permalink
feat(core): add a monkey-patch for require to use @nx packages instea… (
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz committed Apr 24, 2023
1 parent 5272e5a commit 26ad8eb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
43 changes: 43 additions & 0 deletions packages/nx/bin/init-local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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.
Expand Down Expand Up @@ -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
};
}
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down
2 changes: 2 additions & 0 deletions packages/nx/src/utils/register.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// TODO(v17): remove this re-export
export * from '../plugins/js/utils/register';

1 comment on commit 26ad8eb

@vercel
Copy link

@vercel vercel bot commented on 26ad8eb Apr 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-five.vercel.app
nx-dev-nrwl.vercel.app
nx.dev
nx-dev-git-master-nrwl.vercel.app

Please sign in to comment.