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]) {