Skip to content

Commit 2e428b6

Browse files
authored
fix: improve CheckSyntaxRspackPlugin options handling (#29)
1 parent fb1f0a4 commit 2e428b6

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

src/CheckSyntaxPlugin.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export const JS_REGEX: RegExp = /\.(?:js|mjs|cjs|jsx)$/;
1212

1313
export class CheckSyntaxRspackPlugin extends CheckSyntax {
1414
apply(compiler: Compiler): void {
15+
if (compiler.options.context && !this.rootPath) {
16+
this.rootPath = compiler.options.context;
17+
}
18+
1519
compiler.hooks.afterEmit.tapPromise(
1620
CheckSyntaxRspackPlugin.name,
1721
async (compilation: Compilation) => {

src/checkSyntax.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { generateError } from './generateError.js';
55
import { generateHtmlScripts } from './generateHtmlScripts.js';
66
import type {
77
AcornParseError,
8+
CheckSyntaxOptions as BaseCheckSyntaxOptions,
89
CheckSyntaxExclude,
9-
CheckSyntaxOptions,
1010
ECMASyntaxError,
1111
EcmaVersion,
1212
SyntaxErrorKey,
@@ -16,12 +16,19 @@ import { checkIsExclude } from './utils.js';
1616
const HTML_REGEX = /\.html$/;
1717
export const JS_REGEX: RegExp = /\.(?:js|mjs|cjs|jsx)$/;
1818

19+
type CheckSyntaxOptions = BaseCheckSyntaxOptions & {
20+
rootPath?: string;
21+
} & (
22+
| Required<Pick<BaseCheckSyntaxOptions, 'targets'>>
23+
| Required<Pick<BaseCheckSyntaxOptions, 'ecmaVersion'>>
24+
);
25+
1926
export class CheckSyntax {
2027
errors: ECMASyntaxError[] = [];
2128

2229
ecmaVersion: EcmaVersion;
2330

24-
targets: string[];
31+
targets?: string[];
2532

2633
rootPath: string;
2734

@@ -31,27 +38,24 @@ export class CheckSyntax {
3138

3239
excludeErrorLogs: SyntaxErrorKey[];
3340

34-
constructor(
35-
options: CheckSyntaxOptions &
36-
Required<Pick<CheckSyntaxOptions, 'targets'>> & {
37-
rootPath: string;
38-
},
39-
) {
41+
constructor(options: CheckSyntaxOptions) {
4042
if (!options) {
4143
throw new Error('[CheckSyntaxRspackPlugin] `options` is required.');
4244
}
43-
if (!options.targets && !options.ecmaVersion) {
45+
46+
const { targets, ecmaVersion } = options;
47+
if (!targets && !ecmaVersion) {
4448
throw new Error(
4549
'[CheckSyntaxRspackPlugin] `targets` or `ecmaVersion` option is required',
4650
);
4751
}
4852

49-
this.targets = options.targets;
53+
this.targets = targets;
54+
this.ecmaVersion = ecmaVersion || browserslistToESVersion(targets);
55+
5056
this.exclude = options.exclude;
5157
this.excludeOutput = options.excludeOutput;
52-
this.rootPath = options.rootPath;
53-
this.ecmaVersion =
54-
options.ecmaVersion || browserslistToESVersion(this.targets);
58+
this.rootPath = options.rootPath || '';
5559
this.excludeErrorLogs = options.excludeErrorLogs || [];
5660
}
5761

0 commit comments

Comments
 (0)