1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
10 | const ngcc_1 = require("@angular/compiler-cli/ngcc");
|
11 | const child_process_1 = require("child_process");
|
12 | const fs_1 = require("fs");
|
13 | const path = require("path");
|
14 | const benchmark_1 = require("./benchmark");
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | class NgccProcessor {
|
23 | constructor(propertiesToConsider, inputFileSystem, compilationWarnings, compilationErrors, basePath, compilerOptions, tsConfigPath) {
|
24 | this.propertiesToConsider = propertiesToConsider;
|
25 | this.inputFileSystem = inputFileSystem;
|
26 | this.compilationWarnings = compilationWarnings;
|
27 | this.compilationErrors = compilationErrors;
|
28 | this.basePath = basePath;
|
29 | this.compilerOptions = compilerOptions;
|
30 | this.tsConfigPath = tsConfigPath;
|
31 | this._processedModules = new Set();
|
32 | this._logger = new NgccLogger(this.compilationWarnings, this.compilationErrors);
|
33 | this._nodeModulesDirectory = this.findNodeModulesDirectory(this.basePath);
|
34 | const { baseUrl, paths } = this.compilerOptions;
|
35 | if (baseUrl && paths) {
|
36 | this._pathMappings = {
|
37 | baseUrl,
|
38 | paths,
|
39 | };
|
40 | }
|
41 | }
|
42 |
|
43 | process() {
|
44 |
|
45 | if (process.env.BAZEL_TARGET) {
|
46 | return;
|
47 | }
|
48 |
|
49 | const corePackage = this.tryResolvePackage('@angular/core', this._nodeModulesDirectory);
|
50 | if (corePackage && isReadOnlyFile(corePackage)) {
|
51 | return;
|
52 | }
|
53 | const timeLabel = 'NgccProcessor.process';
|
54 | benchmark_1.time(timeLabel);
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | const { status, error } = child_process_1.spawnSync(process.execPath, [
|
61 | require.resolve('@angular/compiler-cli/ngcc/main-ngcc.js'),
|
62 | '--source',
|
63 | this._nodeModulesDirectory,
|
64 | '--properties',
|
65 | ...this.propertiesToConsider,
|
66 | '--first-only',
|
67 | '--create-ivy-entry-points',
|
68 | '--async',
|
69 | '--tsconfig',
|
70 | this.tsConfigPath,
|
71 | ], {
|
72 | stdio: ['inherit', process.stderr, process.stderr],
|
73 | });
|
74 | if (status !== 0) {
|
75 | const errorMessage = (error === null || error === void 0 ? void 0 : error.message) || '';
|
76 | throw new Error(errorMessage + `NGCC failed${errorMessage ? ', see above' : ''}.`);
|
77 | }
|
78 | benchmark_1.timeEnd(timeLabel);
|
79 | }
|
80 |
|
81 | processModule(moduleName, resolvedModule) {
|
82 | const resolvedFileName = resolvedModule.resolvedFileName;
|
83 | if (!resolvedFileName || moduleName.startsWith('.')
|
84 | || this._processedModules.has(resolvedFileName)) {
|
85 |
|
86 | return;
|
87 | }
|
88 | const packageJsonPath = this.tryResolvePackage(moduleName, resolvedFileName);
|
89 |
|
90 |
|
91 | if (!packageJsonPath || isReadOnlyFile(packageJsonPath)) {
|
92 |
|
93 | this._processedModules.add(resolvedFileName);
|
94 | return;
|
95 | }
|
96 | const timeLabel = `NgccProcessor.processModule.ngcc.process+${moduleName}`;
|
97 | benchmark_1.time(timeLabel);
|
98 | ngcc_1.process({
|
99 | basePath: this._nodeModulesDirectory,
|
100 | targetEntryPointPath: path.dirname(packageJsonPath),
|
101 | propertiesToConsider: this.propertiesToConsider,
|
102 | compileAllFormats: false,
|
103 | createNewEntryPointFormats: true,
|
104 | logger: this._logger,
|
105 |
|
106 |
|
107 | pathMappings: this._pathMappings,
|
108 | tsConfigPath: this.tsConfigPath,
|
109 | });
|
110 | benchmark_1.timeEnd(timeLabel);
|
111 |
|
112 |
|
113 |
|
114 | this.inputFileSystem.purge(packageJsonPath);
|
115 | this._processedModules.add(resolvedFileName);
|
116 | }
|
117 | invalidate(fileName) {
|
118 | this._processedModules.delete(fileName);
|
119 | }
|
120 | |
121 |
|
122 |
|
123 | tryResolvePackage(moduleName, resolvedFileName) {
|
124 | try {
|
125 |
|
126 |
|
127 |
|
128 | return require.resolve(`${moduleName}/package.json`, {
|
129 | paths: [resolvedFileName],
|
130 | });
|
131 | }
|
132 | catch (_a) {
|
133 |
|
134 |
|
135 |
|
136 | const packageJsonPath = path.resolve(resolvedFileName, '../package.json');
|
137 | return fs_1.existsSync(packageJsonPath) ? packageJsonPath : undefined;
|
138 | }
|
139 | }
|
140 | findNodeModulesDirectory(startPoint) {
|
141 | let current = startPoint;
|
142 | while (path.dirname(current) !== current) {
|
143 | const nodePath = path.join(current, 'node_modules');
|
144 | if (fs_1.existsSync(nodePath)) {
|
145 | return nodePath;
|
146 | }
|
147 | current = path.dirname(current);
|
148 | }
|
149 | throw new Error(`Cannot locate the 'node_modules' directory.`);
|
150 | }
|
151 | }
|
152 | exports.NgccProcessor = NgccProcessor;
|
153 | class NgccLogger {
|
154 | constructor(compilationWarnings, compilationErrors) {
|
155 | this.compilationWarnings = compilationWarnings;
|
156 | this.compilationErrors = compilationErrors;
|
157 | this.level = ngcc_1.LogLevel.info;
|
158 | }
|
159 | debug(..._args) { }
|
160 | info(...args) {
|
161 |
|
162 | process.stderr.write(`\n${args.join(' ')}\n`);
|
163 | }
|
164 | warn(...args) {
|
165 | this.compilationWarnings.push(args.join(' '));
|
166 | }
|
167 | error(...args) {
|
168 | this.compilationErrors.push(new Error(args.join(' ')));
|
169 | }
|
170 | }
|
171 | function isReadOnlyFile(fileName) {
|
172 | try {
|
173 | fs_1.accessSync(fileName, fs_1.constants.W_OK);
|
174 | return false;
|
175 | }
|
176 | catch (_a) {
|
177 | return true;
|
178 | }
|
179 | }
|