-
Notifications
You must be signed in to change notification settings - Fork 18
/
bundle_util.ts
120 lines (105 loc) · 2.69 KB
/
bundle_util.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
import { CommonOptions } from "https://deno.land/x/[email protected]/mod.js";
import {
build,
denoPlugins,
exists,
parseJsonC,
resolve,
stop,
toFileUrl,
} from "./deps.ts";
export async function bundleByEsbuild(
path: string,
): Promise<string> {
const importMapFile = getImportMap();
let importMapURL: URL | undefined;
if (importMapFile) {
importMapURL = toFileUrl(resolve(importMapFile));
}
// @NekoMaru76: option tsconfig in esbuild's build doesn't work, so I did this
let jsx: CommonOptions["jsx"];
let jsxFactory: CommonOptions["jsxFactory"];
let jsxFragment: CommonOptions["jsxFragment"];
let jsxDev: CommonOptions["jsxDev"];
let jsxImportSource: CommonOptions["jsxImportSource"];
const tsconfigFile = await getTsconfig();
if (tsconfigFile) {
const config = <{
compilerOptions: {
jsx: CommonOptions["jsx"];
jsxFactory: CommonOptions["jsxFactory"];
jsxFragmentFactory: CommonOptions["jsxFragment"];
jsxDev: CommonOptions["jsxDev"];
jsxImportSource: CommonOptions["jsxImportSource"];
};
}> parseJsonC(await Deno.readTextFile(tsconfigFile));
if (
config && typeof config === "object" &&
config.compilerOptions &&
typeof config.compilerOptions === "object"
) {
jsx = config.compilerOptions.jsx;
jsxDev = config.compilerOptions.jsxDev;
jsxFactory = config.compilerOptions.jsxFactory;
jsxFragment = config.compilerOptions.jsxFragmentFactory;
jsxImportSource = config.compilerOptions.jsxImportSource;
}
}
const bundle = await build({
entryPoints: [toFileUrl(resolve(path)).href],
plugins: [
...denoPlugins({
importMapURL: importMapURL?.href,
}),
],
bundle: true,
write: false,
jsx,
jsxFactory,
jsxDev,
jsxFragment,
jsxImportSource,
});
await stop();
return bundle.outputFiles![0].text;
}
let _importMap: string | undefined;
export function setImportMap(importMap: string) {
_importMap = importMap;
}
export function getImportMap() {
return _importMap;
}
let _tsconfig: string | undefined;
export function setTsconfig(tsconfig: string) {
_tsconfig = tsconfig;
}
export async function getTsconfig() {
if (!_tsconfig) {
if (
await exists("./deno.json", {
isReadable: true,
isDirectory: false,
})
) {
return "./deno.json";
}
if (
await exists("./deno.jsonc", {
isReadable: true,
isDirectory: false,
})
) {
return "./deno.jsonc";
}
if (
await exists("./tsconfig.json", {
isReadable: true,
isDirectory: false,
})
) {
return "./tsconfig.json";
}
}
return _tsconfig;
}