-
Notifications
You must be signed in to change notification settings - Fork 231
Expand file tree
/
Copy pathbundleLocalTs.ts
More file actions
43 lines (37 loc) · 1.92 KB
/
bundleLocalTs.ts
File metadata and controls
43 lines (37 loc) · 1.92 KB
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
import { folders, path, tsMorph } from "./deps.ts";
const { Node, Project } = tsMorph;
const project = new Project();
const folderPath = "./dist";
const fileSystem = project.getFileSystem();
const commonFile = project.addSourceFileAtPath(`${folderPath}/ts-morph-common.js`);
for (const varDecl of commonFile.getVariableDeclarations()) {
const initializer = varDecl.getInitializer();
if (initializer != null && Node.isCallExpression(initializer) && initializer.getExpression().getText() === "require") {
const args = initializer.getArguments();
const firstArg = args?.[0];
if (args.length === 1 && firstArg != null && Node.isStringLiteral(firstArg) && firstArg.getLiteralValue() === "typescript")
varDecl.setInitializer("require('./typescript')");
}
}
commonFile.saveSync();
const localTypescriptLibFolderPath = path.join(folders.common, "node_modules/typescript/lib");
const typescriptLibFolderPath = fileSystem.directoryExistsSync(localTypescriptLibFolderPath)
? localTypescriptLibFolderPath
: path.join(folders.root, "node_modules/typescript/lib");
const typeScriptSource = fileSystem.readFileSync(path.join(typescriptLibFolderPath, "typescript.js"))
.replace("\n//# sourceMappingURL=typescript.js.map\n", "\n");
fileSystem.writeFileSync("./dist/typescript.js", typeScriptSource);
const typescriptDtsFileText = fileSystem.readFileSync(path.join(typescriptLibFolderPath, "typescript.d.ts"));
fileSystem.writeFileSync("./lib/typescript.d.ts", typescriptDtsFileText.replace("export = ts;", "export { ts };"));
// add a _nodeWithTypeArgumentsBrand to NodeWithTypeArguments
// in order to distinguish it from TypeNode
const tsFile = project.addSourceFileAtPath("./lib/typescript.d.ts");
const nodeWithTypeArgs = tsFile
.getModules()
.map(m => m.getInterface("NodeWithTypeArguments"))
.filter(m => m != null)[0]!;
nodeWithTypeArgs.insertProperty(0, {
name: "_nodeWithTypeArgumentsBrand",
type: "any",
});
tsFile.saveSync();