/
modify-typescript-module.mjs
264 lines (215 loc) · 6.54 KB
/
modify-typescript-module.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import path from "node:path";
import escapeStringRegexp from "escape-string-regexp";
import { outdent } from "outdent";
import MagicString from "magic-string";
import { writeFile, PROJECT_ROOT } from "../utils/index.mjs";
function* getModules(text) {
const parts = text.split(/(?<=\n)( {2}\/\/ src\/\S+\n)/);
let start = parts[0].length;
for (let partIndex = 1; partIndex < parts.length - 1; partIndex += 2) {
const comment = parts[partIndex];
const code = parts[partIndex + 1];
const path = comment.slice(" // ".length, -1);
const end = start + comment.length + code.length;
if (/\S/.test(code)) {
yield {
isEntry: path === "src/typescript/typescript.ts",
path,
start: start + comment.length,
end: end - 1,
code,
};
}
start = end;
}
}
class TypeScriptModuleSource {
#source;
modules;
constructor(text) {
this.#source = new MagicString(text);
this.modules = [...getModules(text)];
}
replaceModule(module, replacement) {
if (typeof module === "string") {
module = this.modules.find((searching) => searching.path === module);
}
if (!module) {
throw Object.assign(new Error("Module not found"), { module });
}
this.#source.overwrite(module.start, module.end, replacement);
return this;
}
removeModule(module) {
return this.replaceModule(module, "");
}
replaceAlignedCode({ start, end, replacement = "" }) {
const regexp = new RegExp(
[
"(?<=\n)",
"(?<indentString>\\s*)",
escapeStringRegexp(start),
".*?",
"(?<=\n)",
"\\k<indentString>",
escapeStringRegexp(end),
"(?=\n)",
].join(""),
"gsu"
);
this.#source.replaceAll(regexp, replacement);
return this;
}
remove(...args) {
this.#source.remove(...args);
return this;
}
prepend(...args) {
this.#source.prepend(...args);
return this;
}
append(...args) {
this.#source.append(...args);
return this;
}
replace(...args) {
this.#source.replace(...args);
return this;
}
replaceAll(...args) {
this.#source.replaceAll(...args);
return this;
}
applyChanges() {
const text = this.#source.toString();
this.#source = new MagicString(text);
this.modules = getModules(text);
}
toString() {
return this.#source.toString();
}
}
function unwrap(text) {
const startMark = "var ts = (() => {";
const endMark = "return __toCommonJS(typescript_exports);";
const start = text.indexOf(startMark);
const end = text.lastIndexOf(endMark);
if (start === -1 || end === -1) {
throw new Error("Unexpected source");
}
return text.slice(start + startMark.length, end);
}
function modifyTypescriptModule(text) {
text = unwrap(text);
const source = new TypeScriptModuleSource(text);
// Deprecated
for (const module of source.modules) {
if (module.path.startsWith("src/deprecatedCompat/")) {
source.removeModule(module);
}
}
// jsTyping
for (const module of source.modules) {
if (module.path.startsWith("src/jsTyping/")) {
source.removeModule(module);
}
}
// services
for (const module of source.modules) {
if (module.path === "src/services/services.ts") {
continue;
}
// This is a big module, most code except `scanner` is not used
if (module.path === "src/services/utilities.ts") {
source.replaceModule(
module,
"var scanner = createScanner(99 /* Latest */, true);"
);
continue;
}
if (module.path.startsWith("src/services/")) {
source.removeModule(module);
}
}
// `transformers`
source.removeModule("src/compiler/transformer.ts");
for (const module of source.modules) {
if (module.path.startsWith("src/compiler/transformers/")) {
source.removeModule(module);
}
}
// `ts.moduleSpecifiers`
source.removeModule("src/compiler/_namespaces/ts.moduleSpecifiers.ts");
source.removeModule("src/compiler/moduleSpecifiers.ts");
// Sourcemap
source.removeModule("src/compiler/sourcemap.ts");
// watch
source.removeModule("src/compiler/watch.ts");
source.removeModule("src/compiler/watchPublic.ts");
source.removeModule("src/compiler/watchUtilities.ts");
// build
source.removeModule("src/compiler/commandLineParser.ts");
source.removeModule("src/compiler/builder.ts");
source.removeModule("src/compiler/builderPublic.ts");
source.removeModule("src/compiler/resolutionCache.ts");
source.removeModule("src/compiler/tsbuild.ts");
source.removeModule("src/compiler/tsbuildPublic.ts");
// Misc
source.removeModule("src/compiler/symbolWalker.ts");
source.removeModule("src/compiler/binder.ts");
source.removeModule("src/compiler/semver.ts");
source.removeModule("src/compiler/program.ts");
source.removeModule("src/compiler/moduleNameResolver.ts");
source.removeModule("src/compiler/checker.ts");
source.removeModule("src/compiler/visitorPublic.ts");
source.removeModule("src/compiler/_namespaces/ts.performance.ts");
// File system
source.replaceModule("src/compiler/sys.ts", "var sys");
source.replaceModule("src/compiler/tracing.ts", "var tracing");
// perfLogger
source.replaceModule(
"src/compiler/perfLogger.ts",
"var perfLogger = new Proxy(() => {}, {get: () => perfLogger});"
);
// performanceCore
source.replaceModule(
"src/compiler/performanceCore.ts",
outdent`
var tryGetNativePerformanceHooks = () => {};
var timestamp = Date.now;
`
);
// `factory`
source.removeModule("src/compiler/factory/emitNode.ts");
source.removeModule("src/compiler/factory/emitHelpers.ts");
source.replaceModule(
"src/compiler/factory/nodeConverters.ts",
outdent`
var createNodeConverters = () => new Proxy({}, {get: () => () => {}});
`
);
/* spell-checker: disable */
// `ts.createParenthesizerRules`
source.replaceAlignedCode({
start: "function createParenthesizerRules(",
end: "}",
});
/* spell-checker: enable */
source.replaceAlignedCode({
start: "function getScriptTargetFeatures(",
end: "}",
});
source.replaceAlignedCode({
start: "var __require = ",
end: "});",
});
source.append("module.exports = __toCommonJS(typescript_exports);");
return source.toString();
}
// Save modified code to `{PROJECT_ROOT}/.tmp/modified-typescript.js` for debug
const saveOutputToDisk = (process) => (text) => {
const result = process(text);
writeFile(path.join(PROJECT_ROOT, ".tmp/modified-typescript.js"), result);
return result;
};
export default saveOutputToDisk(modifyTypescriptModule);