UNPKG

5.4 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 var desc = Object.getOwnPropertyDescriptor(m, k);
5 if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6 desc = { enumerable: true, get: function() { return m[k]; } };
7 }
8 Object.defineProperty(o, k2, desc);
9}) : (function(o, m, k, k2) {
10 if (k2 === undefined) k2 = k;
11 o[k2] = m[k];
12}));
13var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14 Object.defineProperty(o, "default", { enumerable: true, value: v });
15}) : function(o, v) {
16 o["default"] = v;
17});
18var __importStar = (this && this.__importStar) || function (mod) {
19 if (mod && mod.__esModule) return mod;
20 var result = {};
21 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22 __setModuleDefault(result, mod);
23 return result;
24};
25var __importDefault = (this && this.__importDefault) || function (mod) {
26 return (mod && mod.__esModule) ? mod : { "default": mod };
27};
28Object.defineProperty(exports, "__esModule", { value: true });
29exports.setDependenciesTsConfigPaths = exports.initializeTsConfig = exports.defaultTsConfigPath = void 0;
30const path = __importStar(require("path"));
31const typescript_1 = __importDefault(require("typescript"));
32const load_esm_1 = require("../utils/load-esm");
33const log = __importStar(require("../utils/log"));
34exports.defaultTsConfigPath = path.join(__dirname, 'conf', 'tsconfig.ngc.json');
35/**
36 * Reads the default TypeScript configuration.
37 */
38async function readDefaultTsConfig(fileName = exports.defaultTsConfigPath) {
39 // these options are mandatory
40 const extraOptions = {
41 target: typescript_1.default.ScriptTarget.ES2022,
42 // sourcemaps
43 sourceMap: false,
44 inlineSources: true,
45 inlineSourceMap: true,
46 outDir: '',
47 declaration: true,
48 // ng compiler
49 enableResourceInlining: true,
50 // these are required to set the appropriate EmitFlags
51 flatModuleId: 'AUTOGENERATED',
52 flatModuleOutFile: 'AUTOGENERATED',
53 };
54 const { readConfiguration } = await (0, load_esm_1.ngCompilerCli)();
55 return readConfiguration(fileName, extraOptions);
56}
57/**
58 * Creates a parsed TypeScript configuration object.
59 *
60 * @param values File path or parsed configuration.
61 */
62async function createDefaultTsConfig(values) {
63 if (!values) {
64 return readDefaultTsConfig();
65 }
66 else if (typeof values === 'string') {
67 return readDefaultTsConfig(values);
68 }
69 else {
70 return values;
71 }
72}
73/**
74 * Initializes TypeScript Compiler options and Angular Compiler options by overriding the
75 * default config with entry point-specific values.
76 */
77async function initializeTsConfig(defaultTsConfig, entryPoints) {
78 const defaultTsConfigParsed = await createDefaultTsConfig(defaultTsConfig);
79 if (defaultTsConfigParsed.errors.length > 0) {
80 const { formatDiagnostics } = await (0, load_esm_1.ngCompilerCli)();
81 throw new Error(formatDiagnostics(defaultTsConfigParsed.errors));
82 }
83 entryPoints.forEach(currentEntryPoint => {
84 const { entryPoint } = currentEntryPoint.data;
85 log.debug(`Initializing tsconfig for ${entryPoint.moduleId}`);
86 const basePath = path.dirname(entryPoint.entryFilePath);
87 // Resolve defaults from DI token and create a deep copy of the defaults
88 const tsConfig = JSON.parse(JSON.stringify(defaultTsConfigParsed));
89 const overrideOptions = {
90 flatModuleId: entryPoint.moduleId,
91 flatModuleOutFile: `${entryPoint.flatModuleFile}.js`,
92 basePath,
93 rootDir: basePath,
94 sourceRoot: '',
95 };
96 tsConfig.rootNames = [entryPoint.entryFilePath];
97 tsConfig.options = { ...tsConfig.options, ...overrideOptions };
98 currentEntryPoint.data.tsConfig = tsConfig;
99 });
100}
101exports.initializeTsConfig = initializeTsConfig;
102/**
103 * Set the paths for entrypoint dependencies.
104 *
105 * This doesn't mutate the object.
106 *
107 * @param parsedTsConfig - A parsed tsconfig
108 * @param entryPoints - A list of entryPoints
109 * @param pointToSource Point the path mapping to either the source code or emitted declarations.
110 * Typically for analysis one should point to the source files while for a compilation once should use the emitted declarations
111 */
112function setDependenciesTsConfigPaths(parsedTsConfig, entryPoints, pointToSource = false) {
113 const tsConfig = JSON.parse(JSON.stringify(parsedTsConfig));
114 // Add paths mappings for dependencies
115 if (!tsConfig.options.paths) {
116 tsConfig.options.paths = {};
117 }
118 for (const dep of entryPoints) {
119 const { entryPoint } = dep.data;
120 const { moduleId, destinationFiles, entryFilePath } = entryPoint;
121 const mappedPath = [pointToSource ? entryFilePath : destinationFiles.declarations];
122 if (!tsConfig.options.paths[moduleId]) {
123 tsConfig.options.paths[moduleId] = mappedPath;
124 }
125 else {
126 tsConfig.options.paths[moduleId].unshift(...mappedPath);
127 }
128 }
129 return tsConfig;
130}
131exports.setDependenciesTsConfigPaths = setDependenciesTsConfigPaths;
132//# sourceMappingURL=tsconfig.js.map
\No newline at end of file