From c98615584508a73cda53124cca10b36f5ee6520c Mon Sep 17 00:00:00 2001 From: Josh-Cena Date: Sat, 16 Oct 2021 08:02:25 +0800 Subject: [PATCH] Seperate build util functions --- packages/docusaurus-utils-build/src/build.ts | 47 +-------------- .../docusaurus-utils-build/src/compiler.ts | 58 +++++++++++++++++++ packages/docusaurus-utils-build/src/watch.ts | 22 +++++-- 3 files changed, 76 insertions(+), 51 deletions(-) create mode 100644 packages/docusaurus-utils-build/src/compiler.ts diff --git a/packages/docusaurus-utils-build/src/build.ts b/packages/docusaurus-utils-build/src/build.ts index 357f507f6b370..10fdff9f4356a 100644 --- a/packages/docusaurus-utils-build/src/build.ts +++ b/packages/docusaurus-utils-build/src/build.ts @@ -5,28 +5,10 @@ * LICENSE file in the root directory of this source tree. */ -import path from 'path'; import fs from 'fs'; -import {transformFileSync} from '@babel/core'; import {Globby} from '@docusaurus/utils'; import Prettier from 'prettier'; - -export function compileOrCopy( - filePath: string, - sourceDir: string, - targetDir: string, - compileAction: (file: string) => string, -): void { - const targetPath = path - .resolve(targetDir, path.relative(sourceDir, filePath)) - .replace(/\.tsx?/g, '.js'); - fs.mkdirSync(path.dirname(targetPath), {recursive: true}); - if (/\.tsx?/.test(path.extname(filePath))) { - fs.writeFileSync(targetPath, compileAction(filePath)); - } else { - fs.copyFileSync(filePath, targetPath); - } -} +import {compileOrCopy, fullyTranspile, stripTypes} from './compiler'; function transformDir( sourceDir: string, @@ -41,33 +23,6 @@ function transformDir( ); } -export function fullyTranspile(file: string): string { - return ( - transformFileSync(file, { - presets: [ - ['@babel/preset-typescript', {isTSX: true, allExtensions: true}], - ], - plugins: [ - '@babel/plugin-transform-modules-commonjs', - '@babel/plugin-proposal-nullish-coalescing-operator', - '@babel/plugin-proposal-optional-chaining', - ], - })?.code ?? '' - ); -} - -function stripTypes(file: string, prettierConfig: Prettier.Options) { - // TODO let's hope for the pipeline operator :D - return Prettier.format( - transformFileSync(file, { - presets: [ - ['@babel/preset-typescript', {isTSX: true, allExtensions: true}], - ], - })?.code ?? '', - {parser: 'babel', ...prettierConfig}, - ); -} - export default async function build( options: Partial<{ sourceDir: string; diff --git a/packages/docusaurus-utils-build/src/compiler.ts b/packages/docusaurus-utils-build/src/compiler.ts new file mode 100644 index 0000000000000..1a1512a0a1f93 --- /dev/null +++ b/packages/docusaurus-utils-build/src/compiler.ts @@ -0,0 +1,58 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import fs from 'fs'; +import path from 'path'; +import Prettier from 'prettier'; +import {transformFileSync} from '@babel/core'; + +export function compileOrCopy( + filePath: string, + sourceDir: string, + targetDir: string, + compileAction: (file: string) => string, +): void { + const targetPath = path + .resolve(targetDir, path.relative(sourceDir, filePath)) + .replace(/\.tsx?/g, '.js'); + fs.mkdirSync(path.dirname(targetPath), {recursive: true}); + if (/\.tsx?/.test(path.extname(filePath))) { + fs.writeFileSync(targetPath, compileAction(filePath)); + } else { + fs.copyFileSync(filePath, targetPath); + } +} + +export function fullyTranspile(file: string): string { + return ( + transformFileSync(file, { + presets: [ + ['@babel/preset-typescript', {isTSX: true, allExtensions: true}], + ], + plugins: [ + '@babel/plugin-transform-modules-commonjs', + '@babel/plugin-proposal-nullish-coalescing-operator', + '@babel/plugin-proposal-optional-chaining', + ], + })?.code ?? '' + ); +} + +export function stripTypes( + file: string, + prettierConfig: Prettier.Options, +): string { + // TODO let's hope for the pipeline operator :D + return Prettier.format( + transformFileSync(file, { + presets: [ + ['@babel/preset-typescript', {isTSX: true, allExtensions: true}], + ], + })?.code ?? '', + {parser: 'babel', ...prettierConfig}, + ); +} diff --git a/packages/docusaurus-utils-build/src/watch.ts b/packages/docusaurus-utils-build/src/watch.ts index df52a1b07290d..51e11134956cc 100644 --- a/packages/docusaurus-utils-build/src/watch.ts +++ b/packages/docusaurus-utils-build/src/watch.ts @@ -8,31 +8,43 @@ import chokidar from 'chokidar'; import {debounce} from 'lodash'; import chalk from 'chalk'; -import {fullyTranspile, compileOrCopy} from './build'; +import {fullyTranspile, stripTypes, compileOrCopy} from './compiler'; export default async function watch( options: Partial<{ sourceDir: string; targetDir: string; + themeDir: string; + themeTargetDir: string; ignore: string[]; }> = {}, ): Promise { const { sourceDir = 'src', targetDir = 'lib', + themeDir = 'src/theme', + themeTargetDir = 'lib/theme', ignore = ['**/__tests__/**'], } = options; - const watcher = chokidar.watch(sourceDir, { + const watcher = chokidar.watch([sourceDir, themeDir], { ignoreInitial: true, - ignored: ignore, + ignored: [...ignore, '**/*.d.ts'], awaitWriteFinish: { stabilityThreshold: 50, pollInterval: 10, }, }); - const debouncedCompile = debounce((filePath) => { + const debouncedCompile = debounce((filePath: string) => { try { - compileOrCopy(filePath, sourceDir, targetDir, fullyTranspile); + // TODO: is check this good enough? + if (filePath.includes(themeDir)) { + // For perf reasons, we don't do prettier in watch mode + compileOrCopy(filePath, themeDir, themeTargetDir, (file) => + stripTypes(file, {}), + ); + } else { + compileOrCopy(filePath, sourceDir, targetDir, fullyTranspile); + } } catch (e) { console.log(chalk.red(`Error while processing ${chalk.cyan(filePath)}:`)); console.error(e);