-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
147 lines (145 loc) · 4.58 KB
/
index.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
import { rollup } from "./rollup-browser";
import { type Plugin } from "./rollup-browser";
// import { transformSync } from "./swc-wasm-typescript";
// @ts-ignore
// import coffee from "./coffeescript";
import ts from "typescript"
import * as JSON5 from "json5";
import { arenaless, jsonLoader } from "./arenaless-rollup-plugin";
import alias from "./plugins/alias/src/index"
let minifyTerser:any;
(async()=>{
minifyTerser=(await import("terser")).minify;
})();
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, Uint8Array>,
entry: string,
tsconfigRaw: string,
logger:any,
format: "es" | "cjs" = "cjs",
importMap?: string | undefined,
developmentMode: boolean = false,
dir_prefix_for_aliases: string = "",
) {
// read paths
let aliases:any[] = [], tsconfig:any={};
if (tsconfigRaw) {
try {
tsconfig = JSON5.parse(tsconfigRaw).compilerOptions||tsconfig;
} catch (e) {
throw new Error(`tsconfig读取出错!${e}`);
}
if(!tsconfig.paths){
tsconfig.paths={};
}
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(".tsx") &&
!tsconfig.paths[key][0].endsWith(".jsx") &&
!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`;
} else if(fileList[`${tsconfig.paths[key][0]}.jsx`]){
tsconfig.paths[key][0] = `${tsconfig.paths[key][0]}.jsx`;
}else if(fileList[`${tsconfig.paths[key][0]}.tsx`]){
tsconfig.paths[key][0] = `${tsconfig.paths[key][0]}.tsx`;
}
tsconfig.paths[key][0]=dir_prefix_for_aliases+tsconfig.paths[key][0];
// else if(fileList[`${tsconfig.paths[key][0]}.coffee`]){
// tsconfig.paths[key][0] = `${tsconfig.paths[key][0]}.coffee`;
// } else if(fileList[`${tsconfig.paths[key][0]}.cjsx`]){
// tsconfig.paths[key][0] = `${tsconfig.paths[key][0]}.cjsx`;
// }
}
// 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],
});
// tsconfig.paths[key]=[importMapJSON.imports[key]];
}
} catch (e) {
throw new Error(`importMap读取出错!${e}`);
}
}
let finaltsconfig=await toTypeScriptAPIReadable(tsconfig);
Object.assign(finaltsconfig, {
target: ts.ScriptTarget.ESNext,
paths: tsconfig.paths,
});
let newfileList: Record<string, Uint8Array> = {};
// load tsconfig compilerOptions to interface CompilerOptions
for (let key in fileList) {
if (key.startsWith("dist/") || key.startsWith(".log/")) {continue;}
newfileList[key]=fileList[key];
}
// logger.info(`fileList:${JSON.stringify(newfileList)}`);
const rolled = await rollup({
input: [entry],
plugins: [
alias({
entries: aliases,
}) as Plugin,
{
name:"transpile",
transform(code, id) {
if(id.endsWith(".ts")||id.endsWith(".jsx")||id.endsWith(".tsx")){
return ts.transpile(code,finaltsconfig)
}
// if(id.endsWith(".coffee")||id.endsWith(".cjsx")){
// return coffee.compile(code,{bare:false});
// }
},
},
arenaless({
modules_raw: newfileList,
tsconfig:finaltsconfig
})as Plugin,
jsonLoader()as Plugin,
{
name:"terser-minify",
async renderChunk(code,chunk){
if(developmentMode){return;}
return (await minifyTerser(code, {})).code;
}
}
],
});
// ts
const out = await rolled.generate({ format: format });
return out.output[0].code;
}