Skip to content

Commit 2507585

Browse files
authored
refactor: remove fs-extra in @rspress/shared (#1805)
1 parent 9af3ba7 commit 2507585

File tree

30 files changed

+134
-82
lines changed

30 files changed

+134
-82
lines changed

e2e/tests/public-dir.test.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,41 @@
1+
import { access, readFile } from 'node:fs/promises';
12
import path from 'node:path';
23
import { expect, test } from '@playwright/test';
3-
import { exists, readFile } from 'fs-extra';
44
import { runBuildCommand } from '../utils/runCommands';
55

66
const fixtureDir = path.resolve(__dirname, '../fixtures');
77

8+
async function pathExists(path: string): Promise<boolean> {
9+
try {
10+
await access(path);
11+
return true;
12+
} catch {
13+
return false;
14+
}
15+
}
16+
817
test.describe('basic test', async () => {
918
test('should not generate the routes for html/js/mdx files in publicDir', async () => {
1019
const appDir = path.join(fixtureDir, 'public-dir');
1120
await runBuildCommand(appDir);
1221

13-
const existsImg = exists(
22+
const existsImg = pathExists(
1423
path.resolve(appDir, 'doc_build', 'rspress-icon.png'),
1524
);
1625
expect(existsImg).toBeTruthy();
1726

18-
const existsTestHtml = await exists(
27+
const existsTestHtml = await pathExists(
1928
path.resolve(appDir, 'doc_build', 'test.html'),
2029
);
2130
expect(existsTestHtml).toBeTruthy();
2231

2332
const testJsPath = path.resolve(appDir, 'doc_build', 'test.js');
24-
const existsTestJs = await exists(testJsPath);
33+
const existsTestJs = await pathExists(testJsPath);
2534
const testJsRaw = await readFile(testJsPath, 'utf-8');
2635
expect(existsTestJs).toBeTruthy();
2736
expect(testJsRaw.startsWith("console.log('test.js');")).toBeTruthy();
2837

29-
const existsTestMDX = await exists(
38+
const existsTestMDX = await pathExists(
3039
path.resolve(appDir, 'doc_build', 'test.mdx'),
3140
);
3241
expect(existsTestMDX).toBeTruthy();

e2e/tests/title-suffix.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { readFileSync } from 'node:fs';
12
import path from 'node:path';
23
import { expect, test } from '@playwright/test';
3-
import { readFileSync } from 'fs-extra';
44
import { runBuildCommand } from '../utils/runCommands';
55

66
const appDir = path.resolve(__dirname, '../fixtures/title-suffix');

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,11 @@
3838
"@modern-js/tsconfig": "2.64.0",
3939
"@playwright/test": "1.47.2",
4040
"@types/cross-spawn": "^6.0.6",
41-
"@types/fs-extra": "11.0.4",
4241
"@types/node": "^18.11.17",
4342
"check-dependency-version-consistency": "^5.0.0",
4443
"cross-env": "^7.0.3",
4544
"cross-spawn": "^7.0.6",
4645
"cspell-ban-words": "^0.0.4",
47-
"fs-extra": "11.3.0",
4846
"get-port": "5.1.1",
4947
"heading-case": "^0.1.4",
5048
"husky": "^9.1.7",

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: 2 additions & 2 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,
@@ -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) {

0 commit comments

Comments
 (0)