Skip to content

Commit

Permalink
fix(@angular/cli): correctly redirect nested Angular schematic depend…
Browse files Browse the repository at this point in the history
…ency requests

Closes #21075

(cherry picked from commit cf3b22d)
  • Loading branch information
alan-agius4 committed Jun 8, 2021
1 parent 3fcf318 commit 814f0bb
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions packages/angular/cli/models/schematic-engine-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -115,10 +120,8 @@ function wrap(
moduleCache: Map<string, unknown>,
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]) {
Expand Down

0 comments on commit 814f0bb

Please sign in to comment.