From 814f0bb669423baf0f04e15ae739a9d4070a8dc0 Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Tue, 8 Jun 2021 17:48:48 +0200 Subject: [PATCH] fix(@angular/cli): correctly redirect nested Angular schematic dependency requests Closes #21075 (cherry picked from commit cf3b22de581104d276806d7083f199049dfc93f1) --- .../angular/cli/models/schematic-engine-host.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/angular/cli/models/schematic-engine-host.ts b/packages/angular/cli/models/schematic-engine-host.ts index 05e3c3da25f3..2dca2f3705f2 100644 --- a/packages/angular/cli/models/schematic-engine-host.ts +++ b/packages/angular/cli/models/schematic-engine-host.ts @@ -33,15 +33,20 @@ function shouldWrapSchematic(schematicFile: string): boolean { } } + const normalizedSchematicFile = schematicFile.replace(/\\/g, '/'); // Never wrap the internal update schematic when executed directly // It communicates with the update command via `global` - if (/[\/\\]node_modules[\/\\]@angular[\/\\]cli[\/\\]/.test(schematicFile)) { + // But we still want to redirect schematics located in `@angular/cli/node_modules`. + if ( + normalizedSchematicFile.includes('node_modules/@angular/cli/') && + !normalizedSchematicFile.includes('node_modules/@angular/cli/node_modules/') + ) { return false; } // Default is only first-party Angular schematic packages // Angular schematics are safe to use in the wrapped VM context - return /[\/\\]node_modules[\/\\]@(?:angular|schematics|nguniversal)[\/\\]/.test(schematicFile); + return /\/node_modules\/@(?:angular|schematics|nguniversal)\//.test(normalizedSchematicFile); } export class SchematicEngineHost extends NodeModulesEngineHost { @@ -115,10 +120,8 @@ function wrap( moduleCache: Map, exportName?: string, ): () => unknown { - const { createRequire, createRequireFromPath } = require('module'); - // Node.js 10.x does not support `createRequire` so fallback to `createRequireFromPath` - // `createRequireFromPath` is deprecated in 12+ and can be removed once 10.x support is removed - const scopedRequire = createRequire?.(schematicFile) || createRequireFromPath(schematicFile); + const { createRequire } = require('module'); + const scopedRequire = createRequire(schematicFile); const customRequire = function (id: string) { if (legacyModules[id]) {