@rainfw/core 0.2.4 → 0.2.5
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/package.json +1 -1
- package/scripts/generate.js +64 -4
package/package.json
CHANGED
package/scripts/generate.js
CHANGED
|
@@ -213,6 +213,59 @@ function getClientFiles(dir, base = "") {
|
|
|
213
213
|
return files;
|
|
214
214
|
}
|
|
215
215
|
|
|
216
|
+
function ensureRelativeImport(importPath) {
|
|
217
|
+
return importPath.startsWith(".") ? importPath : `./${importPath}`;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function buildIslandImportLines(index, file, srcDir, entryDir) {
|
|
221
|
+
const fullPath = path.join(srcDir, file);
|
|
222
|
+
const content = fs.readFileSync(fullPath, "utf-8");
|
|
223
|
+
const { named, hasDefault } = detectAllExportsFromContent(content);
|
|
224
|
+
const islandId = clientFileToIslandId(file);
|
|
225
|
+
|
|
226
|
+
const importPath = path
|
|
227
|
+
.relative(entryDir, path.join(srcDir, file.replace(/\.tsx?$/, "")))
|
|
228
|
+
.replace(/\\/g, "/");
|
|
229
|
+
const safeImport = ensureRelativeImport(importPath);
|
|
230
|
+
|
|
231
|
+
const specifiers = [];
|
|
232
|
+
if (hasDefault) specifiers.push(`default as _d${index}`);
|
|
233
|
+
for (const name of named) specifiers.push(`${name} as _n${index}_${name}`);
|
|
234
|
+
if (specifiers.length === 0) return [];
|
|
235
|
+
|
|
236
|
+
const lines = [];
|
|
237
|
+
lines.push(`import { ${specifiers.join(", ")} } from "${safeImport}";`);
|
|
238
|
+
if (hasDefault) {
|
|
239
|
+
lines.push(`registerIsland("${islandId}:default", _d${index});`);
|
|
240
|
+
}
|
|
241
|
+
for (const name of named) {
|
|
242
|
+
lines.push(`registerIsland("${islandId}:${name}", _n${index}_${name});`);
|
|
243
|
+
}
|
|
244
|
+
return lines;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function generateClientEntrySource(clientFiles, srcDir) {
|
|
248
|
+
const entryDir = path.join(PROJECT_ROOT, BUILD_CONFIG.outDir);
|
|
249
|
+
const runtimePath = path
|
|
250
|
+
.relative(entryDir, path.join(PROJECT_ROOT, "src/framework/client/runtime"))
|
|
251
|
+
.replace(/\\/g, "/");
|
|
252
|
+
const runtimeImport = ensureRelativeImport(runtimePath);
|
|
253
|
+
|
|
254
|
+
const lines = [];
|
|
255
|
+
lines.push(
|
|
256
|
+
`import { registerIsland, startHydration } from "${runtimeImport}";`,
|
|
257
|
+
);
|
|
258
|
+
|
|
259
|
+
for (let i = 0; i < clientFiles.length; i++) {
|
|
260
|
+
lines.push(...buildIslandImportLines(i, clientFiles[i], srcDir, entryDir));
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
lines.push("startHydration();");
|
|
264
|
+
lines.push("");
|
|
265
|
+
|
|
266
|
+
return lines.join("\n");
|
|
267
|
+
}
|
|
268
|
+
|
|
216
269
|
function bundleClientFilesSync(clientFiles, srcDir) {
|
|
217
270
|
if (clientFiles.length === 0) return [];
|
|
218
271
|
if (!esbuild) {
|
|
@@ -230,21 +283,27 @@ function bundleClientFilesSync(clientFiles, srcDir) {
|
|
|
230
283
|
}
|
|
231
284
|
|
|
232
285
|
for (const file of fs.readdirSync(outDir)) {
|
|
233
|
-
if (file.startsWith("island-")) {
|
|
286
|
+
if (file.startsWith("island-") || file.startsWith("rain-client-")) {
|
|
234
287
|
fs.unlinkSync(path.join(outDir, file));
|
|
235
288
|
}
|
|
236
289
|
}
|
|
237
290
|
|
|
238
|
-
const
|
|
291
|
+
const entrySource = generateClientEntrySource(clientFiles, srcDir);
|
|
292
|
+
const entryDir = path.join(PROJECT_ROOT, BUILD_CONFIG.outDir);
|
|
293
|
+
if (!fs.existsSync(entryDir)) {
|
|
294
|
+
fs.mkdirSync(entryDir, { recursive: true });
|
|
295
|
+
}
|
|
296
|
+
const clientEntryPath = path.join(entryDir, "client-entry.ts");
|
|
297
|
+
fs.writeFileSync(clientEntryPath, entrySource);
|
|
239
298
|
|
|
240
299
|
const result = esbuild.buildSync({
|
|
241
|
-
entryPoints,
|
|
300
|
+
entryPoints: [clientEntryPath],
|
|
242
301
|
outdir: outDir,
|
|
243
302
|
bundle: true,
|
|
244
303
|
minify: true,
|
|
245
304
|
format: "esm",
|
|
246
305
|
metafile: true,
|
|
247
|
-
entryNames: "
|
|
306
|
+
entryNames: "rain-client-[hash]",
|
|
248
307
|
write: true,
|
|
249
308
|
treeShaking: true,
|
|
250
309
|
platform: "browser",
|
|
@@ -1125,6 +1184,7 @@ module.exports = {
|
|
|
1125
1184
|
updateWranglerAliases,
|
|
1126
1185
|
clientFileToIslandId,
|
|
1127
1186
|
bundleClientFilesSync,
|
|
1187
|
+
generateClientEntrySource,
|
|
1128
1188
|
validateNoPageRouteColocation,
|
|
1129
1189
|
validateNoDuplicateUrls,
|
|
1130
1190
|
stripRouteGroupSegments,
|