mirror of
https://github.com/hanzo-docs/docs.git
synced 2026-10-26 23:49:51 +00:00
MDX: Support re-generating index file with custom runtime
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'fumadocs-mdx': patch
|
||||
---
|
||||
|
||||
Support re-generating index file when using runtime: bun | node
|
||||
@@ -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',
|
||||
|
||||
@@ -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',
|
||||
|
||||
Reference in New Issue
Block a user