-
Notifications
You must be signed in to change notification settings - Fork 0
/
_old_builder.ts
237 lines (231 loc) · 6.74 KB
/
_old_builder.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
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
import { rollup } from "./rollup-browser";
import virtual from "./plugin-virtual";
import alias from "./plugin-alias";
import * as JSON5 from "json5";
import ts from "typescript";
import path from "path-browserify";
// import { minify } from "terser";
let minifyTerser;
(async()=>{
minifyTerser=(await import("terser")).minify;
})();
// now i need to write a bundler that works in the browser
// (async () => {
// try {
// esbuild.initialize({});
// } catch (e) {
// esbuild.initialize({
// wasmModule: await WebAssembly.compileStreaming(
// fetch("https://esm.sh/esbuild-wasm/esbuild.wasm"),
// ),
// });
// }
// })();
async function toTypeScriptAPIReadable(tsconfig: any) {
let res: ts.CompilerOptions = {};
for (let key in tsconfig) {
if (
!["target", "moduleResolution", "module", "baseUrl", "rootDir"].includes(
key,
)
) {
res[key] = tsconfig[key];
}
}
return res;
}
export async function build(
fileList: Record<string, string>,
entry: string,
tsconfigRaw: string,
logger,
dao3config?: any,
format: "es" | "cjs" = "cjs",
importMap?: string | undefined,
developmentMode: boolean = false,
) {
// read paths
let aliases = [], tsconfig:any={};
if (tsconfigRaw) {
try {
tsconfig = JSON5.parse(tsconfigRaw).compilerOptions||tsconfig;
} catch (e) {
throw new Error(`tsconfig读取出错!${e}`);
}
if (tsconfig.paths) {
for (let key in tsconfig.paths) {
// prob suffix
if (tsconfig.paths[key][0].startsWith("")) {
tsconfig.paths[key][0] = tsconfig.paths[key][0].slice(2);
}
if (
!tsconfig.paths[key][0].endsWith(".ts") &&
!tsconfig.paths[key][0].endsWith(".js")
) {
if (fileList[`${tsconfig.paths[key][0]}.ts`]) {
tsconfig.paths[key][0] = `${tsconfig.paths[key][0]}.ts`;
} else if (fileList[`${tsconfig.paths[key][0]}.js`]) {
tsconfig.paths[key][0] = `${tsconfig.paths[key][0]}.js`;
}
}
aliases.push({
find: key,
replacement: tsconfig.paths[key][0],
});
}
}
}
if (importMap) {
let importMapJSON: any;
try {
importMapJSON = JSON5.parse(importMap);
// add to alias
for(let key in importMapJSON.imports){
aliases.push({
find: key,
replacement: importMapJSON.imports[key],
});
}
} catch (e) {
throw new Error(`importMap读取出错!${e}`);
}
}
let newfileList = {};
// load tsconfig compilerOptions to interface CompilerOptions
let compilerOptions = await toTypeScriptAPIReadable(tsconfig);
Object.assign(compilerOptions, {
target: ts.ScriptTarget.ESNext,
paths: tsconfig.paths,
});
for (let key in fileList) {
if (key.startsWith("dist/") || key.startsWith(".log/")) {continue;}
if(!key.endsWith(".ts")){
newfileList[key] = fileList[key];
continue;
}
newfileList[key] = ts.transpile(fileList[key], compilerOptions);
}
// logger.info(`fileList:${JSON.stringify(newfileList)}`);
const rolled = await rollup({
input: [entry],
plugins: [
alias({
entries: aliases,
}) as any,
virtual({
...newfileList,
}) as any,
{
name:"specifier-resolver",
resolveId(source,importer){
if(source.startsWith(".")){return;}
if(source.startsWith("\x00virtual:")){return;}
if(source.startsWith("http://") || source.startsWith("https://")){return;}
let regex=/^([a-zA-Z0-9_]+):(.+)$/;
if(!regex.test(source)){return;}
let res=source.match(regex);
let specifier=res[1],arg=res[2];
if(specifier==="npm"){
return `https://esm.sh/${arg}`;
}else if(specifier==="jsr"){
return `https://esm.sh/jsr/${arg}`;
}
return;
}
},
{
name: "url-resolver",
resolveId(source, importer) {
if (!dao3config?.ArenaLess?.experimental?.allowUrlImport === false) {
logger.error(
"url import is not allowed! fallback to file system. (Please set ArenaLess.experimental.allowUrlImport to true in dao3.config.json)[default true]",
);
return;
}
if (
source[0] !== "." &&
(source.startsWith("/") || source.startsWith("http://") ||
source.startsWith("https://"))
) {
try {
new URL(source);
// If it is a valid URL, return it
return source;
} catch {
// Otherwise make it external
try {
new URL(source, importer);
// If it is a valid URL, return it
return new URL(source, importer).href;
} catch {
// Otherwise make it external
return;
}
}
}
},
async load(id) {
const response = await fetch(id);
return response.text();
},
},
{
name: "virtual-resolver",
resolveId(source, importer, options) {
// logger.info(`resolveId:${source} ${importer} ${JSON.stringify(options)}`);
if (source.startsWith(".")) {
source = path.join(path.dirname(importer), source).trim().replace(
"\x00virtual:",
"",
);
}
// file prob
if (source.split(".").length===1) {
if (newfileList[`${source}.ts`]) {
source = `${source}.ts`;
console.log("added ts");
} else if (newfileList[`${source}.js`]) {
source = `${source}.js`;
}
}
// logger.info(`resolveId solved:${source}`);
return "\x00virtual:" + source;
},
},
{
name: "json-loader",
transform(code, id) {
if(id.endsWith(".json")){
return {code: `export default ${JSON.stringify(JSON.parse(code))}`};
}else{
return null;
}
},
},
// {
// name: "esbuild-minify",
// async renderChunk(code, chunk) {
// return (await esbuild.transform(code, { minify: true, loader: "js" }))
// .code;
// },
// },
{
name:"terser-minify",
async renderChunk(code,chunk){
if(developmentMode){return;}
return (await minifyTerser(code, {})).code;
}
}
// terser({})
],
});
// ts
const out = await rolled.generate({ format: format });
return out.output[0].code;
}
// build(
// a,
// "src/App.ts",
// a["tsconfig.json"],
// console,
// ).then((code) => console.log(code));