-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.ts
138 lines (120 loc) · 3.13 KB
/
build.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
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
import Babel from "https://dev.jspm.io/@babel/standalone";
import {
dirname,
extname,
join,
} from "https://deno.land/[email protected]/path/mod.ts";
import { ensureDir } from "https://deno.land/[email protected]/fs/mod.ts";
interface BabelImportDeclarationPath {
node: {
source: {
value: string;
};
};
}
function denoImports() {
return {
visitor: {
ImportDeclaration(path: BabelImportDeclarationPath) {
if (path.node.source) {
path.node.source.value = path.node.source.value.replace(
/\.ts$/,
".js",
);
}
},
ExportDeclaration(path: BabelImportDeclarationPath) {
if (path.node.source) {
path.node.source.value = path.node.source.value.replace(
/\.ts$/,
".js",
);
}
},
},
};
}
interface BabelCallExpressionPath {
node: {
callee: {
name: string;
};
arguments: {
value: string;
}[];
};
}
function denoRequires() {
return {
visitor: {
CallExpression(path: BabelCallExpressionPath) {
if (path.node.callee.name === "require") {
for (let i = 0; i < path.node.arguments.length; i++) {
path.node.arguments[i].value = path.node.arguments[i].value.replace(
/\.ts$/,
".js",
);
}
}
},
},
};
}
Babel.registerPlugin("deno-imports", denoImports);
Babel.registerPlugin("deno-requires", denoRequires);
const cwd = Deno.cwd();
const dstPath = join(cwd, "dist");
const esmPath = join(dstPath, "esm");
const cjsPath = join(dstPath, "cjs");
const [_, cjsMap] = await Deno.compile("mod.ts", undefined, {
declaration: true,
lib: ["es2019", "es2020.bigint", "es2020.string", "es2020.symbol.wellknown"],
module: "commonjs",
removeComments: false,
sourceMap: false,
target: "es2019",
});
const [__, esmMap] = await Deno.compile("mod.ts", undefined, {
declaration: true,
lib: ["es2019", "es2020.bigint", "es2020.string", "es2020.symbol.wellknown"],
module: "es2015",
removeComments: false,
sourceMap: false,
target: "es2019",
});
for (const key of Reflect.ownKeys(cjsMap)) {
if (typeof key === "string") {
const path = join(cjsPath, new URL(key).pathname.replace(cwd, "").slice(1));
let data = cjsMap[key];
await ensureDir(dirname(path));
if (extname(path) === ".js") {
data = Babel.transform(data, { plugins: ["deno-requires"] }).code;
}
if (extname(path) === ".ts") {
data = data.replace(/\.ts/g, ".js");
}
await Deno.writeTextFile(
path,
data,
{ create: true, append: false },
);
}
}
for (const key of Reflect.ownKeys(esmMap)) {
if (typeof key === "string") {
const path = join(esmPath, new URL(key).pathname.replace(cwd, "").slice(1));
let data = esmMap[key];
await ensureDir(dirname(path));
if (extname(path) === ".js") {
data = Babel.transform(data, { plugins: ["deno-imports"] }).code;
}
if (extname(path) === ".ts") {
data = data.replace(/\.ts/g, ".js");
}
await Deno.writeTextFile(
path,
data,
{ create: true, append: false },
);
}
}