|
| 1 | +// @ignoreDep @angular/compiler-cli |
1 | 2 | /**
|
2 | 3 | * This is a copy of types in @compiler-cli/src/ngtools_api.d.ts file,
|
3 |
| - * together with a safe import to support cases where Angular versions is below 5. |
| 4 | + * together with safe imports for private apis for cases where @angular/compiler-cli isn't |
| 5 | + * available or is below version 5. |
4 | 6 | */
|
| 7 | +import * as path from 'path'; |
5 | 8 | import * as ts from 'typescript';
|
6 | 9 |
|
7 | 10 | export const DEFAULT_ERROR_CODE = 100;
|
@@ -102,6 +105,52 @@ export interface FormatDiagnosticsInterface {
|
102 | 105 | (options: CompilerOptions, diags: Diagnostics): string;
|
103 | 106 | }
|
104 | 107 |
|
| 108 | +// Manually check for Compiler CLI availability and supported version. |
| 109 | +// This is needed because @ngtools/webpack does not depend directly on @angular/compiler-cli, since |
| 110 | +// it is installed as part of global Angular CLI installs and compiler-cli is not of its |
| 111 | +// dependencies. |
| 112 | +export function CompilerCliIsSupported() { |
| 113 | + let version; |
| 114 | + |
| 115 | + // Check that Angular is available. |
| 116 | + try { |
| 117 | + version = require('@angular/compiler-cli').VERSION; |
| 118 | + } catch (e) { |
| 119 | + throw new Error('The "@angular/compiler-cli" package was not properly installed. Error: ' + e); |
| 120 | + } |
| 121 | + |
| 122 | + // Check that Angular is also not part of this module's node_modules (it should be the project's). |
| 123 | + const compilerCliPath = require.resolve('@angular/compiler-cli'); |
| 124 | + if (compilerCliPath.startsWith(path.dirname(__dirname))) { |
| 125 | + throw new Error('The @ngtools/webpack plugin now relies on the project @angular/compiler-cli. ' |
| 126 | + + 'Please clean your node_modules and reinstall.'); |
| 127 | + } |
| 128 | + |
| 129 | + // Throw if we're neither 2.3.1 or more, nor 4.x.y, nor 5.x.y. |
| 130 | + if (!(version.major == '5' |
| 131 | + || version.major == '4' |
| 132 | + || (version.major == '2' |
| 133 | + && (version.minor == '4' |
| 134 | + || version.minor == '3' && version.patch == '1')))) { |
| 135 | + throw new Error('Version of @angular/compiler-cli needs to be 2.3.1 or greater. ' |
| 136 | + + `Current version is "${version.full}".`); |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +// These imports do not exist on a global install for Angular CLI, so we cannot use a static ES6 |
| 141 | +// import. |
| 142 | +let compilerCli: any = {}; |
| 143 | +try { |
| 144 | + compilerCli = require('@angular/compiler-cli'); |
| 145 | +} catch (e) { |
| 146 | + // Don't throw an error if the private API does not exist. |
| 147 | + // Instead, the `CompilerCliIsSupported` method should return throw and indicate the |
| 148 | + // plugin cannot be used. |
| 149 | +} |
| 150 | + |
| 151 | +export const VERSION = compilerCli.VERSION; |
| 152 | +export const __NGTOOLS_PRIVATE_API_2 = compilerCli.__NGTOOLS_PRIVATE_API_2; |
| 153 | + |
105 | 154 | // These imports do not exist on Angular versions lower than 5, so we cannot use a static ES6
|
106 | 155 | // import.
|
107 | 156 | let ngtools2: any = {};
|
|
0 commit comments