Skip to content

Commit

Permalink
fix: remove infinite HMR loop on first change
Browse files Browse the repository at this point in the history
  • Loading branch information
barjin committed Aug 30, 2024
1 parent 6f5c71d commit 8ddf62c
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 17 deletions.
2 changes: 1 addition & 1 deletion packages/plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@apify/docusaurus-plugin-typedoc-api",
"version": "4.2.3-1",
"version": "4.2.3-2",
"description": "Docusaurus plugin that provides source code API documentation powered by TypeDoc. ",
"keywords": [
"docusaurus",
Expand Down
6 changes: 3 additions & 3 deletions packages/plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ export default function typedocApiPlugin(
return {
path: info.permalink,
exact: true,
component: path.join(__dirname, `./components/ApiItem.${process.env.DEV ? 'tsx' : 'js'}`),
component: path.join(__dirname, `./components/ApiItem.${process.env.TYPEDOC_PLUGIN_DEV ? 'tsx' : 'js'}`),
modules,
sidebar: 'api',
// Map the ID here instead of creating a JSON data file,
Expand Down Expand Up @@ -407,7 +407,7 @@ export default function typedocApiPlugin(
{
path: indexPermalink,
exact: false,
component: path.join(__dirname, `./components/ApiPage.${process.env.DEV ? 'tsx' : 'js'}`),
component: path.join(__dirname, `./components/ApiPage.${process.env.TYPEDOC_PLUGIN_DEV ? 'tsx' : 'js'}`),
routes,
modules: {
options: optionsData,
Expand All @@ -434,7 +434,7 @@ export default function typedocApiPlugin(
},

getPathsToWatch() {
return [__dirname];
return process.env.TYPEDOC_PLUGIN_DEV ? [__dirname] : [];
},

configureWebpack(config, isServer, utils) {
Expand Down
13 changes: 8 additions & 5 deletions packages/plugin/src/plugin/python-generator/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { parseWithPydocMarkdown } from "./pydoc-markdown";
import { pydocToTypedoc } from "./transform-docs";
import { spinner } from 'zx';

export async function generateJsonFromPythonProject({
outFile,
projectRoot,
} : { outFile: string, projectRoot: string }): Promise<void> {
} : { outFile: string, projectRoot: string }): Promise<void> {
const pydocJson = await parseWithPydocMarkdown({ projectRoot });

await pydocToTypedoc({
moduleName: 'python', // TODO: get from project config files or passed options
outFile,
pydocJson,
await spinner('Converting the Python JSON AST to a TypeDoc-compliant file...', async () => {
await pydocToTypedoc({
moduleName: 'python', // TODO: get from project config files or passed options
outFile,
pydocJson,
});
});
}
10 changes: 7 additions & 3 deletions packages/plugin/src/plugin/python-generator/pydoc-markdown.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { rmSync, writeFileSync } from 'fs';
import path from 'path';
import { $ } from 'zx';
import { $, ProcessOutput, spinner } from 'zx';

/**
* Generates the pydoc-markdown configuration file
Expand Down Expand Up @@ -36,7 +36,7 @@ export async function parseWithPydocMarkdown({
for (const cmd of ['python', 'pydoc-markdown']) {
try {
// eslint-disable-next-line no-await-in-loop
await $`${cmd} --version`;
await spinner(`Checking for ${cmd}...`, () => $`${cmd} --version`);
} catch {
throw new Error(`Please install ${cmd} to use this plugin with Python projects.`);
}
Expand All @@ -48,7 +48,11 @@ export async function parseWithPydocMarkdown({
const configPath = path.join(__dirname, 'pydoc-markdown.temp.yml');
writeFileSync(configPath, configYml);

const pydoc = await $`pydoc-markdown --quiet --dump ${configPath}`;
let pydoc: ProcessOutput | null = null;

await spinner('Parsing the Python project into a JSON AST...', async () => {
pydoc = await $`pydoc-markdown --quiet --dump ${configPath}`;
});

rmSync(configPath);

Expand Down
9 changes: 9 additions & 0 deletions packages/plugin/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export interface DocusaurusPluginTypeDocApiOptions
sortSidebar?: (a: string, d: string) => number;
tsconfigName?: string;
typedocOptions?: Partial<TypeDocOptions>;

/**
* Switches the processing mode to Python-specific pipeline.
*/
python: boolean;

remarkPlugins: MDXPlugin[];
Expand All @@ -39,6 +43,11 @@ export interface DocusaurusPluginTypeDocApiOptions
disableVersioning?: boolean;
includeCurrentVersion?: boolean;
routeBasePath?: string;

/**
* Rebuild the API reference when the source files change.
*/
watchSources: boolean;
}

// CONFIG
Expand Down
6 changes: 3 additions & 3 deletions playground/website/docusaurus.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ const config: Config = {
context,
{
...options as any,
projectRoot: __dirname + '/../',
packages: ['js'],

projectRoot: __dirname + '/../python',
python: true,
packages: ['.']
},
),
],
Expand Down
4 changes: 2 additions & 2 deletions playground/website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"private": true,
"scripts": {
"docusaurus": "docusaurus",
"start": "DEV=1 docusaurus start",
"build": "DEV=1 docusaurus build",
"start": "TYPEDOC_PLUGIN_DEV=1 docusaurus start",
"build": "TYPEDOC_PLUGIN_DEV=1 docusaurus build",
"swizzle": "docusaurus swizzle",
"deploy": "docusaurus deploy",
"clear": "docusaurus clear",
Expand Down

0 comments on commit 8ddf62c

Please sign in to comment.