Skip to content

Commit

Permalink
feat: optimize cleanDistPath.keep RegExp (#4390)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Jan 18, 2025
1 parent 6a77b69 commit 279dfb2
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion e2e/cases/output/clean-dist-path/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ test('should allow to use `cleanDistPath.keep` to keep some files', async () =>
rsbuildConfig: {
output: {
cleanDistPath: {
keep: [/dist[\\/]test.json/, /dist[\\/]foo[\\/]bar[\\/]test.json/],
keep: [/dist\/test.json/, /dist\/foo\/bar\/test.json/],
},
},
},
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/helpers/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'node:fs';
import path from 'node:path';
import { logger } from '../logger';
import type { Rspack } from '../types';
import { toPosixPath } from './path';

export const isFileSync = (filePath: string): boolean | undefined => {
try {
Expand Down Expand Up @@ -80,7 +81,7 @@ export async function emptyDir(
await Promise.all(
entries.map(async (entry) => {
const fullPath = path.resolve(dir, entry.name);
if (keep.some((reg) => reg.test(fullPath))) {
if (keep.some((reg) => reg.test(toPosixPath(fullPath)))) {
return;
}

Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/helpers/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,15 @@ export const dedupeNestedPaths = (paths: string[]): string[] => {
return prev.concat(curr);
}, []);
};

/**
* Convert Windows backslash paths to posix forward slashes
* @example
* toPosixPath('foo\\bar') // returns 'foo/bar'
*/
export const toPosixPath = (filepath: string): string => {
if (sep === '/') {
return filepath;
}
return filepath.replace(/\\/g, '/');
};
3 changes: 2 additions & 1 deletion packages/core/src/plugins/basic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'node:path';
import { toPosixPath } from '../helpers/path';
import type {
NormalizedEnvironmentConfig,
RsbuildPlugin,
Expand Down Expand Up @@ -80,7 +81,7 @@ export const pluginBasic = (): RsbuildPlugin => ({
// this helps VS Code break points working correctly in monorepo
chain.output.devtoolModuleFilenameTemplate(
(info: { absoluteResourcePath: string }) =>
path.resolve(info.absoluteResourcePath).replace(/\\/g, '/'),
toPosixPath(path.resolve(info.absoluteResourcePath)),
);
}

Expand Down
6 changes: 1 addition & 5 deletions website/docs/en/config/output/clean-dist-path.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,8 @@ For example, to keep the `dist/foo.json` file:
export default {
output: {
cleanDistPath: {
keep: [/dist[\\/]foo.json/],
keep: [/dist\/foo.json/],
},
},
};
```

:::tip
In the regular expression example, we use `[\\/]` to match the path separator because different operating systems use different path separators. Using `[\\/]` ensures that the paths can be matched in macOS, Linux and Windows.
:::
6 changes: 1 addition & 5 deletions website/docs/zh/config/output/clean-dist-path.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,8 @@ export default {
export default {
output: {
cleanDistPath: {
keep: [/dist[\\/]foo.json/],
keep: [/dist\/foo.json/],
},
},
};
```

:::tip
在正则表达式的例子中,我们使用 `[\\/]` 来匹配路径分隔符,这是因为不同的操作系统使用了不同的路径分隔符,使用 `[\\/]` 可以保证 macOS、Linux 和 Windows 的路径都被匹配到。
:::

1 comment on commit 279dfb2

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs ❌ failure
plugins ✅ success
rspress ✅ success
rslib ✅ success
examples ✅ success

Please sign in to comment.