@module-federation/treeshake-server 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +71 -0
- package/dist/adapters/createAdapterDeps.d.ts +10 -0
- package/dist/adapters/local/adapter.d.ts +10 -0
- package/dist/adapters/local/index.d.ts +3 -0
- package/dist/adapters/local/localObjectStore.d.ts +12 -0
- package/dist/adapters/registry.d.ts +7 -0
- package/dist/adapters/types.d.ts +70 -0
- package/dist/app.d.ts +18 -0
- package/dist/cli/ossEnv.d.ts +18 -0
- package/dist/domain/build/constant.d.ts +1 -0
- package/dist/domain/build/normalize-config.d.ts +21 -0
- package/dist/domain/build/retrieve-global-name.d.ts +1 -0
- package/dist/domain/build/schema.d.ts +31 -0
- package/dist/domain/upload/constant.d.ts +2 -0
- package/dist/domain/upload/retrieve-cdn-path.d.ts +4 -0
- package/dist/frontend/adapter/index.d.ts +13 -0
- package/dist/frontend/adapter/index.js +128 -0
- package/dist/frontend/adapter/index.mjs +83 -0
- package/dist/frontend/favicon.ico +0 -0
- package/dist/frontend/index.html +1 -0
- package/dist/frontend/static/css/index.16175e0f.css +1 -0
- package/dist/frontend/static/js/954.dfe166a3.js +2 -0
- package/dist/frontend/static/js/954.dfe166a3.js.LICENSE.txt +16 -0
- package/dist/frontend/static/js/async/873.6ccd5409.js +2 -0
- package/dist/frontend/static/js/async/951.ec9191e2.js +12 -0
- package/dist/frontend/static/js/async/951.ec9191e2.js.LICENSE.txt +6 -0
- package/dist/frontend/static/js/async/987.6bf8e9b0.js +2 -0
- package/dist/frontend/static/js/async/987.6bf8e9b0.js.LICENSE.txt +6 -0
- package/dist/frontend/static/js/index.db4e73c6.js +88 -0
- package/dist/frontend/static/js/lib-react.c59642e3.js +2 -0
- package/dist/frontend/static/js/lib-react.c59642e3.js.LICENSE.txt +39 -0
- package/dist/frontend/static/js/lib-router.75e1e689.js +4 -0
- package/dist/frontend/static/js/lib-router.75e1e689.js.LICENSE.txt +10 -0
- package/dist/http/env.d.ts +10 -0
- package/dist/http/middlewares/di.middleware.d.ts +4 -0
- package/dist/http/middlewares/logger.middleware.d.ts +3 -0
- package/dist/http/routes/build.d.ts +3 -0
- package/dist/http/routes/index.d.ts +2 -0
- package/dist/http/routes/maintenance.d.ts +3 -0
- package/dist/http/routes/static.d.ts +5 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +958 -0
- package/dist/index.mjs +889 -0
- package/dist/infra/logger.d.ts +6 -0
- package/dist/nodeServer.d.ts +7 -0
- package/dist/ports/objectStore.d.ts +1 -0
- package/dist/ports/projectPublisher.d.ts +1 -0
- package/dist/server.d.ts +2 -0
- package/dist/server.js +1090 -0
- package/dist/server.mjs +1058 -0
- package/dist/services/buildService.d.ts +8 -0
- package/dist/services/cacheService.d.ts +15 -0
- package/dist/services/pnpmMaintenance.d.ts +4 -0
- package/dist/services/uploadService.d.ts +36 -0
- package/dist/template/re-shake-share/Collect.js +115 -0
- package/dist/template/re-shake-share/EmitManifest.js +49 -0
- package/dist/template/re-shake-share/index.ts +1 -0
- package/dist/template/re-shake-share/package.json +23 -0
- package/dist/template/re-shake-share/rspack.config.ts +33 -0
- package/dist/utils/runCommand.d.ts +20 -0
- package/dist/utils/runtimeEnv.d.ts +3 -0
- package/dist/utils/uploadSdk.d.ts +10 -0
- package/package.json +61 -0
package/dist/server.mjs
ADDED
|
@@ -0,0 +1,1058 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import node_fs from "node:fs";
|
|
3
|
+
import node_path from "node:path";
|
|
4
|
+
import pino from "pino";
|
|
5
|
+
import { Hono } from "hono";
|
|
6
|
+
import { cors } from "hono/cors";
|
|
7
|
+
import { zValidator } from "@hono/zod-validator";
|
|
8
|
+
import { nanoid } from "nanoid";
|
|
9
|
+
import { z } from "zod";
|
|
10
|
+
import { createHash } from "node:crypto";
|
|
11
|
+
import node_os from "node:os";
|
|
12
|
+
import { spawn } from "node:child_process";
|
|
13
|
+
import json_stable_stringify from "json-stable-stringify";
|
|
14
|
+
import { serve } from "@hono/node-server";
|
|
15
|
+
const SERVER_VERSION = 'v0-011501';
|
|
16
|
+
const UPLOADED_DIR = '_shared-tree-shaking';
|
|
17
|
+
const createLogger = (opts)=>pino({
|
|
18
|
+
level: (null == opts ? void 0 : opts.level) ?? 'info',
|
|
19
|
+
formatters: {
|
|
20
|
+
level (label) {
|
|
21
|
+
return {
|
|
22
|
+
level: label
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
let logger_logger = createLogger();
|
|
28
|
+
const setLogger = (next)=>{
|
|
29
|
+
logger_logger = next;
|
|
30
|
+
};
|
|
31
|
+
async function createAdapterDeps(params) {
|
|
32
|
+
const adapterId = params.adapterId;
|
|
33
|
+
const adapter = params.registry.getAdapterById(adapterId);
|
|
34
|
+
return adapter.create(params.adapterConfig ?? {}, {
|
|
35
|
+
logger: params.logger ?? logger_logger,
|
|
36
|
+
uploadedDir: params.uploadedDir ?? UPLOADED_DIR
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
function _define_property(obj, key, value) {
|
|
40
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
41
|
+
value: value,
|
|
42
|
+
enumerable: true,
|
|
43
|
+
configurable: true,
|
|
44
|
+
writable: true
|
|
45
|
+
});
|
|
46
|
+
else obj[key] = value;
|
|
47
|
+
return obj;
|
|
48
|
+
}
|
|
49
|
+
class LocalObjectStore {
|
|
50
|
+
async exists(key) {
|
|
51
|
+
const filePath = node_path.join(this.rootDir, key.replace(/^\//, ''));
|
|
52
|
+
try {
|
|
53
|
+
const stat = await node_fs.promises.stat(filePath);
|
|
54
|
+
return stat.isFile();
|
|
55
|
+
} catch {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
async uploadFile(localPath, key) {
|
|
60
|
+
const rel = key.replace(/^\//, '');
|
|
61
|
+
const dest = node_path.join(this.rootDir, rel);
|
|
62
|
+
await node_fs.promises.mkdir(node_path.dirname(dest), {
|
|
63
|
+
recursive: true
|
|
64
|
+
});
|
|
65
|
+
await node_fs.promises.copyFile(localPath, dest);
|
|
66
|
+
}
|
|
67
|
+
publicUrl(key) {
|
|
68
|
+
return `${this.publicBaseUrl}${key.replace(/^\//, '')}`;
|
|
69
|
+
}
|
|
70
|
+
constructor(opts){
|
|
71
|
+
_define_property(this, "rootDir", void 0);
|
|
72
|
+
_define_property(this, "publicBaseUrl", void 0);
|
|
73
|
+
this.rootDir = (null == opts ? void 0 : opts.rootDir) ?? node_path.join(process.cwd(), 'log', 'static');
|
|
74
|
+
const base = (null == opts ? void 0 : opts.publicBaseUrl) ?? '/';
|
|
75
|
+
this.publicBaseUrl = base.endsWith('/') ? base : `${base}/`;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function adapter_define_property(obj, key, value) {
|
|
79
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
80
|
+
value: value,
|
|
81
|
+
enumerable: true,
|
|
82
|
+
configurable: true,
|
|
83
|
+
writable: true
|
|
84
|
+
});
|
|
85
|
+
else obj[key] = value;
|
|
86
|
+
return obj;
|
|
87
|
+
}
|
|
88
|
+
function envStr(env, name, fallback) {
|
|
89
|
+
const v = env[name];
|
|
90
|
+
if (void 0 === v || '' === v) return fallback;
|
|
91
|
+
return v;
|
|
92
|
+
}
|
|
93
|
+
class LocalAdapter {
|
|
94
|
+
fromEnv(env) {
|
|
95
|
+
return {
|
|
96
|
+
rootDir: envStr(env, 'LOCAL_STORE_DIR', node_path.join(process.cwd(), 'log', 'static')),
|
|
97
|
+
publicBaseUrl: envStr(env, 'LOCAL_STORE_BASE_URL', '/')
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
async create(config, _context) {
|
|
101
|
+
const objectStore = new LocalObjectStore({
|
|
102
|
+
rootDir: config.rootDir,
|
|
103
|
+
publicBaseUrl: config.publicBaseUrl
|
|
104
|
+
});
|
|
105
|
+
return {
|
|
106
|
+
objectStore
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
constructor(){
|
|
110
|
+
adapter_define_property(this, "id", 'local');
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function createAdapterRegistry(adapters) {
|
|
114
|
+
return {
|
|
115
|
+
adapters,
|
|
116
|
+
getAdapterById: (id)=>{
|
|
117
|
+
const found = adapters.find((a)=>a.id === id);
|
|
118
|
+
if (!found) throw new Error(`Unknown ADAPTER_ID: ${id}`);
|
|
119
|
+
return found;
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
function createOssAdapterRegistry() {
|
|
124
|
+
return createAdapterRegistry([
|
|
125
|
+
new LocalAdapter()
|
|
126
|
+
]);
|
|
127
|
+
}
|
|
128
|
+
function contentTypeByExt(filePath) {
|
|
129
|
+
if (filePath.endsWith('.js')) return "application/javascript";
|
|
130
|
+
if (filePath.endsWith('.json')) return 'application/json';
|
|
131
|
+
return 'application/octet-stream';
|
|
132
|
+
}
|
|
133
|
+
const DEFAULT_STATIC_ROOT = node_path.join(process.cwd(), 'log', 'static');
|
|
134
|
+
async function serveLocalFile(c, rootDir) {
|
|
135
|
+
let requestPath = c.req.path;
|
|
136
|
+
try {
|
|
137
|
+
requestPath = decodeURIComponent(requestPath);
|
|
138
|
+
} catch {
|
|
139
|
+
return c.text('Not Found', 404);
|
|
140
|
+
}
|
|
141
|
+
const relPath = requestPath.replace(/^\/+/, '');
|
|
142
|
+
const rootResolved = node_path.resolve(rootDir);
|
|
143
|
+
const filePath = node_path.resolve(rootResolved, relPath);
|
|
144
|
+
if (filePath !== rootResolved && !filePath.startsWith(`${rootResolved}${node_path.sep}`)) return c.text('Not Found', 404);
|
|
145
|
+
try {
|
|
146
|
+
const buf = await node_fs.promises.readFile(filePath);
|
|
147
|
+
return new Response(buf, {
|
|
148
|
+
status: 200,
|
|
149
|
+
headers: {
|
|
150
|
+
'Content-Type': contentTypeByExt(filePath)
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
} catch {
|
|
154
|
+
return c.text('Not Found', 404);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
function createStaticRoute(opts) {
|
|
158
|
+
const staticRoute = new Hono();
|
|
159
|
+
const rootDir = (null == opts ? void 0 : opts.rootDir) ?? DEFAULT_STATIC_ROOT;
|
|
160
|
+
staticRoute.get('/tree-shaking-shared/*', async (c)=>serveLocalFile(c, rootDir));
|
|
161
|
+
return staticRoute;
|
|
162
|
+
}
|
|
163
|
+
const DEFAULT_PRUNE_INTERVAL_MS = 3600000;
|
|
164
|
+
function ossEnv_envStr(env, name, fallback) {
|
|
165
|
+
const v = env[name];
|
|
166
|
+
if (void 0 === v || '' === v) return fallback;
|
|
167
|
+
return v;
|
|
168
|
+
}
|
|
169
|
+
function envNum(env, name, fallback) {
|
|
170
|
+
const v = ossEnv_envStr(env, name);
|
|
171
|
+
if (!v) return fallback;
|
|
172
|
+
const n = Number(v);
|
|
173
|
+
return Number.isFinite(n) ? n : fallback;
|
|
174
|
+
}
|
|
175
|
+
function resolveOssEnv(params) {
|
|
176
|
+
const adapterId = ossEnv_envStr(params.env, 'ADAPTER_ID', params.defaultAdapterId) ?? 'local';
|
|
177
|
+
const adapter = params.registry.getAdapterById(adapterId);
|
|
178
|
+
const adapterConfig = adapter.fromEnv ? adapter.fromEnv(params.env) : {};
|
|
179
|
+
return {
|
|
180
|
+
adapterId,
|
|
181
|
+
adapterConfig,
|
|
182
|
+
corsOrigin: ossEnv_envStr(params.env, 'CORS_ORIGIN', '*') ?? '*',
|
|
183
|
+
staticRootDir: ossEnv_envStr(params.env, 'LOCAL_STORE_DIR', DEFAULT_STATIC_ROOT) ?? DEFAULT_STATIC_ROOT,
|
|
184
|
+
logLevel: ossEnv_envStr(params.env, 'LOG_LEVEL', 'info') ?? 'info',
|
|
185
|
+
port: envNum(params.env, 'PORT', 3000),
|
|
186
|
+
hostname: ossEnv_envStr(params.env, 'HOST', '0.0.0.0') ?? '0.0.0.0',
|
|
187
|
+
pruneIntervalMs: envNum(params.env, 'PNPM_PRUNE_INTERVAL_MS', DEFAULT_PRUNE_INTERVAL_MS),
|
|
188
|
+
runtimeEnv: params.env
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
function createDiMiddleware(deps) {
|
|
192
|
+
return async (c, next)=>{
|
|
193
|
+
c.set('objectStore', deps.objectStore);
|
|
194
|
+
if (deps.projectPublisher) c.set('projectPublisher', deps.projectPublisher);
|
|
195
|
+
await next();
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
const loggerMiddleware = async (c, next)=>{
|
|
199
|
+
const category = c.req.path.split('/')[1] || 'root';
|
|
200
|
+
const child = logger_logger.child({
|
|
201
|
+
category,
|
|
202
|
+
path: c.req.path,
|
|
203
|
+
method: c.req.method
|
|
204
|
+
});
|
|
205
|
+
c.set('logger', child);
|
|
206
|
+
await next();
|
|
207
|
+
};
|
|
208
|
+
const parseNormalizedKey = (key)=>{
|
|
209
|
+
const res = key.split('@');
|
|
210
|
+
return {
|
|
211
|
+
name: res.slice(0, -1).join('@'),
|
|
212
|
+
version: res[res.length - 1]
|
|
213
|
+
};
|
|
214
|
+
};
|
|
215
|
+
const normalizedKey = (name, v)=>`${name}@${v}`;
|
|
216
|
+
function normalizeConfig(config) {
|
|
217
|
+
const { shared, plugins, target, libraryType, hostName, uploadOptions } = config;
|
|
218
|
+
const commonNormalizedConfig = {
|
|
219
|
+
plugins: (null == plugins ? void 0 : plugins.sort(([a], [b])=>a.localeCompare(b))) ?? [],
|
|
220
|
+
target: Array.isArray(target) ? [
|
|
221
|
+
...target
|
|
222
|
+
].sort() : [
|
|
223
|
+
target
|
|
224
|
+
],
|
|
225
|
+
uploadOptions,
|
|
226
|
+
libraryType,
|
|
227
|
+
hostName
|
|
228
|
+
};
|
|
229
|
+
const normalizedConfig = {};
|
|
230
|
+
shared.forEach(([sharedName, version, usedExports], index)=>{
|
|
231
|
+
const key = normalizedKey(sharedName, version);
|
|
232
|
+
normalizedConfig[key] = {
|
|
233
|
+
...commonNormalizedConfig,
|
|
234
|
+
shared: shared.slice(0, index).concat(shared.slice(index + 1)).sort(([s, v, u], [b, v2, u2])=>`${s}${v}${u.sort().join('')}`.localeCompare(`${b}${v2}${u2.sort().join('')}`)),
|
|
235
|
+
usedExports
|
|
236
|
+
};
|
|
237
|
+
});
|
|
238
|
+
return normalizedConfig;
|
|
239
|
+
}
|
|
240
|
+
function extractBuildConfig(config, type) {
|
|
241
|
+
const { shared, plugins, target, libraryType, usedExports } = config;
|
|
242
|
+
return {
|
|
243
|
+
shared,
|
|
244
|
+
plugins,
|
|
245
|
+
target,
|
|
246
|
+
libraryType,
|
|
247
|
+
usedExports,
|
|
248
|
+
type
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
const UploadOptionsSchema = z.object({
|
|
252
|
+
scmName: z.string(),
|
|
253
|
+
bucketName: z.string(),
|
|
254
|
+
publicFilePath: z.string(),
|
|
255
|
+
publicPath: z.string(),
|
|
256
|
+
cdnRegion: z.string()
|
|
257
|
+
});
|
|
258
|
+
const BasicSchema = z.object({
|
|
259
|
+
shared: z.array(z.tuple([
|
|
260
|
+
z.string(),
|
|
261
|
+
z.string(),
|
|
262
|
+
z.array(z.string())
|
|
263
|
+
])).describe('List of plugins: [name, version, usedExports]'),
|
|
264
|
+
plugins: z.array(z.tuple([
|
|
265
|
+
z.string(),
|
|
266
|
+
z.string().optional()
|
|
267
|
+
])).describe('Specify extra build plugin names, support specify version in second item').optional(),
|
|
268
|
+
target: z.union([
|
|
269
|
+
z.string(),
|
|
270
|
+
z.array(z.string())
|
|
271
|
+
]).describe('Used to configure the target environment of Rspack output and the ECMAScript version of Rspack runtime code, the same with rspack#target'),
|
|
272
|
+
libraryType: z.string(),
|
|
273
|
+
hostName: z.string().describe('The name of the host app / mf')
|
|
274
|
+
});
|
|
275
|
+
const ConfigSchema = BasicSchema.extend({
|
|
276
|
+
uploadOptions: UploadOptionsSchema.optional()
|
|
277
|
+
});
|
|
278
|
+
const CheckTreeShakingSchema = ConfigSchema.extend({
|
|
279
|
+
uploadOptions: UploadOptionsSchema.optional()
|
|
280
|
+
});
|
|
281
|
+
const STATS_NAME = 'mf-stats.json';
|
|
282
|
+
let runtimeEnv = {};
|
|
283
|
+
const setRuntimeEnv = (env)=>{
|
|
284
|
+
runtimeEnv = env;
|
|
285
|
+
};
|
|
286
|
+
const getRuntimeEnv = ()=>runtimeEnv;
|
|
287
|
+
function runCommand_define_property(obj, key, value) {
|
|
288
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
289
|
+
value: value,
|
|
290
|
+
enumerable: true,
|
|
291
|
+
configurable: true,
|
|
292
|
+
writable: true
|
|
293
|
+
});
|
|
294
|
+
else obj[key] = value;
|
|
295
|
+
return obj;
|
|
296
|
+
}
|
|
297
|
+
class CommandExecutionError extends Error {
|
|
298
|
+
constructor(command, exitCode, stdout, stderr){
|
|
299
|
+
super(`Command "${command}" exited with code ${exitCode}`), runCommand_define_property(this, "command", void 0), runCommand_define_property(this, "exitCode", void 0), runCommand_define_property(this, "stdout", void 0), runCommand_define_property(this, "stderr", void 0);
|
|
300
|
+
this.name = 'CommandExecutionError';
|
|
301
|
+
this.command = command;
|
|
302
|
+
this.exitCode = exitCode;
|
|
303
|
+
this.stdout = stdout;
|
|
304
|
+
this.stderr = stderr;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
const runCommand = (command, options = {})=>new Promise((resolve, reject)=>{
|
|
308
|
+
const stdoutChunks = [];
|
|
309
|
+
const stderrChunks = [];
|
|
310
|
+
const child = spawn(command, {
|
|
311
|
+
cwd: options.cwd,
|
|
312
|
+
env: {
|
|
313
|
+
...getRuntimeEnv(),
|
|
314
|
+
...options.env
|
|
315
|
+
},
|
|
316
|
+
shell: true,
|
|
317
|
+
stdio: [
|
|
318
|
+
'ignore',
|
|
319
|
+
'pipe',
|
|
320
|
+
'pipe'
|
|
321
|
+
]
|
|
322
|
+
});
|
|
323
|
+
child.stdout.on('data', (chunk)=>{
|
|
324
|
+
stdoutChunks.push(chunk.toString());
|
|
325
|
+
});
|
|
326
|
+
child.stderr.on('data', (chunk)=>{
|
|
327
|
+
stderrChunks.push(chunk.toString());
|
|
328
|
+
});
|
|
329
|
+
child.on('error', (error)=>{
|
|
330
|
+
reject(error);
|
|
331
|
+
});
|
|
332
|
+
child.on('close', (exitCode)=>{
|
|
333
|
+
const stdout = stdoutChunks.join('');
|
|
334
|
+
const stderr = stderrChunks.join('');
|
|
335
|
+
if (0 === exitCode) return void resolve({
|
|
336
|
+
stdout,
|
|
337
|
+
stderr,
|
|
338
|
+
exitCode
|
|
339
|
+
});
|
|
340
|
+
reject(new CommandExecutionError(command, exitCode ?? -1, stdout, stderr));
|
|
341
|
+
});
|
|
342
|
+
});
|
|
343
|
+
let installs = 0;
|
|
344
|
+
let pruneScheduled = false;
|
|
345
|
+
let pruning = false;
|
|
346
|
+
const DEFAULT_INTERVAL = 3600000;
|
|
347
|
+
const markInstallStart = ()=>{
|
|
348
|
+
installs++;
|
|
349
|
+
};
|
|
350
|
+
const markInstallEnd = ()=>{
|
|
351
|
+
installs = Math.max(0, installs - 1);
|
|
352
|
+
schedulePruneSoon();
|
|
353
|
+
};
|
|
354
|
+
const schedulePruneSoon = (delay = 30000)=>{
|
|
355
|
+
if (pruneScheduled) return;
|
|
356
|
+
pruneScheduled = true;
|
|
357
|
+
setTimeout(()=>{
|
|
358
|
+
pruneScheduled = false;
|
|
359
|
+
maybePrune();
|
|
360
|
+
}, delay);
|
|
361
|
+
};
|
|
362
|
+
const maybePrune = async ()=>{
|
|
363
|
+
if (pruning || installs > 0) return void schedulePruneSoon(60000);
|
|
364
|
+
pruning = true;
|
|
365
|
+
try {
|
|
366
|
+
await runCommand('pnpm store prune');
|
|
367
|
+
} catch {} finally{
|
|
368
|
+
pruning = false;
|
|
369
|
+
}
|
|
370
|
+
};
|
|
371
|
+
const startPeriodicPrune = (intervalMs = DEFAULT_INTERVAL)=>{
|
|
372
|
+
if (intervalMs <= 0) return;
|
|
373
|
+
setInterval(()=>{
|
|
374
|
+
maybePrune();
|
|
375
|
+
}, intervalMs);
|
|
376
|
+
};
|
|
377
|
+
const createUniqueTempDirByKey = (key)=>{
|
|
378
|
+
const base = node_path.join(node_os.tmpdir(), `re-shake-share-${key}`);
|
|
379
|
+
let candidate = base;
|
|
380
|
+
for(;;)try {
|
|
381
|
+
node_fs.mkdirSync(candidate, {
|
|
382
|
+
recursive: false
|
|
383
|
+
});
|
|
384
|
+
return candidate;
|
|
385
|
+
} catch {
|
|
386
|
+
const rand = Math.floor(1e9 * Math.random());
|
|
387
|
+
candidate = `${base}-${rand}`;
|
|
388
|
+
}
|
|
389
|
+
};
|
|
390
|
+
const prepareProject = (config, excludeShared)=>{
|
|
391
|
+
const key = createHash('sha256').update(JSON.stringify(config)).digest('hex');
|
|
392
|
+
const dir = createUniqueTempDirByKey(key);
|
|
393
|
+
const templateDir = node_path.join(__dirname, '..', 'template', 're-shake-share');
|
|
394
|
+
node_fs.cpSync(templateDir, dir, {
|
|
395
|
+
recursive: true
|
|
396
|
+
});
|
|
397
|
+
const pkgPath = node_path.join(dir, 'package.json');
|
|
398
|
+
const pkg = JSON.parse(node_fs.readFileSync(pkgPath, 'utf-8'));
|
|
399
|
+
const deps = {
|
|
400
|
+
...pkg.dependencies || {}
|
|
401
|
+
};
|
|
402
|
+
const shared = {};
|
|
403
|
+
const mfConfig = {
|
|
404
|
+
name: 're_shake',
|
|
405
|
+
library: {
|
|
406
|
+
type: 'global'
|
|
407
|
+
},
|
|
408
|
+
manifest: true,
|
|
409
|
+
shared
|
|
410
|
+
};
|
|
411
|
+
let pluginImportStr = '';
|
|
412
|
+
let pluginOptionStr = '[\n';
|
|
413
|
+
let sharedImport = '';
|
|
414
|
+
Object.entries(config).forEach(([key, { plugins = [], libraryType, usedExports, hostName }], index)=>{
|
|
415
|
+
const { name, version } = parseNormalizedKey(key);
|
|
416
|
+
deps[name] = version;
|
|
417
|
+
if (!index) {
|
|
418
|
+
plugins.forEach(([pluginName, pluginVersion], pIndex)=>{
|
|
419
|
+
deps[pluginName] = pluginVersion ?? 'latest';
|
|
420
|
+
const pluginImportName = `plugin_${pIndex}`;
|
|
421
|
+
pluginImportStr += `import ${pluginImportName} from '${pluginName}';\n`;
|
|
422
|
+
pluginOptionStr += `new ${pluginImportName}(),`;
|
|
423
|
+
});
|
|
424
|
+
mfConfig.library.type = libraryType;
|
|
425
|
+
mfConfig.name = hostName;
|
|
426
|
+
}
|
|
427
|
+
shared[name] = {
|
|
428
|
+
requiredVersion: version,
|
|
429
|
+
version,
|
|
430
|
+
treeShaking: excludeShared.some(([n, v])=>n === name && v === version) ? void 0 : {
|
|
431
|
+
usedExports,
|
|
432
|
+
mode: 'server-calc'
|
|
433
|
+
}
|
|
434
|
+
};
|
|
435
|
+
sharedImport += `import shared_${index} from '${name}';\n`;
|
|
436
|
+
});
|
|
437
|
+
pluginOptionStr += '\n]';
|
|
438
|
+
pkg.dependencies = deps;
|
|
439
|
+
node_fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2));
|
|
440
|
+
const sharedImportPlaceholder = "${SHARED_IMPORT}";
|
|
441
|
+
const pluginsPlaceholder = "${ PLUGINS }";
|
|
442
|
+
const mfConfigPlaceholder = "${ MF_CONFIG }";
|
|
443
|
+
const indexPath = node_path.join(dir, 'index.ts');
|
|
444
|
+
const indexContent = node_fs.readFileSync(indexPath, 'utf-8');
|
|
445
|
+
node_fs.writeFileSync(indexPath, indexContent.replace(sharedImportPlaceholder, sharedImport));
|
|
446
|
+
const rspackConfigPath = node_path.join(dir, 'rspack.config.ts');
|
|
447
|
+
let cfg = node_fs.readFileSync(rspackConfigPath, 'utf-8');
|
|
448
|
+
cfg += pluginImportStr;
|
|
449
|
+
cfg = cfg.replace(pluginsPlaceholder, pluginOptionStr);
|
|
450
|
+
cfg = cfg.replace(mfConfigPlaceholder, JSON.stringify(mfConfig, null, 2));
|
|
451
|
+
node_fs.writeFileSync(rspackConfigPath, cfg);
|
|
452
|
+
return dir;
|
|
453
|
+
};
|
|
454
|
+
const installDependencies = async (cwd)=>{
|
|
455
|
+
markInstallStart();
|
|
456
|
+
try {
|
|
457
|
+
await runCommand('pnpm i', {
|
|
458
|
+
cwd,
|
|
459
|
+
env: {
|
|
460
|
+
npm_config_registry: 'https://registry.npmjs.org/'
|
|
461
|
+
}
|
|
462
|
+
});
|
|
463
|
+
} finally{
|
|
464
|
+
markInstallEnd();
|
|
465
|
+
}
|
|
466
|
+
};
|
|
467
|
+
const buildProject = async (cwd, type)=>{
|
|
468
|
+
const scripts = [];
|
|
469
|
+
if ('re-shake' === type) scripts.push('pnpm build');
|
|
470
|
+
else scripts.push('pnpm build:full');
|
|
471
|
+
await Promise.all(scripts.map((script)=>runCommand(script, {
|
|
472
|
+
cwd
|
|
473
|
+
})));
|
|
474
|
+
};
|
|
475
|
+
const retrieveSharedFilepaths = (projectDir, type)=>{
|
|
476
|
+
const sharedFilepaths = [];
|
|
477
|
+
const collectSharedFilepaths = (t)=>{
|
|
478
|
+
const dir = 'full' === t ? 'full-shared' : 'dist';
|
|
479
|
+
const distDir = node_path.join(projectDir, dir);
|
|
480
|
+
const stats = JSON.parse(node_fs.readFileSync(node_path.join(distDir, STATS_NAME), 'utf-8'));
|
|
481
|
+
stats.shared.forEach((s)=>{
|
|
482
|
+
const { name, version, fallback, fallbackName } = s;
|
|
483
|
+
if (fallback && fallbackName) {
|
|
484
|
+
var _s_usedExports;
|
|
485
|
+
const filepath = node_path.join(distDir, fallback);
|
|
486
|
+
sharedFilepaths.push({
|
|
487
|
+
name,
|
|
488
|
+
version,
|
|
489
|
+
filepath,
|
|
490
|
+
globalName: fallbackName,
|
|
491
|
+
type: t,
|
|
492
|
+
modules: s.usedExports,
|
|
493
|
+
canTreeShaking: s.canTreeShaking ?? (null == (_s_usedExports = s.usedExports) ? void 0 : _s_usedExports.length) !== 0
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
});
|
|
497
|
+
};
|
|
498
|
+
collectSharedFilepaths(type);
|
|
499
|
+
return sharedFilepaths;
|
|
500
|
+
};
|
|
501
|
+
const runBuild = async (normalizedConfig, excludeShared, type)=>{
|
|
502
|
+
const tmpDir = prepareProject(normalizedConfig, excludeShared);
|
|
503
|
+
await installDependencies(tmpDir);
|
|
504
|
+
await buildProject(tmpDir, type);
|
|
505
|
+
const sharedFilePaths = retrieveSharedFilepaths(tmpDir, type);
|
|
506
|
+
return {
|
|
507
|
+
sharedFilePaths,
|
|
508
|
+
dir: tmpDir
|
|
509
|
+
};
|
|
510
|
+
};
|
|
511
|
+
function cleanUp(tmpDir) {
|
|
512
|
+
if (!tmpDir) return;
|
|
513
|
+
node_fs.rmSync(tmpDir, {
|
|
514
|
+
recursive: true,
|
|
515
|
+
force: true
|
|
516
|
+
});
|
|
517
|
+
}
|
|
518
|
+
const encodeName = function(name, prefix = '', withExt = false) {
|
|
519
|
+
const ext = withExt ? '.js' : '';
|
|
520
|
+
return `${prefix}${name.replace(/@/g, 'scope_').replace(/-/g, '_').replace(/\//g, '__').replace(/\./g, '')}${ext}`;
|
|
521
|
+
};
|
|
522
|
+
function retrieveGlobalName(mfName, sharedName, version) {
|
|
523
|
+
return encodeName(`${mfName}_${sharedName}_${version}`);
|
|
524
|
+
}
|
|
525
|
+
function createCacheHash(config, type) {
|
|
526
|
+
const relevant = extractBuildConfig({
|
|
527
|
+
...config,
|
|
528
|
+
usedExports: 'full' === type ? [] : config.usedExports
|
|
529
|
+
}, type);
|
|
530
|
+
const json = json_stable_stringify(relevant);
|
|
531
|
+
if (!json) throw new Error('Can not stringify build config!');
|
|
532
|
+
return createHash('sha256').update(json).digest('hex');
|
|
533
|
+
}
|
|
534
|
+
function retrieveCDNPath({ config, sharedKey, type }) {
|
|
535
|
+
const configHash = createCacheHash(config, type);
|
|
536
|
+
return `tree-shaking-shared/${SERVER_VERSION}/${sharedKey}/${configHash}.js`;
|
|
537
|
+
}
|
|
538
|
+
async function cacheService_hitCache(sharedKey, config, type, store) {
|
|
539
|
+
const cdnPath = retrieveCDNPath({
|
|
540
|
+
config,
|
|
541
|
+
sharedKey,
|
|
542
|
+
type
|
|
543
|
+
});
|
|
544
|
+
const exists = await store.exists(cdnPath);
|
|
545
|
+
return exists ? store.publicUrl(cdnPath) : null;
|
|
546
|
+
}
|
|
547
|
+
const retrieveCacheItems = async (normalizedConfig, type, store)=>{
|
|
548
|
+
const cacheItems = [];
|
|
549
|
+
const restConfig = {};
|
|
550
|
+
const excludeShared = [];
|
|
551
|
+
for (const [sharedKey, config] of Object.entries(normalizedConfig)){
|
|
552
|
+
let cache = false;
|
|
553
|
+
const { name, version } = parseNormalizedKey(sharedKey);
|
|
554
|
+
const cdnUrl = await cacheService_hitCache(sharedKey, config, type, store);
|
|
555
|
+
if (cdnUrl) {
|
|
556
|
+
cache = true;
|
|
557
|
+
cacheItems.push({
|
|
558
|
+
type,
|
|
559
|
+
name,
|
|
560
|
+
version,
|
|
561
|
+
cdnUrl,
|
|
562
|
+
globalName: retrieveGlobalName(config.hostName, name, version)
|
|
563
|
+
});
|
|
564
|
+
}
|
|
565
|
+
if (cache) excludeShared.push([
|
|
566
|
+
name,
|
|
567
|
+
version
|
|
568
|
+
]);
|
|
569
|
+
else if (config.usedExports.length || 're-shake' !== type) restConfig[sharedKey] = config;
|
|
570
|
+
else excludeShared.push([
|
|
571
|
+
name,
|
|
572
|
+
version
|
|
573
|
+
]);
|
|
574
|
+
}
|
|
575
|
+
return {
|
|
576
|
+
cacheItems,
|
|
577
|
+
excludeShared,
|
|
578
|
+
restConfig
|
|
579
|
+
};
|
|
580
|
+
};
|
|
581
|
+
async function uploadToCacheStore(sharedFilePaths, normalizedConfig, store) {
|
|
582
|
+
const results = [];
|
|
583
|
+
for (const file of sharedFilePaths){
|
|
584
|
+
const { name, version, filepath, globalName, type, modules, canTreeShaking } = file;
|
|
585
|
+
try {
|
|
586
|
+
const sharedKey = normalizedKey(name, version);
|
|
587
|
+
logger_logger.info(`Uploading ${sharedKey} to CDN`);
|
|
588
|
+
const config = normalizedConfig[sharedKey];
|
|
589
|
+
const cdnPath = retrieveCDNPath({
|
|
590
|
+
config,
|
|
591
|
+
sharedKey,
|
|
592
|
+
type
|
|
593
|
+
});
|
|
594
|
+
const t0 = Date.now();
|
|
595
|
+
await store.uploadFile(filepath, cdnPath);
|
|
596
|
+
const tUpload = Date.now() - t0;
|
|
597
|
+
const cdnUrl = store.publicUrl(cdnPath);
|
|
598
|
+
const res = {
|
|
599
|
+
name: name,
|
|
600
|
+
version: version,
|
|
601
|
+
globalName: globalName,
|
|
602
|
+
cdnUrl,
|
|
603
|
+
type,
|
|
604
|
+
modules,
|
|
605
|
+
canTreeShaking
|
|
606
|
+
};
|
|
607
|
+
try {
|
|
608
|
+
const jsonFilePath = filepath.replace(/\.js$/, '.json');
|
|
609
|
+
const jsonFile = JSON.stringify(res);
|
|
610
|
+
const jsonCdnUrl = cdnPath.replace(/\.js$/, '.json');
|
|
611
|
+
node_fs.writeFileSync(jsonFilePath, jsonFile);
|
|
612
|
+
await store.uploadFile(jsonFilePath, jsonCdnUrl);
|
|
613
|
+
} catch (error) {
|
|
614
|
+
logger_logger.error(`Failed to upload ${name}@${version} json file: ${error}`);
|
|
615
|
+
}
|
|
616
|
+
results.push(res);
|
|
617
|
+
logger_logger.info(`Successfully uploaded ${name}@${version} to ${cdnUrl} in ${tUpload}ms`);
|
|
618
|
+
} catch (error) {
|
|
619
|
+
logger_logger.error(`Failed to upload ${name}@${version}: ${error}`);
|
|
620
|
+
throw error;
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
return results;
|
|
624
|
+
}
|
|
625
|
+
const downloadToFile = async (url, destPath)=>{
|
|
626
|
+
const res = await fetch(url);
|
|
627
|
+
if (!res.ok) throw new Error(`Download failed: ${res.status} ${res.statusText} - ${url}`);
|
|
628
|
+
const buf = Buffer.from(await res.arrayBuffer());
|
|
629
|
+
await node_fs.promises.mkdir(node_path.dirname(destPath), {
|
|
630
|
+
recursive: true
|
|
631
|
+
});
|
|
632
|
+
await node_fs.promises.writeFile(destPath, buf);
|
|
633
|
+
return destPath;
|
|
634
|
+
};
|
|
635
|
+
async function uploadProject(uploadResults, sharedFilePaths, normalizedConfig, publisher) {
|
|
636
|
+
const tmpDir = createUniqueTempDirByKey(`upload-project${Date.now().toString()}`);
|
|
637
|
+
const uploaded = [];
|
|
638
|
+
try {
|
|
639
|
+
for (const item of uploadResults)try {
|
|
640
|
+
const config = normalizedConfig[normalizedKey(item.name, item.version)];
|
|
641
|
+
if (!config) {
|
|
642
|
+
logger_logger.error(`No config found for ${item.name}`);
|
|
643
|
+
continue;
|
|
644
|
+
}
|
|
645
|
+
const { uploadOptions } = config;
|
|
646
|
+
if (!uploadOptions) throw new Error(`No uploadOptions found for ${item.name}`);
|
|
647
|
+
const filename = node_path.basename(new URL(item.cdnUrl).pathname);
|
|
648
|
+
const localPath = node_path.join(tmpDir, filename);
|
|
649
|
+
const hash = createCacheHash({
|
|
650
|
+
...config,
|
|
651
|
+
plugins: config.plugins || []
|
|
652
|
+
}, item.type);
|
|
653
|
+
const cdnUrl = publisher.publicUrl({
|
|
654
|
+
sharedName: item.name,
|
|
655
|
+
hash,
|
|
656
|
+
fileName: filename,
|
|
657
|
+
options: uploadOptions
|
|
658
|
+
});
|
|
659
|
+
const hitCache = await publisher.exists(cdnUrl);
|
|
660
|
+
if (hitCache) {
|
|
661
|
+
logger_logger.info(`Hit cache for ${item.name}@${item.version} -> ${cdnUrl}`);
|
|
662
|
+
uploaded.push({
|
|
663
|
+
...item,
|
|
664
|
+
cdnUrl
|
|
665
|
+
});
|
|
666
|
+
continue;
|
|
667
|
+
}
|
|
668
|
+
logger_logger.info(`Downloading ${item.name}@${item.version} -> ${localPath}`);
|
|
669
|
+
const t0 = Date.now();
|
|
670
|
+
await downloadToFile(item.cdnUrl, localPath);
|
|
671
|
+
const tDownload = Date.now() - t0;
|
|
672
|
+
logger_logger.info(`Downloaded ${item.name}@${item.version} in ${tDownload}ms`);
|
|
673
|
+
const newCdnUrl = await publisher.publishFile({
|
|
674
|
+
localPath,
|
|
675
|
+
sharedName: item.name,
|
|
676
|
+
hash,
|
|
677
|
+
options: uploadOptions
|
|
678
|
+
});
|
|
679
|
+
uploaded.push({
|
|
680
|
+
...item,
|
|
681
|
+
cdnUrl: newCdnUrl
|
|
682
|
+
});
|
|
683
|
+
} catch (error) {
|
|
684
|
+
logger_logger.error(`Failed to upload ${item.name}@${item.version}: ${error}`);
|
|
685
|
+
}
|
|
686
|
+
for (const s of sharedFilePaths)try {
|
|
687
|
+
const config = normalizedConfig[normalizedKey(s.name, s.version)];
|
|
688
|
+
if (!config) {
|
|
689
|
+
logger_logger.error(`No config found for ${s.name}`);
|
|
690
|
+
continue;
|
|
691
|
+
}
|
|
692
|
+
const { uploadOptions } = config;
|
|
693
|
+
if (!uploadOptions) throw new Error(`No uploadOptions found for ${s.name}`);
|
|
694
|
+
const hash = createCacheHash({
|
|
695
|
+
...config,
|
|
696
|
+
plugins: config.plugins || []
|
|
697
|
+
}, s.type);
|
|
698
|
+
const cdnUrl = await publisher.publishFile({
|
|
699
|
+
localPath: s.filepath,
|
|
700
|
+
sharedName: s.name,
|
|
701
|
+
hash,
|
|
702
|
+
options: uploadOptions
|
|
703
|
+
});
|
|
704
|
+
uploaded.push({
|
|
705
|
+
name: s.name,
|
|
706
|
+
version: s.version,
|
|
707
|
+
globalName: s.globalName,
|
|
708
|
+
cdnUrl,
|
|
709
|
+
type: s.type
|
|
710
|
+
});
|
|
711
|
+
} catch (error) {
|
|
712
|
+
logger_logger.error(`Failed to upload ${s.name}@${s.version}: ${error}`);
|
|
713
|
+
}
|
|
714
|
+
return uploaded;
|
|
715
|
+
} finally{
|
|
716
|
+
node_fs.rmSync(tmpDir, {
|
|
717
|
+
recursive: true,
|
|
718
|
+
force: true
|
|
719
|
+
});
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
async function upload(sharedFilePaths, uploadResults, normalizedConfig, uploadOptions, store, publisher) {
|
|
723
|
+
const cacheUploaded = await uploadToCacheStore(sharedFilePaths, normalizedConfig, store);
|
|
724
|
+
if (!uploadOptions) {
|
|
725
|
+
const hydrated = await Promise.all(uploadResults.map(async (item)=>{
|
|
726
|
+
if ('full' !== item.type) return item;
|
|
727
|
+
const tmpDir = createUniqueTempDirByKey(`download-full-json${Date.now().toString()}`);
|
|
728
|
+
const jsonPath = node_path.join(tmpDir, `${item.name}-${item.version}.json`);
|
|
729
|
+
try {
|
|
730
|
+
const tJson0 = Date.now();
|
|
731
|
+
await downloadToFile(item.cdnUrl.replace('.js', '.json'), jsonPath);
|
|
732
|
+
const tJson = Date.now() - tJson0;
|
|
733
|
+
logger_logger.info(`Downloaded ${item.name}@${item.version} json in ${tJson}ms`);
|
|
734
|
+
const jsonContent = JSON.parse(node_fs.readFileSync(jsonPath, 'utf8'));
|
|
735
|
+
return {
|
|
736
|
+
...item,
|
|
737
|
+
canTreeShaking: jsonContent.canTreeShaking,
|
|
738
|
+
modules: jsonContent.modules
|
|
739
|
+
};
|
|
740
|
+
} catch (jsonError) {
|
|
741
|
+
logger_logger.error(`Failed to download ${item.name}@${item.version} json: ${jsonError}`);
|
|
742
|
+
return {
|
|
743
|
+
...item,
|
|
744
|
+
canTreeShaking: item.canTreeShaking ?? true
|
|
745
|
+
};
|
|
746
|
+
} finally{
|
|
747
|
+
node_fs.rmSync(tmpDir, {
|
|
748
|
+
recursive: true,
|
|
749
|
+
force: true
|
|
750
|
+
});
|
|
751
|
+
}
|
|
752
|
+
}));
|
|
753
|
+
return [
|
|
754
|
+
...cacheUploaded,
|
|
755
|
+
...hydrated
|
|
756
|
+
];
|
|
757
|
+
}
|
|
758
|
+
if (!publisher) throw new Error('uploadOptions provided but no projectPublisher configured (configure the selected adapter to enable it or omit uploadOptions)');
|
|
759
|
+
const projectUploadResults = await uploadProject(uploadResults, sharedFilePaths, normalizedConfig, publisher);
|
|
760
|
+
return projectUploadResults;
|
|
761
|
+
}
|
|
762
|
+
const buildRoute = new Hono();
|
|
763
|
+
buildRoute.post('/', zValidator('json', ConfigSchema), async (c)=>{
|
|
764
|
+
const logger = c.get('logger');
|
|
765
|
+
const startTime = Date.now();
|
|
766
|
+
const jobId = nanoid();
|
|
767
|
+
logger.info(jobId);
|
|
768
|
+
const body = c.req.valid('json');
|
|
769
|
+
logger.info(JSON.stringify(body));
|
|
770
|
+
const normalizedConfig = normalizeConfig(body);
|
|
771
|
+
const store = c.get('objectStore');
|
|
772
|
+
const publisher = c.get('projectPublisher');
|
|
773
|
+
try {
|
|
774
|
+
const { cacheItems, excludeShared, restConfig } = await retrieveCacheItems(normalizedConfig, 're-shake', store);
|
|
775
|
+
let sharedFilePaths = [];
|
|
776
|
+
let dir;
|
|
777
|
+
if (Object.keys(restConfig).length > 0) {
|
|
778
|
+
const buildResult = await runBuild(normalizedConfig, excludeShared, 're-shake');
|
|
779
|
+
sharedFilePaths = buildResult.sharedFilePaths;
|
|
780
|
+
dir = buildResult.dir;
|
|
781
|
+
}
|
|
782
|
+
const uploadResults = await upload(sharedFilePaths, cacheItems, normalizedConfig, body.uploadOptions, store, publisher);
|
|
783
|
+
cleanUp(dir);
|
|
784
|
+
return c.json({
|
|
785
|
+
jobId,
|
|
786
|
+
status: 'success',
|
|
787
|
+
data: uploadResults,
|
|
788
|
+
cached: cacheItems,
|
|
789
|
+
duration: Date.now() - startTime
|
|
790
|
+
});
|
|
791
|
+
} catch (error) {
|
|
792
|
+
if (error instanceof CommandExecutionError) {
|
|
793
|
+
if (137 === error.exitCode) maybePrune();
|
|
794
|
+
return c.json({
|
|
795
|
+
jobId,
|
|
796
|
+
status: 'failed',
|
|
797
|
+
error: error.message,
|
|
798
|
+
command: error.command,
|
|
799
|
+
exitCode: error.exitCode,
|
|
800
|
+
stdout: error.stdout,
|
|
801
|
+
stderr: error.stderr
|
|
802
|
+
});
|
|
803
|
+
}
|
|
804
|
+
return c.json({
|
|
805
|
+
jobId,
|
|
806
|
+
status: 'failed',
|
|
807
|
+
error: error instanceof Error ? error.message : 'Unknown error'
|
|
808
|
+
});
|
|
809
|
+
}
|
|
810
|
+
});
|
|
811
|
+
async function handleCheckTreeshake(c, body) {
|
|
812
|
+
const logger = c.get('logger');
|
|
813
|
+
const startTime = Date.now();
|
|
814
|
+
const jobId = nanoid();
|
|
815
|
+
logger.info(jobId);
|
|
816
|
+
logger.info(JSON.stringify(body));
|
|
817
|
+
const normalizedConfig = normalizeConfig(body);
|
|
818
|
+
const store = c.get('objectStore');
|
|
819
|
+
const publisher = c.get('projectPublisher');
|
|
820
|
+
try {
|
|
821
|
+
const { cacheItems, excludeShared, restConfig } = await retrieveCacheItems(normalizedConfig, 'full', store);
|
|
822
|
+
let sharedFilePaths = [];
|
|
823
|
+
let dir;
|
|
824
|
+
if (Object.keys(restConfig).length > 0) {
|
|
825
|
+
const buildResult = await runBuild(normalizedConfig, excludeShared, 'full');
|
|
826
|
+
sharedFilePaths = buildResult.sharedFilePaths;
|
|
827
|
+
dir = buildResult.dir;
|
|
828
|
+
}
|
|
829
|
+
const uploadResults = await upload(sharedFilePaths, cacheItems, normalizedConfig, body.uploadOptions, store, publisher);
|
|
830
|
+
cleanUp(dir);
|
|
831
|
+
return c.json({
|
|
832
|
+
jobId,
|
|
833
|
+
status: 'success',
|
|
834
|
+
data: uploadResults,
|
|
835
|
+
cached: cacheItems,
|
|
836
|
+
duration: Date.now() - startTime
|
|
837
|
+
});
|
|
838
|
+
} catch (error) {
|
|
839
|
+
if (error instanceof CommandExecutionError) {
|
|
840
|
+
if (137 === error.exitCode) maybePrune();
|
|
841
|
+
return c.json({
|
|
842
|
+
jobId,
|
|
843
|
+
status: 'failed',
|
|
844
|
+
error: error.message,
|
|
845
|
+
command: error.command,
|
|
846
|
+
exitCode: error.exitCode,
|
|
847
|
+
stdout: error.stdout,
|
|
848
|
+
stderr: error.stderr
|
|
849
|
+
});
|
|
850
|
+
}
|
|
851
|
+
return c.json({
|
|
852
|
+
jobId,
|
|
853
|
+
status: 'failed',
|
|
854
|
+
error: error instanceof Error ? error.message : 'Unknown error'
|
|
855
|
+
});
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
buildRoute.post('/check-tree-shaking', zValidator('json', CheckTreeShakingSchema), async (c)=>handleCheckTreeshake(c, c.req.valid('json')));
|
|
859
|
+
const maintenanceRoute = new Hono();
|
|
860
|
+
maintenanceRoute.post('/', async (c)=>{
|
|
861
|
+
const logger = c.get('logger');
|
|
862
|
+
const jobId = nanoid();
|
|
863
|
+
const startTime = Date.now();
|
|
864
|
+
logger.info(jobId);
|
|
865
|
+
await maybePrune();
|
|
866
|
+
return c.json({
|
|
867
|
+
jobId,
|
|
868
|
+
status: 'success',
|
|
869
|
+
duration: Date.now() - startTime
|
|
870
|
+
});
|
|
871
|
+
});
|
|
872
|
+
const routes = new Hono();
|
|
873
|
+
routes.route('/build', buildRoute);
|
|
874
|
+
routes.route('/clean-cache', maintenanceRoute);
|
|
875
|
+
function createApp(deps, opts) {
|
|
876
|
+
var _opts_appExtensions, _opts_frontendAdapters;
|
|
877
|
+
if (null == opts ? void 0 : opts.logger) setLogger(opts.logger);
|
|
878
|
+
setRuntimeEnv((null == opts ? void 0 : opts.runtimeEnv) ?? process.env);
|
|
879
|
+
const app = new Hono();
|
|
880
|
+
const corsOrigin = (null == opts ? void 0 : opts.corsOrigin) ?? '*';
|
|
881
|
+
const staticRootDir = (null == opts ? void 0 : opts.staticRootDir) ?? DEFAULT_STATIC_ROOT;
|
|
882
|
+
app.use('*', cors({
|
|
883
|
+
origin: corsOrigin,
|
|
884
|
+
allowMethods: [
|
|
885
|
+
'GET',
|
|
886
|
+
'POST',
|
|
887
|
+
'OPTIONS'
|
|
888
|
+
],
|
|
889
|
+
allowHeaders: [
|
|
890
|
+
'Content-Type',
|
|
891
|
+
'Authorization'
|
|
892
|
+
]
|
|
893
|
+
}));
|
|
894
|
+
app.use('*', loggerMiddleware);
|
|
895
|
+
app.use('*', createDiMiddleware(deps));
|
|
896
|
+
if (null == opts ? void 0 : null == (_opts_appExtensions = opts.appExtensions) ? void 0 : _opts_appExtensions.length) for (const extend of opts.appExtensions)extend(app);
|
|
897
|
+
app.get('/tree-shaking-shared/healthz', (c)=>c.json({
|
|
898
|
+
status: 'ok',
|
|
899
|
+
timestamp: new Date().toISOString()
|
|
900
|
+
}));
|
|
901
|
+
app.route('/tree-shaking-shared', routes);
|
|
902
|
+
app.route('/', createStaticRoute({
|
|
903
|
+
rootDir: staticRootDir
|
|
904
|
+
}));
|
|
905
|
+
if (null == opts ? void 0 : null == (_opts_frontendAdapters = opts.frontendAdapters) ? void 0 : _opts_frontendAdapters.length) for (const adapter of opts.frontendAdapters)adapter.register(app);
|
|
906
|
+
startPeriodicPrune(null == opts ? void 0 : opts.pruneIntervalMs);
|
|
907
|
+
return app;
|
|
908
|
+
}
|
|
909
|
+
const defaultBasePath = '/tree-shaking';
|
|
910
|
+
const embeddedAdapter_contentTypeByExt = (filePath)=>{
|
|
911
|
+
if (filePath.endsWith('.html')) return 'text/html; charset=utf-8';
|
|
912
|
+
if (filePath.endsWith('.js')) return "application/javascript";
|
|
913
|
+
if (filePath.endsWith('.css')) return 'text/css';
|
|
914
|
+
if (filePath.endsWith('.json')) return 'application/json';
|
|
915
|
+
if (filePath.endsWith('.svg')) return 'image/svg+xml';
|
|
916
|
+
if (filePath.endsWith('.png')) return 'image/png';
|
|
917
|
+
if (filePath.endsWith('.jpg') || filePath.endsWith('.jpeg')) return 'image/jpeg';
|
|
918
|
+
if (filePath.endsWith('.webp')) return 'image/webp';
|
|
919
|
+
if (filePath.endsWith('.ico')) return 'image/x-icon';
|
|
920
|
+
return 'application/octet-stream';
|
|
921
|
+
};
|
|
922
|
+
const safeResolve = (rootDir, requestPath)=>{
|
|
923
|
+
const rootResolved = node_path.resolve(rootDir);
|
|
924
|
+
const rel = requestPath.replace(/^\/+/, '');
|
|
925
|
+
const filePath = node_path.resolve(rootResolved, rel);
|
|
926
|
+
if (filePath !== rootResolved && !filePath.startsWith(`${rootResolved}${node_path.sep}`)) return null;
|
|
927
|
+
return filePath;
|
|
928
|
+
};
|
|
929
|
+
function createEmbeddedFrontendAdapter(opts) {
|
|
930
|
+
const basePath = opts.basePath ?? defaultBasePath;
|
|
931
|
+
const normalizedBase = '/' === basePath ? '' : basePath.replace(/\/$/, '');
|
|
932
|
+
const distDir = opts.distDir;
|
|
933
|
+
const indexFile = opts.indexFile ?? 'index.html';
|
|
934
|
+
const spaFallback = opts.spaFallback ?? true;
|
|
935
|
+
const handler = async (c)=>{
|
|
936
|
+
let requestPath = c.req.path;
|
|
937
|
+
try {
|
|
938
|
+
requestPath = decodeURIComponent(requestPath);
|
|
939
|
+
} catch {
|
|
940
|
+
return c.text('Not Found', 404);
|
|
941
|
+
}
|
|
942
|
+
let relPath = requestPath;
|
|
943
|
+
if (normalizedBase && requestPath.startsWith(normalizedBase)) relPath = requestPath.slice(normalizedBase.length);
|
|
944
|
+
if (!relPath || '/' === relPath) relPath = `/${indexFile}`;
|
|
945
|
+
const filePath = safeResolve(distDir, relPath);
|
|
946
|
+
if (filePath) try {
|
|
947
|
+
const stat = await node_fs.promises.stat(filePath);
|
|
948
|
+
if (stat.isFile()) {
|
|
949
|
+
const buf = await node_fs.promises.readFile(filePath);
|
|
950
|
+
return new Response(buf, {
|
|
951
|
+
status: 200,
|
|
952
|
+
headers: {
|
|
953
|
+
'Content-Type': embeddedAdapter_contentTypeByExt(filePath)
|
|
954
|
+
}
|
|
955
|
+
});
|
|
956
|
+
}
|
|
957
|
+
} catch {}
|
|
958
|
+
if (!spaFallback) return c.text('Not Found', 404);
|
|
959
|
+
const fallbackPath = safeResolve(distDir, `/${indexFile}`);
|
|
960
|
+
if (!fallbackPath) return c.text('Not Found', 404);
|
|
961
|
+
try {
|
|
962
|
+
const buf = await node_fs.promises.readFile(fallbackPath);
|
|
963
|
+
return new Response(buf, {
|
|
964
|
+
status: 200,
|
|
965
|
+
headers: {
|
|
966
|
+
'Content-Type': embeddedAdapter_contentTypeByExt(fallbackPath)
|
|
967
|
+
}
|
|
968
|
+
});
|
|
969
|
+
} catch {
|
|
970
|
+
return c.text('Not Found', 404);
|
|
971
|
+
}
|
|
972
|
+
};
|
|
973
|
+
return {
|
|
974
|
+
id: 'treeshake-embedded-frontend',
|
|
975
|
+
register (app) {
|
|
976
|
+
const base = normalizedBase || '/';
|
|
977
|
+
app.get(base, handler);
|
|
978
|
+
app.get(`${base}/*`, handler);
|
|
979
|
+
}
|
|
980
|
+
};
|
|
981
|
+
}
|
|
982
|
+
function createServer(opts) {
|
|
983
|
+
const port = opts.port ?? 3000;
|
|
984
|
+
const hostname = opts.hostname ?? '0.0.0.0';
|
|
985
|
+
return serve({
|
|
986
|
+
fetch: opts.app.fetch,
|
|
987
|
+
port,
|
|
988
|
+
hostname
|
|
989
|
+
});
|
|
990
|
+
}
|
|
991
|
+
const hasIndexHtml = (dir)=>Boolean(dir && node_fs.existsSync(node_path.join(dir, 'index.html')));
|
|
992
|
+
const resolveFrontendDistDir = ()=>{
|
|
993
|
+
const envDir = process.env.TREESHAKE_FRONTEND_DIST;
|
|
994
|
+
if (envDir) {
|
|
995
|
+
if (hasIndexHtml(envDir)) return envDir;
|
|
996
|
+
throw new Error(`TREESHAKE_FRONTEND_DIST is set but index.html is missing: ${envDir}`);
|
|
997
|
+
}
|
|
998
|
+
const candidates = [
|
|
999
|
+
node_path.resolve(__dirname, 'frontend'),
|
|
1000
|
+
node_path.resolve(__dirname, '..', 'frontend')
|
|
1001
|
+
];
|
|
1002
|
+
for (const candidate of candidates)if (hasIndexHtml(candidate)) return candidate;
|
|
1003
|
+
const workspaceCandidates = [
|
|
1004
|
+
node_path.resolve(process.cwd(), 'packages', 'treeshake-frontend', 'dist'),
|
|
1005
|
+
node_path.resolve(process.cwd(), 'treeshake-frontend', 'dist')
|
|
1006
|
+
];
|
|
1007
|
+
for (const candidate of workspaceCandidates)if (hasIndexHtml(candidate)) return candidate;
|
|
1008
|
+
};
|
|
1009
|
+
async function main() {
|
|
1010
|
+
const registry = createOssAdapterRegistry();
|
|
1011
|
+
const resolved = resolveOssEnv({
|
|
1012
|
+
env: {
|
|
1013
|
+
...process.env,
|
|
1014
|
+
ADAPTER_ID: 'local'
|
|
1015
|
+
},
|
|
1016
|
+
registry,
|
|
1017
|
+
defaultAdapterId: 'local'
|
|
1018
|
+
});
|
|
1019
|
+
const logger = createLogger({
|
|
1020
|
+
level: resolved.logLevel
|
|
1021
|
+
});
|
|
1022
|
+
const deps = await createAdapterDeps({
|
|
1023
|
+
registry,
|
|
1024
|
+
adapterId: resolved.adapterId,
|
|
1025
|
+
adapterConfig: resolved.adapterConfig,
|
|
1026
|
+
logger
|
|
1027
|
+
});
|
|
1028
|
+
const frontendAdapters = [];
|
|
1029
|
+
const distDir = resolveFrontendDistDir();
|
|
1030
|
+
if (!distDir) throw new Error('Treeshake UI dist not found. Rebuild the CLI bundle or set TREESHAKE_FRONTEND_DIST.');
|
|
1031
|
+
frontendAdapters.push(createEmbeddedFrontendAdapter({
|
|
1032
|
+
basePath: process.env.TREESHAKE_FRONTEND_BASE_PATH ?? '/tree-shaking',
|
|
1033
|
+
distDir,
|
|
1034
|
+
spaFallback: '0' !== process.env.TREESHAKE_FRONTEND_SPA_FALLBACK && 'false' !== process.env.TREESHAKE_FRONTEND_SPA_FALLBACK
|
|
1035
|
+
}));
|
|
1036
|
+
const app = createApp(deps, {
|
|
1037
|
+
corsOrigin: resolved.corsOrigin,
|
|
1038
|
+
staticRootDir: resolved.staticRootDir,
|
|
1039
|
+
pruneIntervalMs: resolved.pruneIntervalMs,
|
|
1040
|
+
logger,
|
|
1041
|
+
runtimeEnv: resolved.runtimeEnv,
|
|
1042
|
+
frontendAdapters
|
|
1043
|
+
});
|
|
1044
|
+
createServer({
|
|
1045
|
+
app,
|
|
1046
|
+
port: resolved.port,
|
|
1047
|
+
hostname: resolved.hostname
|
|
1048
|
+
});
|
|
1049
|
+
console.log(`Build service listening on http://${resolved.hostname}:${resolved.port}`);
|
|
1050
|
+
if (frontendAdapters.length) {
|
|
1051
|
+
const basePath = process.env.TREESHAKE_FRONTEND_BASE_PATH ?? '/tree-shaking';
|
|
1052
|
+
console.log(`Treeshake UI available at http://${resolved.hostname}:${resolved.port}${basePath}`);
|
|
1053
|
+
}
|
|
1054
|
+
}
|
|
1055
|
+
main().catch((err)=>{
|
|
1056
|
+
console.error(err);
|
|
1057
|
+
process.exit(1);
|
|
1058
|
+
});
|