-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
190 lines (179 loc) · 7.17 KB
/
index.ts
File metadata and controls
190 lines (179 loc) · 7.17 KB
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
import { MiddlewareCreater, SetResult } from 'f2e-server'
import * as esbuild from 'esbuild'
import * as path from 'path'
import * as fs from 'fs'
import { createExternal, defaultOptions, defaultTsconfig, html, pathname_fixer } from './utils'
namespace creater {
export interface BuildOptions extends esbuild.BuildOptions {
ignore_external?: boolean
}
}
const creater: MiddlewareCreater = (conf, options = {}) => {
const { root, build } = conf
const {
/** 默认配置文件地址 */
esbuildrc = '.esbuildrc.js',
/** 针对哪些文件监听修改 */
watches = [/\.[jet]?sx?$/],
/** 临时文件地址,需要添加到 .gitignore */
cacheRoot = '.esbuild',
/** external文件名称生成方式 */
externalName = (index: number) => `external${index > 0 ? `.${index}` : ''}.ts`,
/** external导出变量名 */
moduleName = (index: number) => `__LIBS_${index}__`,
/** 运行时修改的通用esbuild编译参数不含 external 包 */
options: runtimeOptions,
/** 运行时修改的external编译参数 */
externalOptions = {},
} = options
const cache_root = path.join(root, cacheRoot)
if (!fs.existsSync(cache_root)) {
fs.mkdirSync(cache_root)
}
const cache_esbuild = path.join(cache_root, 'esbuild.json')
if (!fs.existsSync(cache_esbuild)) {
fs.writeFileSync(cache_esbuild, JSON.stringify(defaultOptions(cacheRoot), null, 2))
}
const cache_tsconfig = path.join(cache_root, 'tsconfig.json')
// 写入默认打包tsconfig
if (!fs.existsSync(cache_tsconfig)) {
fs.writeFileSync(cache_tsconfig, JSON.stringify(defaultTsconfig(cacheRoot), null, 2))
}
const option_map = ([].concat(require(path.join(root, esbuildrc))) as creater.BuildOptions[]).reduce((all, op, index) => {
const {
entryPoints,
ignore_external = false,
...base_config
}: creater.BuildOptions = Object.assign({}, op, runtimeOptions);
const { external = [] } = base_config
const globalName = moduleName(index)
if (external.length > 0 && !ignore_external) {
const libname = externalName(index)
fs.writeFileSync(path.join(cache_root, libname), createExternal(external))
const entry = `${cacheRoot}/${libname}`
all.set(entry, {
...require(cache_esbuild),
...externalOptions,
minify: build,
entryPoints: [entry],
globalName,
footer: {
js: `require = function (n) {
var m = ${globalName}[n];
if (!m && '.' != n[0]) {
console.error('module not found:', n);
}
if (m.default) {
m = Object.assign(m.default, m)
}
return m;
};\n`
},
})
}
const entries = Object.entries(entryPoints)
entries.forEach(([k, v]) => {
if (typeof k === 'number') {
all.set(v, {
entryPoints: [v],
...base_config,
})
} else {
all.set(v, {
entryPoints: { [k]: v },
...base_config,
})
}
})
return all
}, new Map<string, esbuild.BuildOptions>())
/** 缓存的编译配置,增量执行 */
const ctx_map = new Map<string, esbuild.BuildContext>()
/** 文件依赖,前面是entries */
const deps_map = new Map<string, string[]>()
// 编译中的文件
const building_set = new Set<string>()
// 等待编译的文件
const needbuilds = new Set<string>()
return {
onSet: async (pathname, data, store) => {
let result_js: SetResult = {
outputPath: pathname,
data,
}
const entry = option_map.get(pathname)
if (!entry) {
return result_js
}
const options: esbuild.BuildOptions = {
write: false,
metafile: true,
minify: build,
...entry,
}
try {
const result = await (async function (build) {
if (build) {
return esbuild.build(options)
}
let ctx = ctx_map.get(pathname)
if (!ctx) {
ctx = await esbuild.context(options)
ctx_map.set(pathname, ctx)
}
return ctx.rebuild()
})(build)
if (result) {
const outputFiles = result.outputFiles;
if (outputFiles && outputFiles.length) {
for (let i = 0; i < outputFiles.length; i++) {
const outputFile = outputFiles[i];
const outputPath = pathname_fixer(path.relative(root, outputFile.path));
if (!build && /\.js$/.test(outputPath)) {
const meta_json = JSON.stringify(result.metafile)
store._set(outputPath + '.json', meta_json);
store._set(outputPath + '.html', html.analyze);
}
deps_map.set(pathname, Object.keys(result.metafile.inputs || {}).map(i => pathname_fixer(i)));
let data = Buffer.concat([outputFile.contents]);
store._set(outputPath, data);
if (/\.js$/.test(outputPath)) {
result_js = {
outputPath,
data,
end: true,
}
}
}
}
}
return result_js
}
catch (e) {
console.log(e)
return data;
}
},
buildWatcher: async (pathname, type, build) => {
const find = watches.find(reg => reg.test(pathname))
if (find) {
deps_map.forEach(async (deps, entry) => {
if (deps.includes(pathname)) {
if (!building_set.has(entry)) {
building_set.add(entry);
await build(entry)
if (needbuilds.has(entry)) {
needbuilds.delete(entry)
await build(entry)
}
building_set.delete(entry);
} else {
needbuilds.add(entry)
}
}
})
}
}
}
}
export = creater