1 | "use strict";
|
2 | var __assign = (this && this.__assign) || function () {
|
3 | __assign = Object.assign || function(t) {
|
4 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
5 | s = arguments[i];
|
6 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
7 | t[p] = s[p];
|
8 | }
|
9 | return t;
|
10 | };
|
11 | return __assign.apply(this, arguments);
|
12 | };
|
13 | var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
14 | if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
15 | if (ar || !(i in from)) {
|
16 | if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
17 | ar[i] = from[i];
|
18 | }
|
19 | }
|
20 | return to.concat(ar || Array.prototype.slice.call(from));
|
21 | };
|
22 | Object.defineProperty(exports, "__esModule", { value: true });
|
23 | exports.patchCreateProgram = exports.addDiagnosticFactory = exports.transformerErrors = void 0;
|
24 | var path_1 = require("path");
|
25 | var PluginCreator_1 = require("./PluginCreator");
|
26 | exports.transformerErrors = new WeakMap();
|
27 | function addDiagnosticFactory(program) {
|
28 | return function (diag) {
|
29 | var arr = exports.transformerErrors.get(program) || [];
|
30 | arr.push(diag);
|
31 | exports.transformerErrors.set(program, arr);
|
32 | };
|
33 | }
|
34 | exports.addDiagnosticFactory = addDiagnosticFactory;
|
35 | function patchCreateProgram(tsm, forceReadConfig, projectDir) {
|
36 | if (forceReadConfig === void 0) { forceReadConfig = false; }
|
37 | if (projectDir === void 0) { projectDir = process.cwd(); }
|
38 | var originCreateProgram = tsm.createProgram;
|
39 | function createProgram(rootNamesOrOptions, options, host, oldProgram, configFileParsingDiagnostics) {
|
40 | var rootNames;
|
41 | var createOpts;
|
42 | if (!Array.isArray(rootNamesOrOptions)) {
|
43 | createOpts = rootNamesOrOptions;
|
44 | }
|
45 | if (createOpts) {
|
46 | rootNames = createOpts.rootNames;
|
47 | options = createOpts.options;
|
48 | host = createOpts.host;
|
49 | oldProgram = createOpts.oldProgram;
|
50 | configFileParsingDiagnostics = createOpts.configFileParsingDiagnostics;
|
51 | }
|
52 | else {
|
53 | options = options;
|
54 | rootNames = rootNamesOrOptions;
|
55 | }
|
56 | if (forceReadConfig) {
|
57 | var info = getConfig(tsm, options, rootNames, projectDir);
|
58 | options = info.compilerOptions;
|
59 | if (createOpts) {
|
60 | createOpts.options = options;
|
61 | }
|
62 | projectDir = info.projectDir;
|
63 | }
|
64 | var program = createOpts
|
65 | ? originCreateProgram(createOpts)
|
66 | : originCreateProgram(rootNames, options, host, oldProgram, configFileParsingDiagnostics);
|
67 | var plugins = preparePluginsFromCompilerOptions(options.plugins);
|
68 | var pluginCreator = new PluginCreator_1.PluginCreator(tsm, plugins, projectDir);
|
69 | var originEmit = program.emit;
|
70 | |
71 |
|
72 |
|
73 |
|
74 |
|
75 | program.emit = function newEmit(targetSourceFile, writeFile, cancellationToken, emitOnlyDtsFiles, customTransformers, arg) {
|
76 | var mergedTransformers = pluginCreator.createTransformers({ program: program }, customTransformers);
|
77 | var result = originEmit(targetSourceFile, writeFile, cancellationToken, emitOnlyDtsFiles, mergedTransformers, arg);
|
78 |
|
79 |
|
80 | return result;
|
81 | };
|
82 | return program;
|
83 | }
|
84 | tsm.createProgram = createProgram;
|
85 | return tsm;
|
86 | }
|
87 | exports.patchCreateProgram = patchCreateProgram;
|
88 | function getConfig(tsm, compilerOptions, rootFileNames, defaultDir) {
|
89 | if (compilerOptions.configFilePath === undefined) {
|
90 | var dir = rootFileNames.length > 0 ? (0, path_1.dirname)(rootFileNames[0]) : defaultDir;
|
91 | var tsconfigPath = tsm.findConfigFile(dir, tsm.sys.fileExists);
|
92 | if (tsconfigPath) {
|
93 | var projectDir = (0, path_1.dirname)(tsconfigPath);
|
94 | var config = readConfig(tsm, tsconfigPath, (0, path_1.dirname)(tsconfigPath));
|
95 | compilerOptions = __assign(__assign({}, config.options), compilerOptions);
|
96 | return {
|
97 | projectDir: projectDir,
|
98 | compilerOptions: compilerOptions,
|
99 | };
|
100 | }
|
101 | }
|
102 | return {
|
103 | projectDir: (0, path_1.dirname)(compilerOptions.configFilePath),
|
104 | compilerOptions: compilerOptions,
|
105 | };
|
106 | }
|
107 | function readConfig(tsm, configFileNamePath, projectDir) {
|
108 | var result = tsm.readConfigFile(configFileNamePath, tsm.sys.readFile);
|
109 | if (result.error) {
|
110 | throw new Error('tsconfig.json error: ' + result.error.messageText);
|
111 | }
|
112 | return tsm.parseJsonConfigFileContent(result.config, tsm.sys, projectDir, undefined, configFileNamePath);
|
113 | }
|
114 | function preparePluginsFromCompilerOptions(plugins) {
|
115 | if (!plugins)
|
116 | return [];
|
117 |
|
118 | if (plugins.length === 1 && plugins[0].customTransformers) {
|
119 | var _a = plugins[0].customTransformers, _b = _a.before, before = _b === void 0 ? [] : _b, _c = _a.after, after = _c === void 0 ? [] : _c;
|
120 | return __spreadArray(__spreadArray([], before.map(function (item) { return ({ transform: item }); }), true), after.map(function (item) { return ({ transform: item, after: true }); }), true);
|
121 | }
|
122 | return plugins;
|
123 | }
|