|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | +import { RuleFactory } from '@angular-devkit/schematics'; |
| 9 | +import { NodeModulesEngineHost } from '@angular-devkit/schematics/tools'; |
| 10 | +import { readFileSync } from 'fs'; |
| 11 | +import { dirname, resolve } from 'path'; |
| 12 | +import { Script } from 'vm'; |
| 13 | + |
| 14 | +/** |
| 15 | + * Environment variable to control schematic package redirection |
| 16 | + * Default: Angular schematics only |
| 17 | + */ |
| 18 | +const schematicRedirectVariable = process.env['NG_SCHEMATIC_REDIRECT']?.toLowerCase(); |
| 19 | + |
| 20 | +function shouldWrapSchematic(schematicFile: string): boolean { |
| 21 | + // Check environment variable if present |
| 22 | + if (schematicRedirectVariable !== undefined) { |
| 23 | + switch (schematicRedirectVariable) { |
| 24 | + case '0': |
| 25 | + case 'false': |
| 26 | + case 'off': |
| 27 | + case 'none': |
| 28 | + return false; |
| 29 | + case 'all': |
| 30 | + return true; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + // Default is only first-party Angular schematic packages |
| 35 | + // Angular schematics are safe to use in the wrapped VM context |
| 36 | + return /[\/\\]node_modules[\/\\]@(?:angular|schematics|nguniversal)[\/\\]/.test( |
| 37 | + schematicFile, |
| 38 | + ); |
| 39 | +} |
| 40 | + |
| 41 | +export class SchematicEngineHost extends NodeModulesEngineHost { |
| 42 | + protected _resolveReferenceString(refString: string, parentPath: string) { |
| 43 | + const [path, name] = refString.split('#', 2); |
| 44 | + // Mimic behavior of ExportStringRef class used in default behavior |
| 45 | + const fullPath = path[0] === '.' ? resolve(parentPath ?? process.cwd(), path) : path; |
| 46 | + |
| 47 | + const schematicFile = require.resolve(fullPath, { paths: [parentPath] }); |
| 48 | + |
| 49 | + if (shouldWrapSchematic(schematicFile)) { |
| 50 | + const schematicPath = dirname(schematicFile); |
| 51 | + |
| 52 | + const moduleCache = new Map<string, unknown>(); |
| 53 | + const factoryInitializer = wrap( |
| 54 | + schematicFile, |
| 55 | + schematicPath, |
| 56 | + moduleCache, |
| 57 | + name || 'default', |
| 58 | + ) as () => RuleFactory<{}>; |
| 59 | + |
| 60 | + const factory = factoryInitializer(); |
| 61 | + if (!factory || typeof factory !== 'function') { |
| 62 | + return null; |
| 63 | + } |
| 64 | + |
| 65 | + return { ref: factory, path: schematicPath }; |
| 66 | + } |
| 67 | + |
| 68 | + // All other schematics use default behavior |
| 69 | + return super._resolveReferenceString(refString, parentPath); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * Wrap a JavaScript file in a VM context to allow specific Angular dependencies to be redirected. |
| 75 | + * This VM setup is ONLY intended to redirect dependencies. |
| 76 | + * |
| 77 | + * @param schematicFile A JavaScript schematic file path that should be wrapped. |
| 78 | + * @param schematicDirectory A directory that will be used as the location of the JavaScript file. |
| 79 | + * @param moduleCache A map to use for caching repeat module usage and proper `instanceof` support. |
| 80 | + * @param exportName An optional name of a specific export to return. Otherwise, return all exports. |
| 81 | + */ |
| 82 | +function wrap( |
| 83 | + schematicFile: string, |
| 84 | + schematicDirectory: string, |
| 85 | + moduleCache: Map<string, unknown>, |
| 86 | + exportName?: string, |
| 87 | +): () => unknown { |
| 88 | + const { createRequire, createRequireFromPath } = require('module'); |
| 89 | + // Node.js 10.x does not support `createRequire` so fallback to `createRequireFromPath` |
| 90 | + // `createRequireFromPath` is deprecated in 12+ and can be removed once 10.x support is removed |
| 91 | + const scopedRequire = createRequire?.(schematicFile) || createRequireFromPath(schematicFile); |
| 92 | + |
| 93 | + const customRequire = function (id: string) { |
| 94 | + if (id.startsWith('@angular-devkit/') || id.startsWith('@schematics/')) { |
| 95 | + // Resolve from inside the `@angular/cli` project |
| 96 | + const packagePath = require.resolve(id); |
| 97 | + |
| 98 | + return require(packagePath); |
| 99 | + } else if (id.startsWith('.') || id.startsWith('@angular/cdk')) { |
| 100 | + // Wrap relative files inside the schematic collection |
| 101 | + // Also wrap `@angular/cdk`, it contains helper utilities that import core schematic packages |
| 102 | + |
| 103 | + // Resolve from the original file |
| 104 | + const modulePath = scopedRequire.resolve(id); |
| 105 | + |
| 106 | + // Use cached module if available |
| 107 | + const cachedModule = moduleCache.get(modulePath); |
| 108 | + if (cachedModule) { |
| 109 | + return cachedModule; |
| 110 | + } |
| 111 | + |
| 112 | + // Do not wrap vendored third-party packages |
| 113 | + if ( |
| 114 | + !/[\/\\]node_modules[\/\\]@schematics[\/\\]angular[\/\\]third_party[\/\\]/.test(modulePath) |
| 115 | + ) { |
| 116 | + // Wrap module and save in cache |
| 117 | + const wrappedModule = wrap(modulePath, dirname(modulePath), moduleCache)(); |
| 118 | + moduleCache.set(modulePath, wrappedModule); |
| 119 | + |
| 120 | + return wrappedModule; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + // All others are required directly from the original file |
| 125 | + return scopedRequire(id); |
| 126 | + }; |
| 127 | + |
| 128 | + // Setup a wrapper function to capture the module's exports |
| 129 | + const schematicCode = readFileSync(schematicFile, 'utf8'); |
| 130 | + // `module` is required due to @angular/localize ng-add being in UMD format |
| 131 | + const headerCode = |
| 132 | + '(function() {\nvar exports = Object.create(null);\nvar module = { exports };\n'; |
| 133 | + const footerCode = exportName ? `\nreturn exports['${exportName}'];});` : '\nreturn exports;});'; |
| 134 | + |
| 135 | + const script = new Script(headerCode + schematicCode + footerCode, { |
| 136 | + filename: schematicFile, |
| 137 | + lineOffset: 3, |
| 138 | + }); |
| 139 | + |
| 140 | + const context = { |
| 141 | + __dirname: schematicDirectory, |
| 142 | + __filename: schematicFile, |
| 143 | + Buffer, |
| 144 | + console, |
| 145 | + process, |
| 146 | + get global() { |
| 147 | + return this; |
| 148 | + }, |
| 149 | + require: customRequire, |
| 150 | + }; |
| 151 | + |
| 152 | + const exportsFactory = script.runInNewContext(context); |
| 153 | + |
| 154 | + return exportsFactory; |
| 155 | +} |
0 commit comments