-
Notifications
You must be signed in to change notification settings - Fork 2
/
deno2node.ts
74 lines (70 loc) · 2.38 KB
/
deno2node.ts
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
// based on https://github.com/dsherret/code-block-writer/blob/99454249cd13bd89befa45ac815b37b3e02896f5/scripts/build_npm.ts
import { Context } from "./context.ts";
import { Node, SourceFile } from "./deps.deno.ts";
import { transpileSpecifier } from "./_transformations/specifiers.ts";
import { vendorEverything } from "./_transformations/vendor.ts";
import { shimEverything } from "./_transformations/shim.ts";
function transpileImportSpecifiers(sourceFile: SourceFile) {
for (const statement of sourceFile.getStatements()) {
if (
Node.isImportDeclaration(statement) ||
Node.isExportDeclaration(statement)
) {
const modSpecifierValue = statement.getModuleSpecifierValue();
if (modSpecifierValue !== undefined) {
statement.setModuleSpecifier(transpileSpecifier(modSpecifierValue));
}
}
}
}
const isDenoSpecific = (sourceFile: SourceFile) =>
sourceFile.getBaseNameWithoutExtension().toLowerCase().endsWith(".deno");
/**
* Attempts to transform arbitrary `ctx.project` into a valid Node.js project:
*
* 1. Changes import specifiers to be Node-friendly:
* - changes extension in relative specifiers to `.js`,
* - replaces some `https://` imports with bare specifiers.
*
* 2. Changes `*.deno.js` imports specifiers to `*.node.js`
* (`import './deps.deno.ts'` -> `import './deps.node.js'`).
* This can be used for re-exporting dependencies
* and other runtime-specific code.
*
* 3. Rewrites remaining https: imports to point
* into `vendorDir`, if specified:
* ```json
* // @filename: tsconfig.json
* {
* "deno2node": {
* "vendorDir": "src/.deno2node/vendor/"
* }
* }
* ```
*
* 4. Imports Node.js shims for Deno globals
* from [shim file], if specified:
* ```json
* // @filename: tsconfig.json
* {
* "deno2node": {
* "shim": "src/shim.node.ts"
* }
* }
* ```
*
* [shim file]: https://github.com/wojpawlik/deno2node/blob/main/src/shim.node.ts
*/
export async function deno2node(ctx: Context): Promise<void> {
console.time("Basic transformations");
for (const sourceFile of ctx.project.getSourceFiles()) {
if (isDenoSpecific(sourceFile)) {
ctx.project.removeSourceFile(sourceFile);
continue;
}
transpileImportSpecifiers(sourceFile);
}
console.timeEnd("Basic transformations");
await vendorEverything(ctx);
shimEverything(ctx);
}