UNPKG

4.81 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.setDependenciesTsConfigPaths = exports.initializeTsConfig = exports.createDefaultTsConfig = exports.readDefaultTsConfig = void 0;
4const ng = require("@angular/compiler-cli");
5const path = require("path");
6const ts = require("typescript");
7const log = require("../utils/log");
8/**
9 * Reads the default TypeScript configuration.
10 */
11function readDefaultTsConfig(fileName) {
12 if (!fileName) {
13 fileName = path.resolve(__dirname, 'conf', 'tsconfig.ngc.json');
14 }
15 // these options are mandatory
16 const extraOptions = {
17 moduleResolution: ts.ModuleResolutionKind.NodeJs,
18 target: ts.ScriptTarget.ES2015,
19 experimentalDecorators: true,
20 // sourcemaps
21 sourceMap: false,
22 inlineSources: true,
23 inlineSourceMap: true,
24 outDir: '',
25 declaration: true,
26 // ng compiler to options
27 enableResourceInlining: true,
28 // these are required to set the appropriate EmitFlags
29 flatModuleId: 'AUTOGENERATED',
30 flatModuleOutFile: 'AUTOGENERATED',
31 };
32 const config = ng.readConfiguration(fileName, extraOptions);
33 const options = config.options;
34 // todo: alanagius4 - the below shouldn't be needed but it seems that setting it only in create-emit-callback.ts
35 // is not working correctly
36 const transformDecorators = !options.enableIvy && options.annotationsAs !== 'decorators';
37 if ((options.annotateForClosureCompiler || options.annotationsAs === 'static fields') && transformDecorators) {
38 // This is needed as a workaround for https://github.com/angular/tsickle/issues/635
39 // Otherwise tsickle might emit references to non imported values
40 // as TypeScript elided the import.
41 options.emitDecoratorMetadata = true;
42 }
43 return config;
44}
45exports.readDefaultTsConfig = readDefaultTsConfig;
46/**
47 * Creates a parsed TypeScript configuration object.
48 *
49 * @param values File path or parsed configuration.
50 */
51function createDefaultTsConfig(values) {
52 if (!values) {
53 return readDefaultTsConfig();
54 }
55 else if (typeof values === 'string') {
56 return readDefaultTsConfig(values);
57 }
58 else {
59 return values;
60 }
61}
62exports.createDefaultTsConfig = createDefaultTsConfig;
63/**
64 * Initializes TypeScript Compiler options and Angular Compiler options by overriding the
65 * default config with entry point-specific values.
66 */
67const initializeTsConfig = (defaultTsConfig, entryPoints) => {
68 if (defaultTsConfig.errors.length > 0) {
69 throw ng.formatDiagnostics(defaultTsConfig.errors);
70 }
71 entryPoints.forEach(currentEntryPoint => {
72 const { entryPoint } = currentEntryPoint.data;
73 log.debug(`Initializing tsconfig for ${entryPoint.moduleId}`);
74 const basePath = path.dirname(entryPoint.entryFilePath);
75 // Resolve defaults from DI token and create a deep copy of the defaults
76 let tsConfig = JSON.parse(JSON.stringify(defaultTsConfig));
77 const overrideOptions = {
78 flatModuleId: entryPoint.moduleId,
79 flatModuleOutFile: `${entryPoint.flatModuleFile}.js`,
80 basePath,
81 rootDir: basePath,
82 sourceRoot: '',
83 };
84 tsConfig.rootNames = [entryPoint.entryFilePath];
85 tsConfig.options = { ...tsConfig.options, ...overrideOptions };
86 currentEntryPoint.data.tsConfig = tsConfig;
87 });
88};
89exports.initializeTsConfig = initializeTsConfig;
90/**
91 * Set the paths for entrypoint dependencies.
92 *
93 * This doesn't mutate the object.
94 *
95 * @param parsedTsConfig - A parsed tsconfig
96 * @param entryPoints - A list of entryPoints
97 * @param pointToSource Point the path mapping to either the source code or emitted declarations.
98 * Typically for analysis one should point to the source files while for a compilation once should use the emitted declarations
99 */
100function setDependenciesTsConfigPaths(parsedTsConfig, entryPoints, pointToSource = false) {
101 const tsConfig = JSON.parse(JSON.stringify(parsedTsConfig));
102 // Add paths mappings for dependencies
103 if (!tsConfig.options.paths) {
104 tsConfig.options.paths = {};
105 }
106 for (let dep of entryPoints) {
107 const { entryPoint } = dep.data;
108 const { moduleId, destinationFiles, entryFilePath } = entryPoint;
109 const mappedPath = [pointToSource ? entryFilePath : destinationFiles.declarations];
110 if (!tsConfig.options.paths[moduleId]) {
111 tsConfig.options.paths[moduleId] = mappedPath;
112 }
113 else {
114 tsConfig.options.paths[moduleId].unshift(...mappedPath);
115 }
116 }
117 return tsConfig;
118}
119exports.setDependenciesTsConfigPaths = setDependenciesTsConfigPaths;
120//# sourceMappingURL=tsconfig.js.map
\No newline at end of file