UNPKG

5.67 kBJavaScriptView Raw
1"use strict";
2var __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};
13var __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};
22Object.defineProperty(exports, "__esModule", { value: true });
23exports.patchCreateProgram = exports.addDiagnosticFactory = exports.transformerErrors = void 0;
24var path_1 = require("path");
25var PluginCreator_1 = require("./PluginCreator");
26exports.transformerErrors = new WeakMap();
27function 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}
34exports.addDiagnosticFactory = addDiagnosticFactory;
35function 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 * The emit method has the following declaration:
72 * https://github.com/microsoft/TypeScript/blob/bfc55b5762443c37ecdef08a3b5a4e057b4d1e85/src/compiler/builderPublic.ts#L101
73 * The declaration specifies 5 arguments, but it's not true. Sometimes the emit method takes 6 arguments.
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 // todo: doesn't work with 3.7
79 // result.diagnostics = [...result.diagnostics, ...transformerErrors.get(program)!];
80 return result;
81 };
82 return program;
83 }
84 tsm.createProgram = createProgram;
85 return tsm;
86}
87exports.patchCreateProgram = patchCreateProgram;
88function 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}
107function 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}
114function preparePluginsFromCompilerOptions(plugins) {
115 if (!plugins)
116 return [];
117 // old transformers system
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}