UNPKG

5.7 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.compileTypeScriptWatcher = exports.compileTypeScript = void 0;
4const tslib_1 = require("tslib");
5const devkit_1 = require("@nrwl/devkit");
6const fs_extra_1 = require("fs-extra");
7const ts = require("typescript");
8const typescript_1 = require("../typescript");
9function compileTypeScript(options) {
10 const normalizedOptions = normalizeOptions(options);
11 const tsConfig = getNormalizedTsConfig(normalizedOptions);
12 if (normalizedOptions.deleteOutputPath) {
13 (0, fs_extra_1.removeSync)(normalizedOptions.outputPath);
14 }
15 return createProgram(tsConfig, normalizedOptions);
16}
17exports.compileTypeScript = compileTypeScript;
18function compileTypeScriptWatcher(options, callback) {
19 const normalizedOptions = normalizeOptions(options);
20 const tsConfig = getNormalizedTsConfig(normalizedOptions);
21 if (normalizedOptions.deleteOutputPath) {
22 (0, fs_extra_1.removeSync)(normalizedOptions.outputPath);
23 }
24 const host = ts.createWatchCompilerHost(tsConfig.fileNames, tsConfig.options, ts.sys);
25 const originalAfterProgramCreate = host.afterProgramCreate;
26 host.afterProgramCreate = (builderProgram) => {
27 const originalProgramEmit = builderProgram.emit;
28 builderProgram.emit = (targetSourceFile, writeFile, cancellationToken, emitOnlyDtsFiles, customTransformers) => {
29 var _a;
30 const consumerCustomTransformers = (_a = options.getCustomTransformers) === null || _a === void 0 ? void 0 : _a.call(options, builderProgram.getProgram());
31 const mergedCustomTransformers = mergeCustomTransformers(customTransformers, consumerCustomTransformers);
32 return originalProgramEmit(targetSourceFile, writeFile, cancellationToken, emitOnlyDtsFiles, mergedCustomTransformers);
33 };
34 if (originalAfterProgramCreate)
35 originalAfterProgramCreate(builderProgram);
36 };
37 const originalOnWatchStatusChange = host.onWatchStatusChange;
38 host.onWatchStatusChange = (a, b, c, d) => tslib_1.__awaiter(this, void 0, void 0, function* () {
39 originalOnWatchStatusChange === null || originalOnWatchStatusChange === void 0 ? void 0 : originalOnWatchStatusChange(a, b, c, d);
40 yield (callback === null || callback === void 0 ? void 0 : callback(a, b, c, d));
41 });
42 return ts.createWatchProgram(host);
43}
44exports.compileTypeScriptWatcher = compileTypeScriptWatcher;
45function mergeCustomTransformers(originalCustomTransformers, consumerCustomTransformers) {
46 if (!consumerCustomTransformers)
47 return originalCustomTransformers;
48 const mergedCustomTransformers = {};
49 if (consumerCustomTransformers.before) {
50 mergedCustomTransformers.before = (originalCustomTransformers === null || originalCustomTransformers === void 0 ? void 0 : originalCustomTransformers.before)
51 ? [
52 ...originalCustomTransformers.before,
53 ...consumerCustomTransformers.before,
54 ]
55 : consumerCustomTransformers.before;
56 }
57 if (consumerCustomTransformers.after) {
58 mergedCustomTransformers.after = (originalCustomTransformers === null || originalCustomTransformers === void 0 ? void 0 : originalCustomTransformers.after)
59 ? [
60 ...originalCustomTransformers.after,
61 ...consumerCustomTransformers.after,
62 ]
63 : consumerCustomTransformers.after;
64 }
65 if (consumerCustomTransformers.afterDeclarations) {
66 mergedCustomTransformers.afterDeclarations =
67 (originalCustomTransformers === null || originalCustomTransformers === void 0 ? void 0 : originalCustomTransformers.afterDeclarations)
68 ? [
69 ...originalCustomTransformers.afterDeclarations,
70 ...consumerCustomTransformers.afterDeclarations,
71 ]
72 : consumerCustomTransformers.afterDeclarations;
73 }
74 return mergedCustomTransformers;
75}
76function getNormalizedTsConfig(options) {
77 const tsConfig = (0, typescript_1.readTsConfig)(options.tsConfig);
78 tsConfig.options.outDir = options.outputPath;
79 tsConfig.options.noEmitOnError = true;
80 tsConfig.options.rootDir = options.rootDir;
81 return tsConfig;
82}
83function createProgram(tsconfig, { projectName, getCustomTransformers }) {
84 const host = ts.createCompilerHost(tsconfig.options);
85 const program = ts.createProgram({
86 rootNames: tsconfig.fileNames,
87 options: tsconfig.options,
88 host,
89 });
90 devkit_1.logger.info(`Compiling TypeScript files for project "${projectName}"...`);
91 const results = program.emit(undefined, undefined, undefined, undefined, getCustomTransformers === null || getCustomTransformers === void 0 ? void 0 : getCustomTransformers(program));
92 if (results.emitSkipped) {
93 const diagnostics = ts.formatDiagnosticsWithColorAndContext(results.diagnostics, {
94 getCurrentDirectory: () => ts.sys.getCurrentDirectory(),
95 getNewLine: () => ts.sys.newLine,
96 getCanonicalFileName: (name) => name,
97 });
98 devkit_1.logger.error(diagnostics);
99 return { success: false };
100 }
101 else {
102 devkit_1.logger.info(`Done compiling TypeScript files for project "${projectName}".`);
103 return { success: true };
104 }
105}
106function normalizeOptions(options) {
107 var _a, _b;
108 return Object.assign(Object.assign({}, options), { deleteOutputPath: (_a = options.deleteOutputPath) !== null && _a !== void 0 ? _a : true, rootDir: (_b = options.rootDir) !== null && _b !== void 0 ? _b : options.projectRoot });
109}
110//# sourceMappingURL=compilation.js.map
\No newline at end of file