|
| 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 } from 'path'; |
| 12 | +import { Script } from 'vm'; |
| 13 | + |
| 14 | +export class SchematicEngineHost extends NodeModulesEngineHost { |
| 15 | + protected _resolveReferenceString(refString: string, parentPath: string) { |
| 16 | + const [path, name] = refString.split('#', 2); |
| 17 | + const schematicFile = require.resolve(path, { paths: [parentPath] }); |
| 18 | + |
| 19 | + const isAngularSchematic = /[\/\\]node_modules[\/\\]@(?:angular|schematics)[\/\\]/.test( |
| 20 | + schematicFile, |
| 21 | + ); |
| 22 | + |
| 23 | + // Angular schematics are safe to use in the wrapped VM context |
| 24 | + if (isAngularSchematic) { |
| 25 | + const schematicPath = dirname(schematicFile); |
| 26 | + |
| 27 | + const moduleCache = new Map<string, unknown>(); |
| 28 | + const factoryInitializer = wrap( |
| 29 | + schematicFile, |
| 30 | + schematicPath, |
| 31 | + moduleCache, |
| 32 | + name || 'default', |
| 33 | + ) as () => RuleFactory<{}>; |
| 34 | + |
| 35 | + const factory = factoryInitializer(); |
| 36 | + if (!factory || typeof factory !== 'function') { |
| 37 | + return null; |
| 38 | + } |
| 39 | + |
| 40 | + return { ref: factory, path: schematicPath }; |
| 41 | + } |
| 42 | + |
| 43 | + // All other schematics use default behavior |
| 44 | + return super._resolveReferenceString(refString, parentPath); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Wrap a JavaScript file in a VM context to allow specific Angular dependencies to be redirected. |
| 50 | + * This VM setup is ONLY intended to redirect dependencies. |
| 51 | + * |
| 52 | + * @param schematicFile A JavaScript schematic file path that should be wrapped. |
| 53 | + * @param schematicDirectory A directory that will be used as the location of the JavaScript file. |
| 54 | + * @param moduleCache A map to use for caching repeat module usage and proper `instanceof` support. |
| 55 | + * @param exportName An optional name of a specific export to return. Otherwise, return all exports. |
| 56 | + */ |
| 57 | +function wrap( |
| 58 | + schematicFile: string, |
| 59 | + schematicDirectory: string, |
| 60 | + moduleCache: Map<string, unknown>, |
| 61 | + exportName?: string, |
| 62 | +): () => unknown { |
| 63 | + const { createRequire, createRequireFromPath } = require('module'); |
| 64 | + // Node.js 10.x does not support `createRequire` so fallback to `createRequireFromPath` |
| 65 | + // `createRequireFromPath` is deprecated in 12+ and can be removed once 10.x support is removed |
| 66 | + const scopedRequire = createRequire?.(schematicFile) || createRequireFromPath(schematicDirectory); |
| 67 | + |
| 68 | + const customRequire = function (id: string) { |
| 69 | + if (id.startsWith('@angular-devkit/') || id.startsWith('@schematics/')) { |
| 70 | + // Resolve from inside the @angular/cli project |
| 71 | + const packagePath = require.resolve(id); |
| 72 | + |
| 73 | + return require(packagePath); |
| 74 | + } else if (id.startsWith('.') || id.startsWith('@angular/cdk')) { |
| 75 | + // Resolve from the original file |
| 76 | + const modulePath = scopedRequire.resolve(id); |
| 77 | + |
| 78 | + // Do not wrap vendored third-party packages |
| 79 | + if (!modulePath.includes('@schematics/angular/third_party')) { |
| 80 | + const cachedModule = moduleCache.get(modulePath); |
| 81 | + if (cachedModule) { |
| 82 | + return cachedModule; |
| 83 | + } |
| 84 | + |
| 85 | + const wrappedModule = wrap(modulePath, dirname(modulePath), moduleCache)(); |
| 86 | + moduleCache.set(modulePath, wrappedModule); |
| 87 | + |
| 88 | + return wrappedModule; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // All others are required directly from the original file |
| 93 | + return scopedRequire(id); |
| 94 | + }; |
| 95 | + |
| 96 | + // Setup a wrapper function to capture the module's exports |
| 97 | + const schematicCode = readFileSync(schematicFile, 'utf8'); |
| 98 | + // `module` is required due to @angular/localize ng-add being in UMD format |
| 99 | + const headerCode = '(function() {\nvar exports = Object.create(null);\nvar module = { exports };\n'; |
| 100 | + const footerCode = exportName ? `\nreturn exports['${exportName}'];});` : '\nreturn exports;});'; |
| 101 | + |
| 102 | + const script = new Script(headerCode + schematicCode + footerCode, { |
| 103 | + filename: schematicFile, |
| 104 | + lineOffset: 3, |
| 105 | + }); |
| 106 | + |
| 107 | + const context = { |
| 108 | + __dirname: schematicDirectory, |
| 109 | + __filename: schematicFile, |
| 110 | + Buffer, |
| 111 | + console, |
| 112 | + process, |
| 113 | + get global() { |
| 114 | + return this; |
| 115 | + }, |
| 116 | + require: customRequire, |
| 117 | + }; |
| 118 | + |
| 119 | + const exportsFactory = script.runInNewContext(context); |
| 120 | + |
| 121 | + return exportsFactory; |
| 122 | +} |
0 commit comments