@samchon/ts-patch 3.2.2-dev.20241204
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +268 -0
- package/LICENSE.md +13 -0
- package/README.md +348 -0
- package/actions/check.d.ts +11 -0
- package/actions/check.js +45 -0
- package/actions/check.js.map +1 -0
- package/actions/index.d.ts +5 -0
- package/actions/index.js +22 -0
- package/actions/index.js.map +1 -0
- package/actions/install.d.ts +5 -0
- package/actions/install.js +25 -0
- package/actions/install.js.map +1 -0
- package/actions/patch.d.ts +6 -0
- package/actions/patch.js +82 -0
- package/actions/patch.js.map +1 -0
- package/actions/uninstall.d.ts +5 -0
- package/actions/uninstall.js +26 -0
- package/actions/uninstall.js.map +1 -0
- package/actions/unpatch.d.ts +3 -0
- package/actions/unpatch.js +82 -0
- package/actions/unpatch.js.map +1 -0
- package/bin/ts-patch.d.ts +1 -0
- package/bin/ts-patch.js +33 -0
- package/bin/ts-patch.js.map +1 -0
- package/bin/tspc.d.ts +0 -0
- package/bin/tspc.js +14 -0
- package/bin/tspc.js.map +1 -0
- package/cli/cli.d.ts +14 -0
- package/cli/cli.js +96 -0
- package/cli/cli.js.map +1 -0
- package/cli/commands.d.ts +5 -0
- package/cli/commands.js +49 -0
- package/cli/commands.js.map +1 -0
- package/cli/help-menu.d.ts +1 -0
- package/cli/help-menu.js +43 -0
- package/cli/help-menu.js.map +1 -0
- package/cli/options.d.ts +14 -0
- package/cli/options.js +63 -0
- package/cli/options.js.map +1 -0
- package/compiler/package.json +4 -0
- package/compiler/tsc.js +19 -0
- package/compiler/tsserver.js +19 -0
- package/compiler/tsserverlibrary.js +19 -0
- package/compiler/typescript.js +19 -0
- package/config.d.ts +18 -0
- package/config.js +50 -0
- package/config.js.map +1 -0
- package/index.d.ts +4 -0
- package/index.js +29 -0
- package/index.js.map +1 -0
- package/module/get-live-module.d.ts +5 -0
- package/module/get-live-module.js +25 -0
- package/module/get-live-module.js.map +1 -0
- package/module/index.d.ts +4 -0
- package/module/index.js +21 -0
- package/module/index.js.map +1 -0
- package/module/module-file.d.ts +9 -0
- package/module/module-file.js +97 -0
- package/module/module-file.js.map +1 -0
- package/module/module-source.d.ts +15 -0
- package/module/module-source.js +35 -0
- package/module/module-source.js.map +1 -0
- package/module/source-section.d.ts +20 -0
- package/module/source-section.js +73 -0
- package/module/source-section.js.map +1 -0
- package/module/ts-module.d.ts +35 -0
- package/module/ts-module.js +100 -0
- package/module/ts-module.js.map +1 -0
- package/options.d.ts +19 -0
- package/options.js +32 -0
- package/options.js.map +1 -0
- package/package.json +107 -0
- package/patch/get-patched-source.d.ts +12 -0
- package/patch/get-patched-source.js +69 -0
- package/patch/get-patched-source.js.map +1 -0
- package/patch/patch-detail.d.ts +36 -0
- package/patch/patch-detail.js +67 -0
- package/patch/patch-detail.js.map +1 -0
- package/patch/patch-module.d.ts +5 -0
- package/patch/patch-module.js +182 -0
- package/patch/patch-module.js.map +1 -0
- package/patch/transformers/add-original-create-program.d.ts +3 -0
- package/patch/transformers/add-original-create-program.js +71 -0
- package/patch/transformers/add-original-create-program.js.map +1 -0
- package/patch/transformers/fix-ts-early-return.d.ts +2 -0
- package/patch/transformers/fix-ts-early-return.js +51 -0
- package/patch/transformers/fix-ts-early-return.js.map +1 -0
- package/patch/transformers/hook-tsc-exec.d.ts +2 -0
- package/patch/transformers/hook-tsc-exec.js +32 -0
- package/patch/transformers/hook-tsc-exec.js.map +1 -0
- package/patch/transformers/index.d.ts +6 -0
- package/patch/transformers/index.js +23 -0
- package/patch/transformers/index.js.map +1 -0
- package/patch/transformers/merge-statements.d.ts +2 -0
- package/patch/transformers/merge-statements.js +60 -0
- package/patch/transformers/merge-statements.js.map +1 -0
- package/patch/transformers/patch-create-program.d.ts +2 -0
- package/patch/transformers/patch-create-program.js +36 -0
- package/patch/transformers/patch-create-program.js.map +1 -0
- package/patch/transformers/patch-emitter.d.ts +2 -0
- package/patch/transformers/patch-emitter.js +44 -0
- package/patch/transformers/patch-emitter.js.map +1 -0
- package/plugin-types.d.ts +95 -0
- package/plugin-types.js +4 -0
- package/plugin-types.js.map +1 -0
- package/resources/module-patch.d.ts +50 -0
- package/resources/module-patch.js +609 -0
- package/slice/module-slice.d.ts +19 -0
- package/slice/module-slice.js +37 -0
- package/slice/module-slice.js.map +1 -0
- package/slice/ts54.d.ts +6 -0
- package/slice/ts54.js +70 -0
- package/slice/ts54.js.map +1 -0
- package/slice/ts55.d.ts +6 -0
- package/slice/ts55.js +70 -0
- package/slice/ts55.js.map +1 -0
- package/slice/ts552.d.ts +6 -0
- package/slice/ts552.js +70 -0
- package/slice/ts552.js.map +1 -0
- package/system/cache.d.ts +3 -0
- package/system/cache.js +69 -0
- package/system/cache.js.map +1 -0
- package/system/errors.d.ts +35 -0
- package/system/errors.js +82 -0
- package/system/errors.js.map +1 -0
- package/system/index.d.ts +4 -0
- package/system/index.js +21 -0
- package/system/index.js.map +1 -0
- package/system/logger.d.ts +7 -0
- package/system/logger.js +48 -0
- package/system/logger.js.map +1 -0
- package/system/types.d.ts +4 -0
- package/system/types.js +7 -0
- package/system/types.js.map +1 -0
- package/ts-package.d.ts +18 -0
- package/ts-package.js +73 -0
- package/ts-package.js.map +1 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/utils/file-utils.d.ts +9 -0
- package/utils/file-utils.js +98 -0
- package/utils/file-utils.js.map +1 -0
- package/utils/find-cache-dir.d.ts +23 -0
- package/utils/find-cache-dir.js +93 -0
- package/utils/find-cache-dir.js.map +1 -0
- package/utils/general.d.ts +5 -0
- package/utils/general.js +16 -0
- package/utils/general.js.map +1 -0
- package/utils/index.d.ts +3 -0
- package/utils/index.js +20 -0
- package/utils/index.js.map +1 -0
|
@@ -0,0 +1,609 @@
|
|
|
1
|
+
var tsp = (function () {
|
|
2
|
+
"use strict";
|
|
3
|
+
var tsp;
|
|
4
|
+
(function (tsp) {
|
|
5
|
+
const os = require("os");
|
|
6
|
+
const path = require("path");
|
|
7
|
+
const fs = require("fs");
|
|
8
|
+
tsp.diagnosticMap = new WeakMap();
|
|
9
|
+
tsp.supportedExtensions = [".ts", ".mts", ".cts", ".js", ".mjs", ".cjs"];
|
|
10
|
+
tsp.tsExtensions = [".ts", ".mts", ".cts"];
|
|
11
|
+
function diagnosticExtrasFactory(program) {
|
|
12
|
+
const diagnostics = tsp.diagnosticMap.get(program) || tsp.diagnosticMap.set(program, []).get(program);
|
|
13
|
+
const addDiagnostic = (diag) => diagnostics.push(diag);
|
|
14
|
+
const removeDiagnostic = (index) => { diagnostics.splice(index, 1); };
|
|
15
|
+
return { addDiagnostic, removeDiagnostic, diagnostics };
|
|
16
|
+
}
|
|
17
|
+
tsp.diagnosticExtrasFactory = diagnosticExtrasFactory;
|
|
18
|
+
function getTmpDir(subPath) {
|
|
19
|
+
const tmpDir = path.resolve(os.tmpdir(), "tsp", subPath);
|
|
20
|
+
if (!fs.existsSync(tmpDir))
|
|
21
|
+
fs.mkdirSync(tmpDir, { recursive: true });
|
|
22
|
+
return tmpDir;
|
|
23
|
+
}
|
|
24
|
+
tsp.getTmpDir = getTmpDir;
|
|
25
|
+
function getTsInstance() {
|
|
26
|
+
return (typeof ts !== "undefined" ? ts : module.exports);
|
|
27
|
+
}
|
|
28
|
+
tsp.getTsInstance = getTsInstance;
|
|
29
|
+
class TsPatchError extends Error {
|
|
30
|
+
constructor(message, diagnostic) {
|
|
31
|
+
super(message);
|
|
32
|
+
this.diagnostic = diagnostic;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
tsp.TsPatchError = TsPatchError;
|
|
36
|
+
})(tsp || (tsp = {}));
|
|
37
|
+
var tsp;
|
|
38
|
+
(function (tsp) {
|
|
39
|
+
const Module = require("module");
|
|
40
|
+
const path = require("path");
|
|
41
|
+
const fs = require("fs");
|
|
42
|
+
const crypto = require("crypto");
|
|
43
|
+
function getEsmLibrary() {
|
|
44
|
+
try {
|
|
45
|
+
return require("esm");
|
|
46
|
+
}
|
|
47
|
+
catch (e) {
|
|
48
|
+
if (e.code === "MODULE_NOT_FOUND")
|
|
49
|
+
throw new tsp.TsPatchError(`Plugin is an ESM module. To enable experimental ESM support, ` +
|
|
50
|
+
`install the 'esm' package as a (dev)-dependency or global.`);
|
|
51
|
+
else
|
|
52
|
+
throw e;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function registerEsmIntercept(registerConfig) {
|
|
56
|
+
const originalRequire = Module.prototype.require;
|
|
57
|
+
const builtFiles = new Map();
|
|
58
|
+
const getHash = () => {
|
|
59
|
+
let hash;
|
|
60
|
+
do {
|
|
61
|
+
hash = crypto.randomBytes(16).toString("hex");
|
|
62
|
+
} while (builtFiles.has(hash));
|
|
63
|
+
return hash;
|
|
64
|
+
};
|
|
65
|
+
const cleanup = () => {
|
|
66
|
+
for (const { 1: filePath } of builtFiles) {
|
|
67
|
+
delete require.cache[filePath];
|
|
68
|
+
try {
|
|
69
|
+
fs.rmSync(filePath, { force: true, maxRetries: 3 });
|
|
70
|
+
}
|
|
71
|
+
catch (e) {
|
|
72
|
+
if (process.env.NODE_ENV !== "production")
|
|
73
|
+
console.warn(`[ts-patch] Warning: Failed to delete temporary esm cache file: ${filePath}.`);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
builtFiles.clear();
|
|
77
|
+
Module.prototype.require = originalRequire;
|
|
78
|
+
};
|
|
79
|
+
try {
|
|
80
|
+
Module.prototype.require = wrappedRequire;
|
|
81
|
+
}
|
|
82
|
+
catch (e) {
|
|
83
|
+
cleanup();
|
|
84
|
+
}
|
|
85
|
+
function wrappedRequire(request) {
|
|
86
|
+
try {
|
|
87
|
+
return originalRequire.apply(this, arguments);
|
|
88
|
+
}
|
|
89
|
+
catch (e) {
|
|
90
|
+
if (e.code === "ERR_REQUIRE_ESM") {
|
|
91
|
+
const resolvedPath = Module._resolveFilename(request, this, false);
|
|
92
|
+
const resolvedPathExt = path.extname(resolvedPath);
|
|
93
|
+
if (Module._cache[resolvedPath])
|
|
94
|
+
return Module._cache[resolvedPath].exports;
|
|
95
|
+
let targetFilePath;
|
|
96
|
+
if (tsp.tsExtensions.includes(resolvedPathExt)) {
|
|
97
|
+
if (!builtFiles.has(resolvedPath)) {
|
|
98
|
+
const tsCode = fs.readFileSync(resolvedPath, "utf8");
|
|
99
|
+
const newPath = resolvedPath.replace(/\.ts$/, ".mts");
|
|
100
|
+
const jsCode = registerConfig.tsNodeInstance.compile(tsCode, newPath);
|
|
101
|
+
const outputFileName = getHash() + ".mjs";
|
|
102
|
+
const outputFilePath = path.join(tsp.getTmpDir("esm"), outputFileName);
|
|
103
|
+
fs.writeFileSync(outputFilePath, jsCode, "utf8");
|
|
104
|
+
builtFiles.set(resolvedPath, outputFilePath);
|
|
105
|
+
targetFilePath = outputFilePath;
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
targetFilePath = builtFiles.get(resolvedPath);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
targetFilePath = resolvedPath;
|
|
113
|
+
}
|
|
114
|
+
const newModule = new Module(request, this);
|
|
115
|
+
newModule.filename = resolvedPath;
|
|
116
|
+
newModule.paths = Module._nodeModulePaths(resolvedPath);
|
|
117
|
+
Module._cache[resolvedPath] = newModule;
|
|
118
|
+
const res = getEsmLibrary()(newModule)(targetFilePath);
|
|
119
|
+
newModule.filename = resolvedPath;
|
|
120
|
+
return res;
|
|
121
|
+
}
|
|
122
|
+
throw e;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return cleanup;
|
|
126
|
+
}
|
|
127
|
+
tsp.registerEsmIntercept = registerEsmIntercept;
|
|
128
|
+
})(tsp || (tsp = {}));
|
|
129
|
+
var tsp;
|
|
130
|
+
(function (tsp) {
|
|
131
|
+
const crypto = require("crypto");
|
|
132
|
+
function createTransformersFromPattern(opt) {
|
|
133
|
+
const { factory, config, program, ls, registerConfig } = opt;
|
|
134
|
+
const { transform, after, afterDeclarations, name, type, transformProgram, ...cleanConfig } = config;
|
|
135
|
+
if (!transform)
|
|
136
|
+
throw new tsp.TsPatchError("Not a valid config entry: \"transform\" key not found");
|
|
137
|
+
const transformerKind = after ? "after" :
|
|
138
|
+
afterDeclarations ? "afterDeclarations" :
|
|
139
|
+
"before";
|
|
140
|
+
let pluginFactoryResult;
|
|
141
|
+
switch (config.type) {
|
|
142
|
+
case "ls":
|
|
143
|
+
if (!ls)
|
|
144
|
+
throw new tsp.TsPatchError(`Plugin ${transform} needs a LanguageService`);
|
|
145
|
+
pluginFactoryResult = factory(ls, cleanConfig);
|
|
146
|
+
break;
|
|
147
|
+
case "config":
|
|
148
|
+
pluginFactoryResult = factory(cleanConfig);
|
|
149
|
+
break;
|
|
150
|
+
case "compilerOptions":
|
|
151
|
+
pluginFactoryResult = factory(program.getCompilerOptions(), cleanConfig);
|
|
152
|
+
break;
|
|
153
|
+
case "checker":
|
|
154
|
+
pluginFactoryResult = factory(program.getTypeChecker(), cleanConfig);
|
|
155
|
+
break;
|
|
156
|
+
case undefined:
|
|
157
|
+
case "program":
|
|
158
|
+
const { addDiagnostic, removeDiagnostic, diagnostics } = tsp.diagnosticExtrasFactory(program);
|
|
159
|
+
pluginFactoryResult = factory(program, cleanConfig, {
|
|
160
|
+
ts: tsp.getTsInstance(),
|
|
161
|
+
addDiagnostic,
|
|
162
|
+
removeDiagnostic,
|
|
163
|
+
diagnostics,
|
|
164
|
+
library: tsp.currentLibrary
|
|
165
|
+
});
|
|
166
|
+
break;
|
|
167
|
+
case "raw":
|
|
168
|
+
pluginFactoryResult = (ctx) => factory(ctx, program, cleanConfig);
|
|
169
|
+
break;
|
|
170
|
+
default:
|
|
171
|
+
throw new tsp.TsPatchError(`Invalid plugin type found in tsconfig.json: '${config.type}'`);
|
|
172
|
+
}
|
|
173
|
+
let transformerFactories;
|
|
174
|
+
switch (typeof pluginFactoryResult) {
|
|
175
|
+
case "function":
|
|
176
|
+
transformerFactories = [pluginFactoryResult];
|
|
177
|
+
break;
|
|
178
|
+
case "object":
|
|
179
|
+
const factoryOrFactories = pluginFactoryResult[transformerKind];
|
|
180
|
+
if (typeof factoryOrFactories === "function") {
|
|
181
|
+
transformerFactories = [pluginFactoryResult[transformerKind]];
|
|
182
|
+
break;
|
|
183
|
+
}
|
|
184
|
+
else if (Array.isArray(factoryOrFactories)) {
|
|
185
|
+
transformerFactories = [...factoryOrFactories];
|
|
186
|
+
break;
|
|
187
|
+
}
|
|
188
|
+
default:
|
|
189
|
+
throw new tsp.TsPatchError(`Invalid plugin result: expected a function or an object with a '${transformerKind}' property`);
|
|
190
|
+
}
|
|
191
|
+
const wrappedFactories = [];
|
|
192
|
+
for (const transformerFactory of transformerFactories) {
|
|
193
|
+
if (!transformerFactory || typeof transformerFactory !== "function")
|
|
194
|
+
throw new tsp.TsPatchError(`Invalid plugin entry point! Expected a transformer factory function or an object with a '${transformerKind}' property`);
|
|
195
|
+
const wrapper = wrapTransformerFactory(transformerFactory, registerConfig, true);
|
|
196
|
+
wrappedFactories.push(wrapper);
|
|
197
|
+
}
|
|
198
|
+
const res = {
|
|
199
|
+
[transformerKind]: wrappedFactories
|
|
200
|
+
};
|
|
201
|
+
return res;
|
|
202
|
+
}
|
|
203
|
+
function wrapTransformerFactory(transformerFn, requireConfig, wrapInnerFunction) {
|
|
204
|
+
const wrapper = function tspWrappedFactory(...args) {
|
|
205
|
+
let res;
|
|
206
|
+
try {
|
|
207
|
+
tsp.registerPlugin(requireConfig);
|
|
208
|
+
if (!wrapInnerFunction) {
|
|
209
|
+
res = transformerFn(...args);
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
const resFn = transformerFn(...args);
|
|
213
|
+
if (typeof resFn !== "function")
|
|
214
|
+
throw new tsp.TsPatchError("Invalid plugin: expected a function");
|
|
215
|
+
res = wrapTransformerFactory(resFn, requireConfig, false);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
finally {
|
|
219
|
+
tsp.unregisterPlugin();
|
|
220
|
+
}
|
|
221
|
+
return res;
|
|
222
|
+
};
|
|
223
|
+
return wrapper;
|
|
224
|
+
}
|
|
225
|
+
class PluginCreator {
|
|
226
|
+
constructor(configs, options) {
|
|
227
|
+
this.plugins = [];
|
|
228
|
+
this.configs = configs;
|
|
229
|
+
this.options = options;
|
|
230
|
+
const { resolveBaseDir } = options;
|
|
231
|
+
this.plugins = configs
|
|
232
|
+
.filter(config => config.transform !== undefined)
|
|
233
|
+
.map(config => new tsp.TspPlugin(config, { resolveBaseDir }));
|
|
234
|
+
this.needsTscJsDocParsing = this.plugins.some(plugin => plugin.packageConfig?.tscOptions?.parseAllJsDoc === true);
|
|
235
|
+
}
|
|
236
|
+
mergeTransformers(into, source) {
|
|
237
|
+
const slice = (input) => (Array.isArray(input) ? input.slice() : [input]);
|
|
238
|
+
if (source.before)
|
|
239
|
+
into.before.push(...slice(source.before));
|
|
240
|
+
if (source.after)
|
|
241
|
+
into.after.push(...slice(source.after));
|
|
242
|
+
if (source.afterDeclarations)
|
|
243
|
+
into.afterDeclarations.push(...slice(source.afterDeclarations));
|
|
244
|
+
return this;
|
|
245
|
+
}
|
|
246
|
+
createSourceTransformers(params, customTransformers) {
|
|
247
|
+
const transformers = { before: [], after: [], afterDeclarations: [] };
|
|
248
|
+
const [ls, program] = ("ls" in params) ? [params.ls, params.ls.getProgram()] : [void 0, params.program];
|
|
249
|
+
for (const plugin of this.plugins) {
|
|
250
|
+
if (plugin.kind !== "SourceTransformer")
|
|
251
|
+
continue;
|
|
252
|
+
const { config } = plugin;
|
|
253
|
+
const createFactoryResult = plugin.createFactory();
|
|
254
|
+
if (!createFactoryResult)
|
|
255
|
+
continue;
|
|
256
|
+
const { factory, registerConfig } = createFactoryResult;
|
|
257
|
+
this.mergeTransformers(transformers, createTransformersFromPattern({
|
|
258
|
+
factory: factory,
|
|
259
|
+
registerConfig,
|
|
260
|
+
config,
|
|
261
|
+
program,
|
|
262
|
+
ls
|
|
263
|
+
}));
|
|
264
|
+
}
|
|
265
|
+
if (customTransformers)
|
|
266
|
+
this.mergeTransformers(transformers, customTransformers);
|
|
267
|
+
return transformers;
|
|
268
|
+
}
|
|
269
|
+
createProgramTransformers() {
|
|
270
|
+
const res = new Map();
|
|
271
|
+
for (const plugin of this.plugins) {
|
|
272
|
+
if (plugin.kind !== "ProgramTransformer")
|
|
273
|
+
continue;
|
|
274
|
+
const { config } = plugin;
|
|
275
|
+
const createFactoryResult = plugin.createFactory();
|
|
276
|
+
if (createFactoryResult === undefined)
|
|
277
|
+
continue;
|
|
278
|
+
const { registerConfig, factory: unwrappedFactory } = createFactoryResult;
|
|
279
|
+
const factory = wrapTransformerFactory(unwrappedFactory, registerConfig, false);
|
|
280
|
+
const transformerKey = crypto
|
|
281
|
+
.createHash("md5")
|
|
282
|
+
.update(JSON.stringify({ factory, config }))
|
|
283
|
+
.digest("hex");
|
|
284
|
+
res.set(transformerKey, [factory, config]);
|
|
285
|
+
}
|
|
286
|
+
return res;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
tsp.PluginCreator = PluginCreator;
|
|
290
|
+
})(tsp || (tsp = {}));
|
|
291
|
+
var tsp;
|
|
292
|
+
(function (tsp) {
|
|
293
|
+
const path = require("path");
|
|
294
|
+
const fs = require("fs");
|
|
295
|
+
const requireStack = [];
|
|
296
|
+
function getPackagePath(entryFilePath) {
|
|
297
|
+
let currentDir = path.dirname(entryFilePath);
|
|
298
|
+
const seenPaths = new Set();
|
|
299
|
+
while (currentDir !== path.parse(currentDir).root) {
|
|
300
|
+
if (seenPaths.has(currentDir))
|
|
301
|
+
return undefined;
|
|
302
|
+
seenPaths.add(currentDir);
|
|
303
|
+
const potentialPkgPath = path.join(currentDir, "package.json");
|
|
304
|
+
if (fs.existsSync(potentialPkgPath))
|
|
305
|
+
return potentialPkgPath;
|
|
306
|
+
currentDir = path.resolve(currentDir, "..");
|
|
307
|
+
}
|
|
308
|
+
return undefined;
|
|
309
|
+
}
|
|
310
|
+
class TspPlugin {
|
|
311
|
+
constructor(config, createOptions) {
|
|
312
|
+
this.config = { ...config };
|
|
313
|
+
this.validateConfig();
|
|
314
|
+
this._createOptions = createOptions;
|
|
315
|
+
this.importKey = config.import || "default";
|
|
316
|
+
this.kind = config.transformProgram === true ? "ProgramTransformer" : "SourceTransformer";
|
|
317
|
+
const { resolveBaseDir } = createOptions;
|
|
318
|
+
const configTransformValue = config.transform;
|
|
319
|
+
this.tsConfigPath = config.tsConfig && path.resolve(resolveBaseDir, config.tsConfig);
|
|
320
|
+
const entryFilePath = require.resolve(configTransformValue, { paths: [resolveBaseDir] });
|
|
321
|
+
this.entryFilePath = entryFilePath;
|
|
322
|
+
let pluginPackageConfig;
|
|
323
|
+
const modulePackagePath = getPackagePath(entryFilePath);
|
|
324
|
+
if (modulePackagePath) {
|
|
325
|
+
const modulePkgJsonContent = fs.readFileSync(modulePackagePath, "utf8");
|
|
326
|
+
const modulePkgJson = JSON.parse(modulePkgJsonContent);
|
|
327
|
+
pluginPackageConfig = modulePkgJson.tsp;
|
|
328
|
+
if (pluginPackageConfig === null || typeof pluginPackageConfig !== "object")
|
|
329
|
+
pluginPackageConfig = undefined;
|
|
330
|
+
}
|
|
331
|
+
this.packageConfig = pluginPackageConfig;
|
|
332
|
+
}
|
|
333
|
+
validateConfig() {
|
|
334
|
+
const { config } = this;
|
|
335
|
+
const configTransformValue = config.transform;
|
|
336
|
+
if (!configTransformValue)
|
|
337
|
+
throw new tsp.TsPatchError(`Invalid plugin config: missing "transform" value`);
|
|
338
|
+
if (config.resolvePathAliases && !config.tsConfig) {
|
|
339
|
+
console.warn(`[ts-patch] Warning: resolvePathAliases needs a tsConfig value pointing to a tsconfig.json for transformer" ${configTransformValue}.`);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
createFactory() {
|
|
343
|
+
const { entryFilePath, config, tsConfigPath, importKey } = this;
|
|
344
|
+
const configTransformValue = config.transform;
|
|
345
|
+
if (requireStack.includes(entryFilePath))
|
|
346
|
+
return;
|
|
347
|
+
requireStack.push(entryFilePath);
|
|
348
|
+
let isEsm = config.isEsm;
|
|
349
|
+
if (isEsm == null) {
|
|
350
|
+
const impliedModuleFormat = tsp.tsShim.getImpliedNodeFormatForFile(entryFilePath, undefined, tsp.tsShim.sys, { moduleResolution: tsp.tsShim.ModuleResolutionKind.Node16 });
|
|
351
|
+
isEsm = impliedModuleFormat === tsp.tsShim.ModuleKind.ESNext;
|
|
352
|
+
}
|
|
353
|
+
const isTs = configTransformValue.match(/\.[mc]?ts$/) != null;
|
|
354
|
+
const registerConfig = {
|
|
355
|
+
isTs,
|
|
356
|
+
isEsm,
|
|
357
|
+
tsConfig: tsConfigPath,
|
|
358
|
+
pluginConfig: config
|
|
359
|
+
};
|
|
360
|
+
tsp.registerPlugin(registerConfig);
|
|
361
|
+
try {
|
|
362
|
+
const commonjsModule = loadEntryFile();
|
|
363
|
+
const factoryModule = (typeof commonjsModule === "function") ? { default: commonjsModule } : commonjsModule;
|
|
364
|
+
const factory = factoryModule[importKey];
|
|
365
|
+
if (!factory)
|
|
366
|
+
throw new tsp.TsPatchError(`tsconfig.json > plugins: "${configTransformValue}" does not have an export "${importKey}": ` +
|
|
367
|
+
require("util").inspect(factoryModule));
|
|
368
|
+
if (typeof factory !== "function") {
|
|
369
|
+
throw new tsp.TsPatchError(`tsconfig.json > plugins: "${configTransformValue}" export "${importKey}" is not a plugin: ` +
|
|
370
|
+
require("util").inspect(factory));
|
|
371
|
+
}
|
|
372
|
+
return {
|
|
373
|
+
factory,
|
|
374
|
+
registerConfig: registerConfig
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
finally {
|
|
378
|
+
requireStack.pop();
|
|
379
|
+
tsp.unregisterPlugin();
|
|
380
|
+
}
|
|
381
|
+
function loadEntryFile() {
|
|
382
|
+
let res;
|
|
383
|
+
try {
|
|
384
|
+
res = require(entryFilePath);
|
|
385
|
+
}
|
|
386
|
+
catch (e) {
|
|
387
|
+
if (e.code === "ERR_REQUIRE_ESM") {
|
|
388
|
+
if (!registerConfig.isEsm) {
|
|
389
|
+
tsp.unregisterPlugin();
|
|
390
|
+
registerConfig.isEsm = true;
|
|
391
|
+
tsp.registerPlugin(registerConfig);
|
|
392
|
+
return loadEntryFile();
|
|
393
|
+
}
|
|
394
|
+
else {
|
|
395
|
+
throw new tsp.TsPatchError(`Cannot load ESM transformer "${configTransformValue}" from "${entryFilePath}". Please file a bug report`);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
else
|
|
399
|
+
throw e;
|
|
400
|
+
}
|
|
401
|
+
return res;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
tsp.TspPlugin = TspPlugin;
|
|
406
|
+
})(tsp || (tsp = {}));
|
|
407
|
+
var tsp;
|
|
408
|
+
(function (tsp) {
|
|
409
|
+
const path = require("path");
|
|
410
|
+
let configStack = [];
|
|
411
|
+
function getTsNode() {
|
|
412
|
+
try {
|
|
413
|
+
return require("ts-node");
|
|
414
|
+
}
|
|
415
|
+
catch (e) {
|
|
416
|
+
if (e.code === "MODULE_NOT_FOUND")
|
|
417
|
+
throw new tsp.TsPatchError(`Cannot use a typescript-based transformer without ts-node installed. ` +
|
|
418
|
+
`Add ts-node as a (dev)-dependency or install globally.`);
|
|
419
|
+
else
|
|
420
|
+
throw e;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
function getTsConfigPaths() {
|
|
424
|
+
try {
|
|
425
|
+
return require("tsconfig-paths");
|
|
426
|
+
}
|
|
427
|
+
catch (e) {
|
|
428
|
+
if (e.code === "MODULE_NOT_FOUND")
|
|
429
|
+
throw new tsp.TsPatchError(`resolvePathAliases requires the library: tsconfig-paths. ` +
|
|
430
|
+
`Add tsconfig-paths as a (dev)-dependency or install globally.`);
|
|
431
|
+
else
|
|
432
|
+
throw e;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
function getCompilerOptions(tsConfig) {
|
|
436
|
+
const configFile = tsp.tsShim.readConfigFile(tsConfig, tsp.tsShim.sys.readFile);
|
|
437
|
+
const parsedConfig = configFile && tsp.tsShim.parseJsonConfigFileContent(configFile.config, tsp.tsShim.sys, path.dirname(tsConfig));
|
|
438
|
+
return parsedConfig.options;
|
|
439
|
+
}
|
|
440
|
+
function unregisterPlugin() {
|
|
441
|
+
const activeRegisterConfig = configStack.pop();
|
|
442
|
+
if (activeRegisterConfig.tsConfigPathsCleanup) {
|
|
443
|
+
activeRegisterConfig.tsConfigPathsCleanup();
|
|
444
|
+
delete activeRegisterConfig.tsConfigPathsCleanup;
|
|
445
|
+
}
|
|
446
|
+
if (activeRegisterConfig.tsNodeInstance) {
|
|
447
|
+
activeRegisterConfig.tsNodeInstance.enabled(false);
|
|
448
|
+
}
|
|
449
|
+
if (activeRegisterConfig.esmInterceptCleanup) {
|
|
450
|
+
activeRegisterConfig.esmInterceptCleanup();
|
|
451
|
+
delete activeRegisterConfig.esmInterceptCleanup;
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
tsp.unregisterPlugin = unregisterPlugin;
|
|
455
|
+
function registerPlugin(registerConfig) {
|
|
456
|
+
if (!registerConfig)
|
|
457
|
+
throw new tsp.TsPatchError("requireConfig is required");
|
|
458
|
+
configStack.push(registerConfig);
|
|
459
|
+
const { isTs, isEsm, tsConfig, pluginConfig } = registerConfig;
|
|
460
|
+
if (isEsm) {
|
|
461
|
+
registerConfig.esmInterceptCleanup = tsp.registerEsmIntercept(registerConfig);
|
|
462
|
+
}
|
|
463
|
+
if (isTs) {
|
|
464
|
+
const tsNode = getTsNode();
|
|
465
|
+
let tsNodeInstance;
|
|
466
|
+
if (registerConfig.tsNodeInstance) {
|
|
467
|
+
tsNodeInstance = registerConfig.tsNodeInstance;
|
|
468
|
+
tsNode.register(tsNodeInstance);
|
|
469
|
+
}
|
|
470
|
+
else {
|
|
471
|
+
tsNodeInstance = tsNode.register({
|
|
472
|
+
transpileOnly: true,
|
|
473
|
+
...(tsConfig ? { project: tsConfig } : { skipProject: true }),
|
|
474
|
+
compilerOptions: {
|
|
475
|
+
target: isEsm ? "ESNext" : "ES2018",
|
|
476
|
+
jsx: "react",
|
|
477
|
+
esModuleInterop: true,
|
|
478
|
+
module: isEsm ? "ESNext" : "commonjs",
|
|
479
|
+
}
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
tsNodeInstance.enabled(true);
|
|
483
|
+
registerConfig.tsNodeInstance = tsNodeInstance;
|
|
484
|
+
}
|
|
485
|
+
if (tsConfig && pluginConfig.resolvePathAliases) {
|
|
486
|
+
registerConfig.compilerOptions ?? (registerConfig.compilerOptions = getCompilerOptions(tsConfig));
|
|
487
|
+
const { paths, baseUrl } = registerConfig.compilerOptions;
|
|
488
|
+
if (paths && baseUrl) {
|
|
489
|
+
registerConfig.tsConfigPathsCleanup = getTsConfigPaths().register({ baseUrl, paths });
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
tsp.registerPlugin = registerPlugin;
|
|
494
|
+
})(tsp || (tsp = {}));
|
|
495
|
+
var tsp;
|
|
496
|
+
(function (tsp) {
|
|
497
|
+
const activeProgramTransformers = new Set();
|
|
498
|
+
const { dirname } = require("path");
|
|
499
|
+
function getProjectDir(compilerOptions) {
|
|
500
|
+
return compilerOptions.configFilePath && dirname(compilerOptions.configFilePath);
|
|
501
|
+
}
|
|
502
|
+
function getProjectConfig(compilerOptions, rootFileNames) {
|
|
503
|
+
let configFilePath = compilerOptions.configFilePath;
|
|
504
|
+
let projectDir = getProjectDir(compilerOptions);
|
|
505
|
+
if (configFilePath === undefined) {
|
|
506
|
+
const baseDir = (rootFileNames.length > 0) ? dirname(rootFileNames[0]) : projectDir ?? process.cwd();
|
|
507
|
+
configFilePath = tsp.tsShim.findConfigFile(baseDir, tsp.tsShim.sys.fileExists);
|
|
508
|
+
if (configFilePath) {
|
|
509
|
+
const config = readConfig(configFilePath);
|
|
510
|
+
compilerOptions = { ...config.options, ...compilerOptions };
|
|
511
|
+
projectDir = getProjectDir(compilerOptions);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
return ({ projectDir, compilerOptions });
|
|
515
|
+
}
|
|
516
|
+
function readConfig(configFileNamePath) {
|
|
517
|
+
const projectDir = dirname(configFileNamePath);
|
|
518
|
+
const result = tsp.tsShim.readConfigFile(configFileNamePath, tsp.tsShim.sys.readFile);
|
|
519
|
+
if (result.error)
|
|
520
|
+
throw new tsp.TsPatchError("Error in tsconfig.json: " + result.error.messageText);
|
|
521
|
+
return tsp.tsShim.parseJsonConfigFileContent(result.config, tsp.tsShim.sys, projectDir, undefined, configFileNamePath);
|
|
522
|
+
}
|
|
523
|
+
function preparePluginsFromCompilerOptions(plugins) {
|
|
524
|
+
if (!plugins)
|
|
525
|
+
return [];
|
|
526
|
+
if ((plugins.length === 1) && plugins[0].customTransformers) {
|
|
527
|
+
const { before = [], after = [] } = plugins[0].customTransformers;
|
|
528
|
+
return [
|
|
529
|
+
...before.map((item) => ({ transform: item })),
|
|
530
|
+
...after.map((item) => ({ transform: item, after: true })),
|
|
531
|
+
];
|
|
532
|
+
}
|
|
533
|
+
return plugins;
|
|
534
|
+
}
|
|
535
|
+
function createProgram(rootNamesOrOptions, options, host, oldProgram, configFileParsingDiagnostics) {
|
|
536
|
+
let rootNames;
|
|
537
|
+
const createOpts = !Array.isArray(rootNamesOrOptions) ? rootNamesOrOptions : void 0;
|
|
538
|
+
if (createOpts) {
|
|
539
|
+
rootNames = createOpts.rootNames;
|
|
540
|
+
options = createOpts.options;
|
|
541
|
+
host = createOpts.host;
|
|
542
|
+
oldProgram = createOpts.oldProgram;
|
|
543
|
+
configFileParsingDiagnostics = createOpts.configFileParsingDiagnostics;
|
|
544
|
+
}
|
|
545
|
+
else {
|
|
546
|
+
options = options;
|
|
547
|
+
rootNames = rootNamesOrOptions;
|
|
548
|
+
}
|
|
549
|
+
const projectConfig = getProjectConfig(options, rootNames);
|
|
550
|
+
if (["tsc", "tsserver", "tsserverlibrary"].includes(tsp.currentLibrary)) {
|
|
551
|
+
options = projectConfig.compilerOptions;
|
|
552
|
+
if (createOpts)
|
|
553
|
+
createOpts.options = options;
|
|
554
|
+
}
|
|
555
|
+
const plugins = preparePluginsFromCompilerOptions(options.plugins);
|
|
556
|
+
const pluginCreator = new tsp.PluginCreator(plugins, { resolveBaseDir: projectConfig.projectDir ?? process.cwd() });
|
|
557
|
+
if (tsp.currentLibrary === "tsc" && tsp.tsShim.JSDocParsingMode && pluginCreator.needsTscJsDocParsing) {
|
|
558
|
+
host.jsDocParsingMode = tsp.tsShim.JSDocParsingMode.ParseAll;
|
|
559
|
+
}
|
|
560
|
+
let program = createOpts ?
|
|
561
|
+
tsp.tsShim.originalCreateProgram(createOpts) :
|
|
562
|
+
tsp.tsShim.originalCreateProgram(rootNames, options, host, oldProgram, configFileParsingDiagnostics);
|
|
563
|
+
const programTransformers = pluginCreator.createProgramTransformers();
|
|
564
|
+
for (const [transformerKey, [programTransformer, config]] of programTransformers) {
|
|
565
|
+
if (activeProgramTransformers.has(transformerKey))
|
|
566
|
+
continue;
|
|
567
|
+
activeProgramTransformers.add(transformerKey);
|
|
568
|
+
const newProgram = programTransformer(program, host, config, { ts: tsp.getTsInstance() });
|
|
569
|
+
if (typeof newProgram?.["emit"] === "function")
|
|
570
|
+
program = newProgram;
|
|
571
|
+
activeProgramTransformers.delete(transformerKey);
|
|
572
|
+
}
|
|
573
|
+
if (!program.originalEmit) {
|
|
574
|
+
program.originalEmit = program.emit;
|
|
575
|
+
program.emit = newEmit;
|
|
576
|
+
}
|
|
577
|
+
function newEmit(targetSourceFile, writeFile, cancellationToken, emitOnlyDtsFiles, customTransformers, ...additionalArgs) {
|
|
578
|
+
const transformers = pluginCreator.createSourceTransformers({ program }, customTransformers);
|
|
579
|
+
const result = program.originalEmit(targetSourceFile, writeFile, cancellationToken, emitOnlyDtsFiles, transformers, ...additionalArgs);
|
|
580
|
+
for (const diagnostic of tsp.diagnosticMap.get(program) || [])
|
|
581
|
+
if (!result.diagnostics.includes(diagnostic))
|
|
582
|
+
result.diagnostics.push(diagnostic);
|
|
583
|
+
return result;
|
|
584
|
+
}
|
|
585
|
+
return program;
|
|
586
|
+
}
|
|
587
|
+
tsp.createProgram = createProgram;
|
|
588
|
+
})(tsp || (tsp = {}));
|
|
589
|
+
var tsp;
|
|
590
|
+
(function (tsp) {
|
|
591
|
+
tsp.tsShim = new Proxy({}, {
|
|
592
|
+
get(_, key) {
|
|
593
|
+
const target = tsp.getTsInstance();
|
|
594
|
+
if (target) {
|
|
595
|
+
return target[key];
|
|
596
|
+
}
|
|
597
|
+
else {
|
|
598
|
+
try {
|
|
599
|
+
return eval(key);
|
|
600
|
+
}
|
|
601
|
+
catch (e) {
|
|
602
|
+
throw new tsp.TsPatchError(`Failed to find "${key}" in TypeScript shim`, e);
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
},
|
|
606
|
+
});
|
|
607
|
+
})(tsp || (tsp = {}));
|
|
608
|
+
return tsp;
|
|
609
|
+
})();
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ModuleFile } from '../module';
|
|
2
|
+
import { Position } from '../system';
|
|
3
|
+
export interface ModuleSlice {
|
|
4
|
+
moduleFile: ModuleFile;
|
|
5
|
+
firstSourceFileStart: number;
|
|
6
|
+
wrapperPos?: Position;
|
|
7
|
+
bodyPos: Position;
|
|
8
|
+
fileEnd: number;
|
|
9
|
+
sourceFileStarts: [name: string, position: number][];
|
|
10
|
+
bodyWrapper?: {
|
|
11
|
+
start: string;
|
|
12
|
+
end: string;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
export declare function sliceModule(moduleFile: ModuleFile, tsVersion: string): ModuleSlice;
|
|
16
|
+
/** @internal */
|
|
17
|
+
export declare namespace ModuleSlice {
|
|
18
|
+
const createError: (msg?: string) => Error;
|
|
19
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.ModuleSlice = void 0;
|
|
7
|
+
exports.sliceModule = sliceModule;
|
|
8
|
+
const semver_1 = __importDefault(require("semver"));
|
|
9
|
+
const ts54_1 = require("./ts54");
|
|
10
|
+
const ts55_1 = require("./ts55");
|
|
11
|
+
const ts552_1 = require("./ts552");
|
|
12
|
+
// endregion
|
|
13
|
+
/* ****************************************************************************************************************** */
|
|
14
|
+
// region: Utils
|
|
15
|
+
/* ****************************************************************************************************************** */
|
|
16
|
+
function sliceModule(moduleFile, tsVersion) {
|
|
17
|
+
const baseVersion = semver_1.default.coerce(tsVersion, { includePrerelease: false });
|
|
18
|
+
if (!baseVersion)
|
|
19
|
+
throw new Error(`Could not parse TS version: ${tsVersion}`);
|
|
20
|
+
if (semver_1.default.lt(baseVersion, '5.0.0')) {
|
|
21
|
+
throw new Error(`Cannot patch TS version <5`);
|
|
22
|
+
}
|
|
23
|
+
if (semver_1.default.lt(baseVersion, '5.5.0')) {
|
|
24
|
+
return (0, ts54_1.sliceTs54)(moduleFile);
|
|
25
|
+
}
|
|
26
|
+
if (semver_1.default.lt(baseVersion, '5.5.2')) {
|
|
27
|
+
return (0, ts55_1.sliceTs55)(moduleFile);
|
|
28
|
+
}
|
|
29
|
+
return (0, ts552_1.sliceTs552)(moduleFile);
|
|
30
|
+
}
|
|
31
|
+
/** @internal */
|
|
32
|
+
var ModuleSlice;
|
|
33
|
+
(function (ModuleSlice) {
|
|
34
|
+
ModuleSlice.createError = (msg) => new Error(`Could not recognize TS format during slice!` + (msg ? ` — ${msg}` : ''));
|
|
35
|
+
})(ModuleSlice || (exports.ModuleSlice = ModuleSlice = {}));
|
|
36
|
+
// endregion
|
|
37
|
+
//# sourceMappingURL=module-slice.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"module-slice.js","sourceRoot":"","sources":["../../../projects/core/src/slice/module-slice.ts"],"names":[],"mappings":";;;;;;AAgCA,kCAiBC;AA/CD,oDAA4B;AAC5B,iCAAmC;AACnC,iCAAmC;AACnC,mCAAqC;AAoBrC,YAAY;AAGZ,wHAAwH;AACxH,gBAAgB;AAChB,wHAAwH;AAExH,SAAgB,WAAW,CAAC,UAAsB,EAAE,SAAiB;IACnE,MAAM,WAAW,GAAG,gBAAM,CAAC,MAAM,CAAC,SAAS,EAAE,EAAE,iBAAiB,EAAE,KAAK,EAAE,CAAC,CAAC;IAC3E,IAAI,CAAC,WAAW;QAAE,MAAM,IAAI,KAAK,CAAC,+BAA+B,SAAS,EAAE,CAAC,CAAC;IAE9E,IAAI,gBAAM,CAAC,EAAE,CAAC,WAAW,EAAE,OAAO,CAAC,EAAE,CAAC;QACpC,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAC;IAChD,CAAC;IAED,IAAI,gBAAM,CAAC,EAAE,CAAC,WAAW,EAAE,OAAO,CAAC,EAAE,CAAC;QACpC,OAAO,IAAA,gBAAS,EAAC,UAAU,CAAC,CAAC;IAC/B,CAAC;IAED,IAAI,gBAAM,CAAC,EAAE,CAAC,WAAW,EAAE,OAAO,CAAC,EAAE,CAAC;QACpC,OAAO,IAAA,gBAAS,EAAC,UAAU,CAAC,CAAC;IAC/B,CAAC;IAED,OAAO,IAAA,kBAAU,EAAC,UAAU,CAAC,CAAC;AAChC,CAAC;AAED,gBAAgB;AAChB,IAAiB,WAAW,CAG3B;AAHD,WAAiB,WAAW;IACb,uBAAW,GAAG,CAAC,GAAY,EAAE,EAAE,CAC1C,IAAI,KAAK,CAAC,6CAA6C,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,GAAG,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AACxF,CAAC,EAHgB,WAAW,2BAAX,WAAW,QAG3B;AAED,YAAY"}
|