@octanejs/mdx 0.1.62 → 0.1.63

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 CHANGED
@@ -143,7 +143,10 @@ async — it works in the vite plugin and `compileMdx`, not `compileMdxSync`.
143
143
  - `.mdx` edits fast-refresh in dev: the pipeline wraps the document's default
144
144
  export in octane's runtime `hmr()` and self-accepts, so live mounts re-render
145
145
  the new body in place (no page reload; the vite plugin enables this in serve
146
- mode automatically).
146
+ mode automatically). Importers keep the named exports they read, such as
147
+ `frontmatter`, so the update also reaches them when one is no longer
148
+ identical. An object export, `frontmatter` included, is a new object after
149
+ every edit.
147
150
 
148
151
  ## Status
149
152
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@octanejs/mdx",
3
- "version": "0.1.62",
3
+ "version": "0.1.63",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "engines": {
@@ -61,7 +61,7 @@
61
61
  "devDependencies": {
62
62
  "@octanejs/testing-library": "0.1.63",
63
63
  "@shikijs/rehype": "^3.23.0",
64
- "octane": "0.13.0",
64
+ "octane": "0.13.2",
65
65
  "vite": "^8.1.5",
66
66
  "vitest": "^4.1.10"
67
67
  },
package/src/compile.js CHANGED
@@ -135,7 +135,7 @@ export async function compileMdx(source, id, options = {}) {
135
135
  { value: source, path: id },
136
136
  buildMdxOptions(id, options, diagnosticContext),
137
137
  );
138
- return octaneStage(String(out.value), out.map, source, id, options, diagnosticContext);
138
+ return octaneStage(out, source, id, options, diagnosticContext);
139
139
  }
140
140
 
141
141
  /**
@@ -157,7 +157,7 @@ export function compileMdxSync(source, id, options = {}) {
157
157
  { value: source, path: id },
158
158
  buildMdxOptions(id, options, diagnosticContext),
159
159
  );
160
- return octaneStage(String(out.value), out.map, source, id, options, diagnosticContext);
160
+ return octaneStage(out, source, id, options, diagnosticContext);
161
161
  }
162
162
 
163
163
  /**
@@ -356,15 +356,16 @@ function restoreAuthoredJsxAttributeLocations(context) {
356
356
  }
357
357
 
358
358
  /**
359
- * @param {string} jsxSource
360
- * @param {unknown} mdxMap
359
+ * @param {{ value: unknown, map?: unknown, data: Record<string, any> }} mdxFile
361
360
  * @param {string} authoredSource
362
361
  * @param {string} id
363
362
  * @param {CompileMdxOptions} options
364
363
  * @param {DiagnosticMappingContext} diagnosticContext
365
364
  * @returns {CompileMdxResult}
366
365
  */
367
- function octaneStage(jsxSource, mdxMap, authoredSource, id, options, diagnosticContext) {
366
+ function octaneStage(mdxFile, authoredSource, id, options, diagnosticContext) {
367
+ const jsxSource = String(mdxFile.value);
368
+ const mdxMap = mdxFile.map;
368
369
  const mode = options.mode ?? 'client';
369
370
  const out = octaneCompile(jsxSource, id, {
370
371
  mode,
@@ -401,19 +402,34 @@ function octaneStage(jsxSource, mdxMap, authoredSource, id, options, diagnosticC
401
402
  // `@{}`-form components in `hmr(...)` — `MDXContent` (a passthrough function
402
403
  // returning a ternary of descriptors) isn't recognized as one, so the octane
403
404
  // `hmr` flag alone leaves `.mdx` edits as full module invalidations. The
404
- // PIPELINE knows the emitted shape, so it appends the exact registration the
405
+ // PIPELINE knows the emitted shape, so it appends the registration the
405
406
  // octane compiler emits for `.tsrx` exports: wrap the default export in the
406
- // runtime `hmr()` (identity-stable across edits — parents keep their mounted
407
- // wrapper) + a self-accepting `import.meta.hot` block that swaps the body
408
- // and re-renders live blocks in place. Appending after the fact keeps the
409
- // source map's earlier segments valid (ESM imports hoist).
407
+ // runtime `hmr()`, hand each evaluation's body to the wrapper that importers
408
+ // and live blocks already hold (kept in `hot.data`, so parents never remount
409
+ // the document), and self-accept. Importers keep the bindings they linked
410
+ // against, so the callback invalidates when another export they read
411
+ // changed or cannot be compared. Appending after the fact keeps the source
412
+ // map's earlier segments valid (ESM imports hoist).
410
413
  if (mode === 'client' && options.hmr && /\bexport default function MDXContent\b/.test(out.code)) {
414
+ const namedExports = mdxFile.data.octaneNamedExports;
415
+ const changed =
416
+ namedExports === null
417
+ ? '_$mdxModule'
418
+ : `_$mdxModule && (${[['default', 'MDXContent'], ...namedExports]
419
+ .map(([name, local]) => `_$mdxModule[${JSON.stringify(name)}] !== ${local}`)
420
+ .join(' || ')})`;
411
421
  out.code +=
412
422
  "\nimport { hmr as _$mdxHmr, HMR as _$mdxHMR } from 'octane';\n" +
413
423
  'MDXContent = _$mdxHmr(MDXContent);\n' +
414
424
  'if (import.meta.hot) {\n' +
415
- ' import.meta.hot.accept((module) => {\n' +
416
- ' module && MDXContent[_$mdxHMR].update(module.default);\n' +
425
+ ' const _$mdxPrevious = import.meta.hot.data?.__octaneMdxContent;\n' +
426
+ ' if (_$mdxPrevious) {\n' +
427
+ ' if (_$mdxPrevious[_$mdxHMR].update(MDXContent)) MDXContent = _$mdxPrevious;\n' +
428
+ ' else import.meta.hot.invalidate();\n' +
429
+ ' }\n' +
430
+ ' if (import.meta.hot.data) import.meta.hot.data.__octaneMdxContent = MDXContent;\n' +
431
+ ' import.meta.hot.accept((_$mdxModule) => {\n' +
432
+ ` if (${changed}) import.meta.hot.invalidate();\n` +
417
433
  ' });\n' +
418
434
  '}\n';
419
435
  }
@@ -608,12 +624,43 @@ function remapDiagnostics(diagnostics, mdxMap, jsxSource, source, id, authoredAt
608
624
  const MDX_BODY_NAME = '_createMdxContent';
609
625
 
610
626
  function recmaOctaneAdapter() {
611
- /** @param {unknown} tree */
612
- return (tree) => {
627
+ /**
628
+ * @param {unknown} tree
629
+ * @param {{ data: Record<string, unknown> }} file
630
+ */
631
+ return (tree, file) => {
613
632
  walkReplace(/** @type {EstreeNode} */ (tree), adaptNode);
633
+ file.data.octaneNamedExports = namedExportBindings(/** @type {any} */ (tree));
614
634
  };
615
635
  }
616
636
 
637
+ /**
638
+ * The document's named exports as [exportName, localName] pairs, or null when
639
+ * one has no single module binding to compare: a re-export, `export *`, or a
640
+ * destructuring declaration.
641
+ *
642
+ * @param {{ body: any[] }} program
643
+ * @returns {Array<[string, string]> | null}
644
+ */
645
+ function namedExportBindings(program) {
646
+ /** @type {Array<[string, string]>} */
647
+ const bindings = [];
648
+ for (const node of program.body) {
649
+ if (node.type === 'ExportAllDeclaration') return null;
650
+ if (node.type !== 'ExportNamedDeclaration') continue;
651
+ if (node.source) return null;
652
+ const declaration = node.declaration;
653
+ for (const { id } of declaration?.declarations ?? (declaration ? [declaration] : [])) {
654
+ if (id.type !== 'Identifier') return null;
655
+ bindings.push([id.name, id.name]);
656
+ }
657
+ for (const specifier of node.specifiers) {
658
+ bindings.push([specifier.exported.name ?? specifier.exported.value, specifier.local.name]);
659
+ }
660
+ }
661
+ return bindings;
662
+ }
663
+
617
664
  /** @typedef {{ type: string, [key: string]: unknown }} EstreeNode */
618
665
 
619
666
  /**