Skip to content

Commit c986155

Browse files
committed
Seperate build util functions
1 parent 22a7e38 commit c986155

File tree

3 files changed

+76
-51
lines changed

3 files changed

+76
-51
lines changed

packages/docusaurus-utils-build/src/build.ts

+1-46
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,10 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import path from 'path';
98
import fs from 'fs';
10-
import {transformFileSync} from '@babel/core';
119
import {Globby} from '@docusaurus/utils';
1210
import Prettier from 'prettier';
13-
14-
export function compileOrCopy(
15-
filePath: string,
16-
sourceDir: string,
17-
targetDir: string,
18-
compileAction: (file: string) => string,
19-
): void {
20-
const targetPath = path
21-
.resolve(targetDir, path.relative(sourceDir, filePath))
22-
.replace(/\.tsx?/g, '.js');
23-
fs.mkdirSync(path.dirname(targetPath), {recursive: true});
24-
if (/\.tsx?/.test(path.extname(filePath))) {
25-
fs.writeFileSync(targetPath, compileAction(filePath));
26-
} else {
27-
fs.copyFileSync(filePath, targetPath);
28-
}
29-
}
11+
import {compileOrCopy, fullyTranspile, stripTypes} from './compiler';
3012

3113
function transformDir(
3214
sourceDir: string,
@@ -41,33 +23,6 @@ function transformDir(
4123
);
4224
}
4325

44-
export function fullyTranspile(file: string): string {
45-
return (
46-
transformFileSync(file, {
47-
presets: [
48-
['@babel/preset-typescript', {isTSX: true, allExtensions: true}],
49-
],
50-
plugins: [
51-
'@babel/plugin-transform-modules-commonjs',
52-
'@babel/plugin-proposal-nullish-coalescing-operator',
53-
'@babel/plugin-proposal-optional-chaining',
54-
],
55-
})?.code ?? ''
56-
);
57-
}
58-
59-
function stripTypes(file: string, prettierConfig: Prettier.Options) {
60-
// TODO let's hope for the pipeline operator :D
61-
return Prettier.format(
62-
transformFileSync(file, {
63-
presets: [
64-
['@babel/preset-typescript', {isTSX: true, allExtensions: true}],
65-
],
66-
})?.code ?? '',
67-
{parser: 'babel', ...prettierConfig},
68-
);
69-
}
70-
7126
export default async function build(
7227
options: Partial<{
7328
sourceDir: string;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import fs from 'fs';
9+
import path from 'path';
10+
import Prettier from 'prettier';
11+
import {transformFileSync} from '@babel/core';
12+
13+
export function compileOrCopy(
14+
filePath: string,
15+
sourceDir: string,
16+
targetDir: string,
17+
compileAction: (file: string) => string,
18+
): void {
19+
const targetPath = path
20+
.resolve(targetDir, path.relative(sourceDir, filePath))
21+
.replace(/\.tsx?/g, '.js');
22+
fs.mkdirSync(path.dirname(targetPath), {recursive: true});
23+
if (/\.tsx?/.test(path.extname(filePath))) {
24+
fs.writeFileSync(targetPath, compileAction(filePath));
25+
} else {
26+
fs.copyFileSync(filePath, targetPath);
27+
}
28+
}
29+
30+
export function fullyTranspile(file: string): string {
31+
return (
32+
transformFileSync(file, {
33+
presets: [
34+
['@babel/preset-typescript', {isTSX: true, allExtensions: true}],
35+
],
36+
plugins: [
37+
'@babel/plugin-transform-modules-commonjs',
38+
'@babel/plugin-proposal-nullish-coalescing-operator',
39+
'@babel/plugin-proposal-optional-chaining',
40+
],
41+
})?.code ?? ''
42+
);
43+
}
44+
45+
export function stripTypes(
46+
file: string,
47+
prettierConfig: Prettier.Options,
48+
): string {
49+
// TODO let's hope for the pipeline operator :D
50+
return Prettier.format(
51+
transformFileSync(file, {
52+
presets: [
53+
['@babel/preset-typescript', {isTSX: true, allExtensions: true}],
54+
],
55+
})?.code ?? '',
56+
{parser: 'babel', ...prettierConfig},
57+
);
58+
}

packages/docusaurus-utils-build/src/watch.ts

+17-5
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,43 @@
88
import chokidar from 'chokidar';
99
import {debounce} from 'lodash';
1010
import chalk from 'chalk';
11-
import {fullyTranspile, compileOrCopy} from './build';
11+
import {fullyTranspile, stripTypes, compileOrCopy} from './compiler';
1212

1313
export default async function watch(
1414
options: Partial<{
1515
sourceDir: string;
1616
targetDir: string;
17+
themeDir: string;
18+
themeTargetDir: string;
1719
ignore: string[];
1820
}> = {},
1921
): Promise<void> {
2022
const {
2123
sourceDir = 'src',
2224
targetDir = 'lib',
25+
themeDir = 'src/theme',
26+
themeTargetDir = 'lib/theme',
2327
ignore = ['**/__tests__/**'],
2428
} = options;
25-
const watcher = chokidar.watch(sourceDir, {
29+
const watcher = chokidar.watch([sourceDir, themeDir], {
2630
ignoreInitial: true,
27-
ignored: ignore,
31+
ignored: [...ignore, '**/*.d.ts'],
2832
awaitWriteFinish: {
2933
stabilityThreshold: 50,
3034
pollInterval: 10,
3135
},
3236
});
33-
const debouncedCompile = debounce((filePath) => {
37+
const debouncedCompile = debounce((filePath: string) => {
3438
try {
35-
compileOrCopy(filePath, sourceDir, targetDir, fullyTranspile);
39+
// TODO: is check this good enough?
40+
if (filePath.includes(themeDir)) {
41+
// For perf reasons, we don't do prettier in watch mode
42+
compileOrCopy(filePath, themeDir, themeTargetDir, (file) =>
43+
stripTypes(file, {}),
44+
);
45+
} else {
46+
compileOrCopy(filePath, sourceDir, targetDir, fullyTranspile);
47+
}
3648
} catch (e) {
3749
console.log(chalk.red(`Error while processing ${chalk.cyan(filePath)}:`));
3850
console.error(e);

0 commit comments

Comments
 (0)