Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena committed Sep 25, 2021
1 parent 5362d4d commit 4982a2d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 34 deletions.
38 changes: 13 additions & 25 deletions packages/docusaurus-utils-plugin/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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'],
);
}
}
13 changes: 4 additions & 9 deletions packages/docusaurus-utils-plugin/src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 4982a2d

Please sign in to comment.