fondu 0.17.0 → 0.18.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.
@@ -1,5 +1,6 @@
1
- import { relative, sep } from 'node:path';
2
- import { CONFIG, HANDLERS, isPageFile, METHODS } from '../paths.js';
1
+ import { dirname, relative, resolve, sep } from 'node:path';
2
+ import { normalizePath } from 'vite';
3
+ import { CONFIG, checkExportNames, HANDLERS, isPageFile, METHODS } from '../paths.js';
3
4
  /** the exports of a page's route file that are server code: the browser gets the page (default) alone */
4
5
  const SERVER_EXPORTS = new Set([...HANDLERS, ...CONFIG, ...METHODS]);
5
6
  const nameOf = (n) => (n.name ?? n.value);
@@ -79,16 +80,19 @@ function references(node, out = new Set(), scopes = []) {
79
80
  case 'FunctionDeclaration':
80
81
  case 'FunctionExpression':
81
82
  case 'ArrowFunctionExpression': {
82
- const scope = new Set();
83
+ // the parameters' scope (a function expression's own name, the parameters): what their default expressions
84
+ // see. The body's own declarations are another scope, inside it: a default never reaches them
85
+ const params = new Set();
83
86
  if (n.type === 'FunctionExpression')
84
- bound(n.id, []).forEach((x) => scope.add(x));
87
+ bound(n.id, []).forEach((x) => params.add(x));
85
88
  for (const p of n.params)
86
- bound(p).forEach((x) => scope.add(x));
87
- vars(n.body, scope);
89
+ bound(p).forEach((x) => params.add(x));
90
+ const body = new Set();
91
+ vars(n.body, body);
88
92
  if (n.body.type === 'BlockStatement')
89
- lexical(n.body.body, scope);
90
- visit(n.params, [...scopes, scope]);
91
- visit(n.body, [...scopes, scope]);
93
+ lexical(n.body.body, body);
94
+ visit(n.params, [...scopes, params]);
95
+ visit(n.body, [...scopes, params, body]);
92
96
  return out;
93
97
  }
94
98
  case 'ClassDeclaration':
@@ -272,9 +276,26 @@ function importOf(code, node, kept) {
272
276
  ].filter(Boolean);
273
277
  return `import ${clauses.join(', ')} from ${source};`;
274
278
  }
279
+ /** the names a module exports, as its namespace lists them (sorted); `export *` is not followed */
280
+ function exportedNames(body) {
281
+ const names = new Set();
282
+ for (const node of body) {
283
+ if (node.type === 'ExportDefaultDeclaration')
284
+ names.add('default');
285
+ if (node.type !== 'ExportNamedDeclaration')
286
+ continue;
287
+ for (const name of bound(node.declaration))
288
+ names.add(name);
289
+ for (const s of node.specifiers)
290
+ names.add(nameOf(s.exported));
291
+ }
292
+ return [...names].sort();
293
+ }
275
294
  /** The browser's copy of every page's route file (`.tsx` in `app/routes/`): its server code removed */
276
295
  export function stripRoutes() {
277
296
  let root = process.cwd();
297
+ // the route files the strip changed (the build's): their source maps must not carry the original
298
+ const changed = new Set();
278
299
  return {
279
300
  name: 'fondu:strip-routes',
280
301
  enforce: 'post', // after the JSX is compiled: plain JavaScript to read
@@ -285,10 +306,48 @@ export function stripRoutes() {
285
306
  if (this.environment.config.consumer !== 'client')
286
307
  return;
287
308
  const file = relative(`${root}/app/routes`, id.split('?')[0]).split(sep).join('/');
288
- if (file.startsWith('..') || !isPageFile(file) || file.endsWith('.mdx'))
309
+ if (file.startsWith('..'))
310
+ return;
311
+ // a layout or the error page is not stripped: what the server refuses at startup fails the build
312
+ if (/(?:^|\/)_(?:layout|error)\.tsx$/.test(file)) {
313
+ const program = this.parse(code);
314
+ return void checkExportNames(file, exportedNames(program.body));
315
+ }
316
+ if (!isPageFile(file) || file.endsWith('.mdx'))
289
317
  return;
290
318
  const stripped = stripServerCode(code, this.parse(code), file);
291
- return stripped === code ? undefined : { code: stripped, map: null };
319
+ if (stripped === code)
320
+ return;
321
+ changed.add(id.split('?')[0]);
322
+ return { code: stripped, map: null };
323
+ },
324
+ // The maps' `sourcesContent` holds the file as it was read, the loaders in it. Stopgap: the entries of the files
325
+ // the strip changed are blanked (`null` keeps the indexes valid). The full fix: strip in `load`, so that the
326
+ // stripped text is the source (#241).
327
+ generateBundle(options, bundle) {
328
+ if (this.environment.config.consumer !== 'client' || !changed.size)
329
+ return;
330
+ const outDir = resolve(root, this.environment.config.build.outDir);
331
+ const blank = (map, from) => {
332
+ map.sources.forEach((source, i) => {
333
+ if (source && changed.has(normalizePath(resolve(outDir, dirname(from), source))))
334
+ map.sourcesContent[i] = null;
335
+ });
336
+ };
337
+ for (const [name, file] of Object.entries(bundle)) {
338
+ if (file.type === 'chunk' && file.map?.sourcesContent)
339
+ blank(file.map, name);
340
+ // a map written as a file of its own (`.map`): its text
341
+ else if (file.type === 'asset' &&
342
+ name.endsWith('.map') &&
343
+ typeof file.source === 'string') {
344
+ const map = JSON.parse(file.source);
345
+ if (!map.sourcesContent)
346
+ continue;
347
+ blank(map, name);
348
+ file.source = JSON.stringify(map);
349
+ }
350
+ }
292
351
  },
293
352
  };
294
353
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fondu",
3
- "version": "0.17.0",
3
+ "version": "0.18.1",
4
4
  "description": "A small meta-framework for full-stack React apps, built on Hono and Vite. It runs on Bun, Cloudflare Workers and Node.",
5
5
  "keywords": [
6
6
  "bun",