@valentinkolb/ssr 0.6.1 → 0.7.0
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/src/build.ts +48 -1
- package/src/index.ts +4 -1
package/package.json
CHANGED
package/src/build.ts
CHANGED
|
@@ -20,8 +20,9 @@ export const buildIslands = async (options: {
|
|
|
20
20
|
outdir: string;
|
|
21
21
|
verbose: boolean;
|
|
22
22
|
dev?: boolean;
|
|
23
|
+
external?: string[];
|
|
23
24
|
}): Promise<void> => {
|
|
24
|
-
const { pattern, outdir, verbose, dev = false } = options;
|
|
25
|
+
const { pattern, outdir, verbose, dev = false, external } = options;
|
|
25
26
|
|
|
26
27
|
const totalStart = performance.now();
|
|
27
28
|
|
|
@@ -77,6 +78,7 @@ export const buildIslands = async (options: {
|
|
|
77
78
|
outdir,
|
|
78
79
|
naming: { entry: "[name].js", chunk: "chunk-[hash].js" },
|
|
79
80
|
target: "browser",
|
|
81
|
+
external,
|
|
80
82
|
minify: !dev,
|
|
81
83
|
splitting: true,
|
|
82
84
|
sourcemap: dev ? "inline" : "none",
|
|
@@ -123,6 +125,51 @@ export const buildIslands = async (options: {
|
|
|
123
125
|
],
|
|
124
126
|
});
|
|
125
127
|
|
|
128
|
+
// Workaround for Bun bundler bug: duplicate export statements in shared chunks
|
|
129
|
+
// when splitting: true. Scan chunk files and deduplicate export lines.
|
|
130
|
+
if (result.success) {
|
|
131
|
+
const chunkGlob = new Glob("chunk-*.js");
|
|
132
|
+
for await (const chunkFile of chunkGlob.scan({ cwd: outdir, absolute: true })) {
|
|
133
|
+
const src = await Bun.file(chunkFile).text();
|
|
134
|
+
// Collect all export blocks and deduplicate
|
|
135
|
+
const exportRegex = /^export\s*\{[^}]*\}\s*;?\s*$/gm;
|
|
136
|
+
const exportBlocks = [...src.matchAll(exportRegex)].map((m) => m[0]);
|
|
137
|
+
if (exportBlocks.length > 1) {
|
|
138
|
+
// Parse all exported names from all blocks
|
|
139
|
+
const seenNames = new Set<string>();
|
|
140
|
+
const uniqueBlocks: string[] = [];
|
|
141
|
+
for (const block of exportBlocks) {
|
|
142
|
+
const names =
|
|
143
|
+
block
|
|
144
|
+
.match(/\{([^}]*)\}/)?.[1]
|
|
145
|
+
?.split(",")
|
|
146
|
+
.map((n) => n.trim())
|
|
147
|
+
.filter(Boolean) ?? [];
|
|
148
|
+
const newNames = names.filter((n) => !seenNames.has(n));
|
|
149
|
+
if (newNames.length > 0) {
|
|
150
|
+
uniqueBlocks.push(`export { ${newNames.join(", ")} };`);
|
|
151
|
+
newNames.forEach((n) => seenNames.add(n));
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
// Replace all export blocks with deduplicated ones
|
|
155
|
+
let fixed = src;
|
|
156
|
+
for (const block of exportBlocks) {
|
|
157
|
+
fixed = fixed.replace(block, "");
|
|
158
|
+
}
|
|
159
|
+
// Append single combined export before any debugId comment
|
|
160
|
+
const debugIdIdx = fixed.lastIndexOf("//# debugId=");
|
|
161
|
+
const combined = uniqueBlocks.join("\n") + "\n";
|
|
162
|
+
if (debugIdIdx !== -1) {
|
|
163
|
+
fixed = fixed.slice(0, debugIdIdx) + combined + fixed.slice(debugIdIdx);
|
|
164
|
+
} else {
|
|
165
|
+
fixed = fixed.trimEnd() + "\n" + combined;
|
|
166
|
+
}
|
|
167
|
+
await Bun.write(chunkFile, fixed);
|
|
168
|
+
if (verbose) console.log(` Fixed duplicate exports in ${chunkFile.split("/").pop()}`);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
126
173
|
if (verbose) {
|
|
127
174
|
console.log(`Bundle: ${fmt(performance.now() - bundleStart)}`);
|
|
128
175
|
for (const c of components) {
|
package/src/index.ts
CHANGED
|
@@ -29,6 +29,8 @@ export type SsrOptions<T extends object = object> = {
|
|
|
29
29
|
dev?: boolean;
|
|
30
30
|
/** Enable verbose logging (default: true in prod, false in dev) */
|
|
31
31
|
verbose?: boolean;
|
|
32
|
+
/** Modules to exclude from the island bundle (passed to Bun.build) */
|
|
33
|
+
external?: string[];
|
|
32
34
|
/** HTML template function (optional, has default) */
|
|
33
35
|
template?: (
|
|
34
36
|
ctx: {
|
|
@@ -85,7 +87,7 @@ export type SsrResult<T extends object> = {
|
|
|
85
87
|
* ```
|
|
86
88
|
*/
|
|
87
89
|
export const createConfig = <T extends object = object>(options: SsrOptions<T> = {}): SsrResult<T> => {
|
|
88
|
-
const { dev = false, verbose, template } = options;
|
|
90
|
+
const { dev = false, verbose, external, template } = options;
|
|
89
91
|
|
|
90
92
|
// Default template if none provided
|
|
91
93
|
const htmlTemplate =
|
|
@@ -156,6 +158,7 @@ export const createConfig = <T extends object = object>(options: SsrOptions<T> =
|
|
|
156
158
|
outdir: islandsOutdir,
|
|
157
159
|
verbose: verbose ?? !dev,
|
|
158
160
|
dev,
|
|
161
|
+
external,
|
|
159
162
|
});
|
|
160
163
|
};
|
|
161
164
|
|