From 4982a2db3e22a52546dd2c8bc51afc60b9d20e0d Mon Sep 17 00:00:00 2001 From: Josh-Cena Date: Sat, 25 Sep 2021 21:14:54 +0800 Subject: [PATCH] Improvements --- packages/docusaurus-utils-plugin/src/build.ts | 38 +++++++------------ packages/docusaurus-utils-plugin/src/watch.ts | 13 ++----- 2 files changed, 17 insertions(+), 34 deletions(-) diff --git a/packages/docusaurus-utils-plugin/src/build.ts b/packages/docusaurus-utils-plugin/src/build.ts index be4e19f356880..4fe283c3a86cf 100644 --- a/packages/docusaurus-utils-plugin/src/build.ts +++ b/packages/docusaurus-utils-plugin/src/build.ts @@ -70,29 +70,6 @@ function stripTypes(file: string, prettierConfig: PrettierOptions) { ); } -function compile(sourceDir: string, targetDir: string, ignore: string[]) { - transformDir(sourceDir, targetDir, fullyTranspile, [...ignore, '**/*.d.ts']); -} - -async function compileTheme( - themeDir: string, - themeTargetDir: string, - ignore: string[], -) { - if (fs.existsSync(themeDir)) { - const prettierConfig = await prettier.resolveConfig(themeDir); - if (!prettierConfig) { - throw new Error('Prettier config file not found'); - } - transformDir( - themeDir, - themeTargetDir, - (file) => stripTypes(file, prettierConfig), - [...ignore, '**/*.d.ts'], - ); - } -} - export default async function build( options: Partial<{ sourceDir: string; @@ -110,7 +87,18 @@ export default async function build( ignore = ['**/__tests__/**'], } = options; // Compile: src/*.ts -> lib/*.js - compile(sourceDir, targetDir, ignore); + transformDir(sourceDir, targetDir, fullyTranspile, [...ignore, '**/*.d.ts']); // Re-compile & prettier: src/theme/*.tsx -> lib/js-theme/*.js (for swizzling) - compileTheme(themeDir, themeTargetDir, ignore); + if (fs.existsSync(themeDir)) { + const prettierConfig = await prettier.resolveConfig(themeDir); + if (!prettierConfig) { + throw new Error('Prettier config file not found'); + } + transformDir( + themeDir, + themeTargetDir, + (file) => stripTypes(file, prettierConfig), + [...ignore, '**/*.d.ts'], + ); + } } diff --git a/packages/docusaurus-utils-plugin/src/watch.ts b/packages/docusaurus-utils-plugin/src/watch.ts index 25f58351c58a7..df52a1b07290d 100644 --- a/packages/docusaurus-utils-plugin/src/watch.ts +++ b/packages/docusaurus-utils-plugin/src/watch.ts @@ -30,26 +30,21 @@ export default async function watch( pollInterval: 10, }, }); - // Keeps track of how many requests are being processed. - let processing = 0; const debouncedCompile = debounce((filePath) => { try { compileOrCopy(filePath, sourceDir, targetDir, fullyTranspile); } catch (e) { + console.log(chalk.red(`Error while processing ${chalk.cyan(filePath)}:`)); console.error(e); } }, 200); ['add', 'change'].forEach((event) => watcher.on(event, async (filePath: string) => { - processing += 1; debouncedCompile(filePath); - processing -= 1; - if (processing === 0) { - console.log( - chalk.green(`Compilation of ${chalk.cyan(filePath)} finished`), - ); - } + console.log( + chalk.green(`Compilation of ${chalk.cyan(filePath)} finished`), + ); }), ); console.log(