Skip to content

Commit 89fd65b

Browse files
feat: support option.excludeOutput (#7)
Co-authored-by: neverland <[email protected]>
1 parent 49271dd commit 89fd65b

File tree

5 files changed

+41
-12
lines changed

5 files changed

+41
-12
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,23 @@ pluginCheckSyntax({
148148
});
149149
```
150150
151+
### excludeOutput
152+
153+
- **Type:** `RegExp | RegExp[]`
154+
- **Default:** `undefined`
155+
156+
`excludeOutput` is used to exclude a portion of output files before detection. You can pass in one or more regular expressions to match the paths of output files. Files that match the regular expression will be ignored and will not trigger syntax checking.
157+
158+
- **Example:**
159+
160+
For example, to ignore files under the `dist/js` directory:
161+
162+
```ts
163+
pluginCheckSyntax({
164+
excludeOutput: /dist\/js/,
165+
});
166+
```
167+
151168
## Limitations
152169
153170
1. Check Syntax plugin only checks incompatible syntax in the outputs and cannot detect missing polyfills.

src/CheckSyntaxPlugin.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type {
1313
ECMASyntaxError,
1414
EcmaVersion,
1515
} from './types.js';
16-
import { checkIsExcludeSource } from './utils.js';
16+
import { checkIsExclude } from './utils.js';
1717

1818
type Compiler = Rspack.Compiler;
1919
type Compilation = Rspack.Compilation;
@@ -32,6 +32,8 @@ export class CheckSyntaxPlugin {
3232

3333
exclude: CheckSyntaxExclude | undefined;
3434

35+
excludeOutput: CheckSyntaxExclude | undefined;
36+
3537
constructor(
3638
options: CheckSyntaxOptions &
3739
Required<Pick<CheckSyntaxOptions, 'targets'>> & {
@@ -40,6 +42,7 @@ export class CheckSyntaxPlugin {
4042
) {
4143
this.targets = options.targets;
4244
this.exclude = options.exclude;
45+
this.excludeOutput = options.excludeOutput;
4346
this.rootPath = options.rootPath;
4447
this.ecmaVersion =
4548
options.ecmaVersion || browserslistToESVersion(this.targets);
@@ -58,7 +61,11 @@ export class CheckSyntaxPlugin {
5861
.map((a) => {
5962
// remove query from name
6063
const resourcePath = a.name.split('?')[0];
61-
return resolve(outputPath, resourcePath);
64+
const file = resolve(outputPath, resourcePath);
65+
if (!checkIsExclude(file, this.excludeOutput)) {
66+
return file;
67+
}
68+
return '';
6269
});
6370

6471
const files = emittedAssets.filter(
@@ -80,7 +87,7 @@ export class CheckSyntaxPlugin {
8087
const htmlScripts = await generateHtmlScripts(filepath);
8188
await Promise.all(
8289
htmlScripts.map(async (script) => {
83-
if (!checkIsExcludeSource(filepath, this.exclude)) {
90+
if (!checkIsExclude(filepath, this.exclude)) {
8491
await this.tryParse(filepath, script);
8592
}
8693
}),

src/generateError.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
type CheckSyntaxExclude,
77
ECMASyntaxError,
88
} from './types.js';
9-
import { checkIsExcludeSource } from './utils.js';
9+
import { checkIsExclude } from './utils.js';
1010

1111
export function displayCodePointer(code: string, pos: number) {
1212
const SUB_LEN = 80;
@@ -48,7 +48,7 @@ export async function generateError({
4848
});
4949
}
5050

51-
if (checkIsExcludeSource(error.source.path, exclude)) {
51+
if (checkIsExclude(error.source.path, exclude)) {
5252
return null;
5353
}
5454

src/types.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,31 @@ import type { ecmaVersion as EcmaVersion } from 'acorn';
22

33
export type { EcmaVersion };
44

5+
export type CheckSyntaxExclude = RegExp | RegExp[];
6+
57
export type CheckSyntaxOptions = {
68
/**
79
* The target browser range of the project.
810
* Its value is a standard browserslist array.
911
*/
1012
targets?: string[];
1113
/**
12-
* The minimum ECMAScript syntax version that can be used in the build artifact.
13-
* The priority of `ecmaVersion` is higher than `targets`.
14+
* Used to exclude a portion of source files during detection.
15+
* You can pass in one or more regular expressions to match the paths of source files.
1416
*/
15-
exclude?: RegExp | RegExp[];
17+
exclude?: CheckSyntaxExclude;
1618
/**
17-
* Used to exclude a portion of files during detection.
19+
* Used to exclude files by output path before detection.
1820
* You can pass in one or more regular expressions to match the paths of source files.
1921
*/
22+
excludeOutput?: CheckSyntaxExclude;
23+
/**
24+
* The minimum ECMAScript syntax version that can be used in the build artifact.
25+
* The priority of `ecmaVersion` is higher than `targets`.
26+
*/
2027
ecmaVersion?: EcmaVersion;
2128
};
2229

23-
export type CheckSyntaxExclude = NonNullable<CheckSyntaxOptions['exclude']>;
24-
2530
export interface Location {
2631
line: number;
2732
column: number;

src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { CheckSyntaxExclude } from './types.js';
22

3-
export function checkIsExcludeSource(
3+
export function checkIsExclude(
44
path: string,
55
exclude?: CheckSyntaxExclude,
66
): boolean {

0 commit comments

Comments
 (0)