MDX: Support re-generating index file with custom runtime

This commit is contained in:
Fuma Nama
2025-11-01 16:15:37 +08:00
parent 2161d19607
commit d1e43f490b
3 changed files with 57 additions and 10 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'fumadocs-mdx': patch
---
Support re-generating index file when using runtime: bun | node
+9 -2
View File
@@ -130,7 +130,7 @@ export function toBun(loader: Loader, test: RegExp) {
build.onResolve({ filter: test }, (args) => {
const [filePath, query = ''] = args.path.split('?', 2);
// store the query string because `onLoad` striped it
// TODO: this isn't working because `args.path` still doesn't include query string
queryData.set(filePath, parse(query));
return null;
});
@@ -148,7 +148,14 @@ export function toBun(loader: Loader, test: RegExp) {
},
});
if (result === null) return;
// must return something, no fallback: https://github.com/oven-sh/bun/issues/5303
if (result === null) {
return {
contents: content,
loader: args.loader,
};
}
return {
contents: result.code,
loader: 'js',
+43 -8
View File
@@ -33,19 +33,47 @@ export default function vite({
index: IndexFileOptions | boolean;
}): Plugin {
let config: LoadedConfig;
let indexOptions: Required<IndexFileOptions> | false;
if (index === false) indexOptions = false;
else indexOptions = applyDefaults(index === true ? {} : index);
return {
name: 'vite',
config(v) {
config = v;
},
configureServer(server) {
if (
!server.watcher ||
indexOptions === false ||
indexOptions.runtime === false
)
return;
// for bun/node runtimes, alternative import.meta.glob has to be re-generated on update
server.watcher.on('all', (event, file) => {
if (event === 'change') return;
const isUpdated = config.collectionList.some((collection) => {
if (collection.type === 'docs')
return (
collection.docs.hasFile(file) || collection.meta.hasFile(file)
);
return collection.hasFile(file);
});
if (isUpdated) {
this.core.emitAndWrite({
filterPlugin: (plugin) => plugin.name === 'vite',
});
}
});
},
emit() {
const out: EmitEntry[] = [];
if (index === false) return out;
if (indexOptions === false) return out;
const indexOptions: IndexFileOptions =
typeof index === 'object' ? index : {};
const { browser = false } = indexOptions;
if (browser) {
if (indexOptions.browser) {
out.push({
path: 'browser.ts',
content: indexFile(this, config, indexOptions, 'browser'),
@@ -58,7 +86,7 @@ export default function vite({
this,
config,
indexOptions,
browser ? 'server' : 'all',
indexOptions.browser ? 'server' : 'all',
),
});
@@ -67,13 +95,20 @@ export default function vite({
};
}
function applyDefaults(options: IndexFileOptions): Required<IndexFileOptions> {
return {
addJsExtension: options.addJsExtension ?? false,
browser: options.browser ?? false,
runtime: options.runtime ?? false,
};
}
function indexFile(
{ configPath, outDir }: PluginContext,
config: LoadedConfig,
options: IndexFileOptions,
{ addJsExtension, runtime }: Required<IndexFileOptions>,
environment: 'all' | 'browser' | 'server',
) {
const { addJsExtension = false, runtime } = options;
const runtimePath = {
all: 'fumadocs-mdx/runtime/vite',
server: 'fumadocs-mdx/runtime/vite.server',