Skip to content

Commit 759087e

Browse files
committed
chore: update
1 parent dfcaf89 commit 759087e

File tree

25 files changed

+115
-67
lines changed

25 files changed

+115
-67
lines changed

packages/cli/src/update.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
import { spawn } from 'node:child_process';
22
import fs from 'node:fs/promises';
33
import path from 'node:path';
4-
import fse from '@rspress/shared/fs-extra';
54
import { logger } from '@rspress/shared/logger';
65

6+
async function pathExists(path: string): Promise<boolean> {
7+
try {
8+
await fs.access(path);
9+
return true;
10+
} catch {
11+
return false;
12+
}
13+
}
14+
715
type PackageManager = 'npm' | 'yarn' | 'pnpm' | 'bun';
816

917
const lockfileMap: Record<string, PackageManager> = {
@@ -16,7 +24,7 @@ const lockfileMap: Record<string, PackageManager> = {
1624
async function getPackageManager(rootPath: string) {
1725
let packageManager: PackageManager = 'npm';
1826
for (const file of Object.keys(lockfileMap)) {
19-
if (await fse.pathExists(path.join(rootPath, file))) {
27+
if (await pathExists(path.join(rootPath, file))) {
2028
packageManager = lockfileMap[file];
2129
break;
2230
}

packages/core/src/node/build.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import fs from 'node:fs/promises';
12
import { dirname, join } from 'node:path';
23
import { pathToFileURL } from 'node:url';
34
import type { Route } from '@/node/route/RouteService';
@@ -11,7 +12,6 @@ import {
1112
withBase,
1213
} from '@rspress/shared';
1314
import chalk from '@rspress/shared/chalk';
14-
import fs from '@rspress/shared/fs-extra';
1515
import { logger } from '@rspress/shared/logger';
1616
import type { HelmetData } from 'react-helmet-async';
1717
import { PluginDriver } from './PluginDriver';
@@ -50,13 +50,18 @@ export async function bundle(
5050
pluginDriver,
5151
enableSSG,
5252
);
53+
5354
await rsbuild.build();
5455
} finally {
5556
await writeSearchIndex(config);
5657
await checkLanguageParity(config);
5758
}
5859
}
5960

61+
function emptyDir(path: string): Promise<void> {
62+
return fs.rm(path, { force: true, recursive: true });
63+
}
64+
6065
export interface SSRBundleExports {
6166
render: (
6267
url: string,
@@ -77,7 +82,6 @@ export async function renderPages(
7782
const ssrBundlePath = join(outputPath, 'ssr', 'main.cjs');
7883

7984
try {
80-
const { default: fs } = await import('@rspress/shared/fs-extra');
8185
const { version } = await import('../../package.json');
8286
// There are two cases where we will fallback to CSR:
8387
// 1. ssr bundle load failed
@@ -204,15 +208,17 @@ export async function renderPages(
204208
return `${path}.html`.replace(normalizedBase, '');
205209
};
206210
const fileName = normalizeHtmlFilePath(routePath);
207-
await fs.ensureDir(join(outputPath, dirname(fileName)));
211+
await fs.mkdir(join(outputPath, dirname(fileName)), {
212+
recursive: true,
213+
});
208214
await fs.writeFile(join(outputPath, fileName), html);
209215
}),
210216
);
211217
// Remove ssr bundle
212218
if (!isDebugMode()) {
213-
await fs.remove(join(outputPath, 'ssr'));
219+
await emptyDir(join(outputPath, 'ssr'));
214220
}
215-
await fs.remove(join(outputPath, 'html'));
221+
await emptyDir(join(outputPath, 'html'));
216222

217223
const totalTime = Date.now() - startTime;
218224
logger.success(`Pages rendered in ${chalk.yellow(totalTime)} ms.`);
@@ -227,13 +233,15 @@ export async function build(options: BuildOptions) {
227233
const pluginDriver = new PluginDriver(config, true);
228234
await pluginDriver.init();
229235
const modifiedConfig = await pluginDriver.modifyConfig();
236+
230237
await pluginDriver.beforeBuild();
231238
const ssgConfig = modifiedConfig.ssg ?? true;
232239

233240
// empty temp dir before build
234-
await fs.emptyDir(TEMP_DIR);
241+
await emptyDir(TEMP_DIR);
235242

236243
await bundle(docDirectory, modifiedConfig, pluginDriver, Boolean(ssgConfig));
244+
237245
await renderPages(appDirectory, modifiedConfig, pluginDriver, ssgConfig);
238246
await pluginDriver.afterBuild();
239247
}

packages/core/src/node/dev.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export async function dev(options: DevOptions): Promise<ServerInstance> {
2727
await pluginDriver.beforeBuild();
2828

2929
// empty temp dir before build
30-
// await fs.emptyDir( TEMP_DIR);
3130
const builder = await initRsbuild(
3231
docDirectory,
3332
modifiedConfig,

packages/core/src/node/initRsbuild.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import fs from 'node:fs/promises';
12
import path from 'node:path';
23
import type {
34
RsbuildConfig,
@@ -15,7 +16,6 @@ import {
1516
removeLeadingSlash,
1617
removeTrailingSlash,
1718
} from '@rspress/shared';
18-
import fs from '@rspress/shared/fs-extra';
1919
import type { PluginDriver } from './PluginDriver';
2020
import {
2121
CLIENT_ENTRY,
@@ -122,11 +122,11 @@ async function createInternalBuildConfig(
122122
dev: {
123123
progressBar: false,
124124
// Serve static files
125-
setupMiddlewares: [
126-
middlewares => {
127-
middlewares.unshift(serveSearchIndexMiddleware(config));
128-
},
129-
],
125+
// setupMiddlewares: [
126+
// middlewares => {
127+
// middlewares.unshift(serveSearchIndexMiddleware(config));
128+
// },
129+
// ],
130130
cliShortcuts: {
131131
// does not support restart server yet
132132
custom: shortcuts => shortcuts.filter(({ key }) => key !== 'r'),
@@ -321,7 +321,7 @@ export async function initRsbuild(
321321
// and we should empty temp dir before build
322322
const runtimeTempDir = path.join(RSPRESS_TEMP_DIR, 'runtime');
323323
const runtimeAbsTempDir = path.join(cwd, 'node_modules', runtimeTempDir);
324-
await fs.ensureDir(runtimeAbsTempDir);
324+
await fs.mkdir(runtimeAbsTempDir, { recursive: true });
325325

326326
const routeService = await initRouteService({
327327
config,

packages/core/src/node/mdx/loader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import fs from 'node:fs/promises';
12
import path from 'node:path';
23
import { pathToFileURL } from 'node:url';
34
import { createProcessor } from '@mdx-js/mdx';
45
import { isProduction } from '@rspress/shared';
5-
import fs from '@rspress/shared/fs-extra';
66
import { logger } from '@rspress/shared/logger';
77
import { loadFrontMatter } from '@rspress/shared/node-utils';
88
import type { PluginDriver } from '../PluginDriver';

packages/core/src/node/route/RouteService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import fs from 'node:fs/promises';
12
import path from 'node:path';
23
import {
34
type PageModule,
@@ -7,7 +8,6 @@ import {
78
addTrailingSlash,
89
withBase,
910
} from '@rspress/shared';
10-
import fs from '@rspress/shared/fs-extra';
1111
import type { ComponentType } from 'react';
1212
import { glob } from 'tinyglobby';
1313
import type { PluginDriver } from '../PluginDriver';

packages/core/src/node/runtimeModule/globalStyles.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import fs from '@rspress/shared/fs-extra';
1+
import fs from 'node:fs/promises';
22
import { type FactoryContext, RuntimeModuleID } from '.';
33

44
export async function globalStylesVMPlugin(context: FactoryContext) {

packages/core/src/node/runtimeModule/siteData/extractPageData.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import fs from 'node:fs/promises';
12
import path from 'node:path';
23
import { importStatementRegex } from '@/node/constants';
34
import type { RouteService } from '@/node/route/RouteService';
@@ -10,7 +11,6 @@ import {
1011
type PageIndexInfo,
1112
type ReplaceRule,
1213
} from '@rspress/shared';
13-
import fs from '@rspress/shared/fs-extra';
1414
import { loadFrontMatter } from '@rspress/shared/node-utils';
1515
import { htmlToText } from 'html-to-text';
1616

packages/core/src/node/runtimeModule/siteData/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import fs from 'node:fs/promises';
12
import path from 'node:path';
23
import { TEMP_DIR, isProduction } from '@/node/constants';
34
import { createHash } from '@/node/utils';
45
import { SEARCH_INDEX_NAME, type SiteData } from '@rspress/shared';
5-
import fs from '@rspress/shared/fs-extra';
66
import { groupBy } from 'lodash-es';
77
import { type FactoryContext, RuntimeModuleID } from '..';
88
import { extractPageData } from './extractPageData';
@@ -97,7 +97,7 @@ export async function siteDataVMPlugin(context: FactoryContext) {
9797
const indexVersion = version ? `.${version.replace('.', '_')}` : '';
9898
const indexLang = lang ? `.${lang}` : '';
9999

100-
await fs.ensureDir(TEMP_DIR);
100+
await fs.mkdir(TEMP_DIR, { recursive: true });
101101
await fs.writeFile(
102102
path.join(
103103
TEMP_DIR,

packages/core/src/node/searchIndex.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import { createReadStream } from 'node:fs';
2+
import fs from 'node:fs/promises';
13
import type { IncomingMessage, ServerResponse } from 'node:http';
24
import path, { join } from 'node:path';
35
import { SEARCH_INDEX_NAME, type UserConfig, isSCM } from '@rspress/shared';
46
import chalk from '@rspress/shared/chalk';
5-
import fs from '@rspress/shared/fs-extra';
67
import { logger } from '@rspress/shared/logger';
78
import { OUTPUT_DIR, TEMP_DIR, isProduction } from './constants';
89

@@ -34,7 +35,7 @@ export async function writeSearchIndex(config: UserConfig) {
3435
searchIndexData = `${searchIndexData.slice(0, -1)}${
3536
scanning ? ',' : ''
3637
}${searchIndex.slice(1)}`;
37-
await fs.move(source, target, { overwrite: true });
38+
await fs.rename(source, target);
3839
scanning = true;
3940
}
4041

@@ -75,7 +76,7 @@ export function serveSearchIndexMiddleware(config: UserConfig): RequestHandler {
7576
res.setHeader('Content-Type', 'application/json');
7677
// Get search index name from request url
7778
const searchIndexFile = req.url?.split('/').pop();
78-
fs.createReadStream(
79+
createReadStream(
7980
path.join(
8081
process.cwd(),
8182
config?.outDir || OUTPUT_DIR,

0 commit comments

Comments
 (0)