Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(@angular/cli): correctly redirect nested Angular schematic dependency requests #21081

Merged
merged 1 commit into from
Jun 8, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
Copy link
Member

Choose a reason for hiding this comment

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

nit: This can be an import now.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export.

const scopedRequire = createRequire(schematicFile);

const customRequire = function (id: string) {
if (legacyModules[id]) {
Expand Down