From b10045f36cdd62db86f1d01674181088ba9aa4f2 Mon Sep 17 00:00:00 2001 From: zoomdong <1344492820@qq.com> Date: Thu, 29 Jun 2023 17:57:25 +0800 Subject: [PATCH] fix(builder): checkSyntax targets should get default browserlist when only set checkSyntax.exclude (#4095) * fix(builder): checkSyntax targets should get default browserlist when only set checkSyntax.exclude * fix(builder): checkSyntax targets should get default browserlist when only set checkSyntax.exclude * chore: update --- .changeset/fix-checksyntax-exclude.md | 8 ++++ .../src/plugins/CheckSyntaxPlugin/index.ts | 5 ++- .../src/types/config/security.ts | 2 +- .../builder/src/plugins/checkSyntax.ts | 15 +++---- .../__snapshots__/checkSyntax.test.ts.snap | 21 ++++++++++ .../builder/tests/plugins/checkSyntax.test.ts | 41 +++++++++++++++++++ 6 files changed, 83 insertions(+), 9 deletions(-) create mode 100644 .changeset/fix-checksyntax-exclude.md diff --git a/.changeset/fix-checksyntax-exclude.md b/.changeset/fix-checksyntax-exclude.md new file mode 100644 index 000000000000..2493aa1d6c50 --- /dev/null +++ b/.changeset/fix-checksyntax-exclude.md @@ -0,0 +1,8 @@ +--- +'@modern-js/builder': patch +'@modern-js/builder-shared': patch +--- + +fix(builder): checkSyntax targets should get default browserlist when only set checkSyntax.exclude + +fix(builder): 当只设置 checkSyntax.exclude 时,checkSyntax targets 应该使用默认的 broserlist 值 diff --git a/packages/builder/builder-shared/src/plugins/CheckSyntaxPlugin/index.ts b/packages/builder/builder-shared/src/plugins/CheckSyntaxPlugin/index.ts index 9f8b16fb3bb5..3c764ddef84e 100644 --- a/packages/builder/builder-shared/src/plugins/CheckSyntaxPlugin/index.ts +++ b/packages/builder/builder-shared/src/plugins/CheckSyntaxPlugin/index.ts @@ -26,7 +26,10 @@ export class CheckSyntaxPlugin { exclude: CheckSyntaxExclude | undefined; - constructor(options: CheckSyntaxOptions) { + constructor(options: { + targets: string[]; + exclude: CheckSyntaxOptions['exclude']; + }) { this.targets = options.targets; this.exclude = options.exclude; this.ecmaVersion = getEcmaVersion(this.targets); diff --git a/packages/builder/builder-shared/src/types/config/security.ts b/packages/builder/builder-shared/src/types/config/security.ts index d956b189756c..47e7cf47b6cf 100644 --- a/packages/builder/builder-shared/src/types/config/security.ts +++ b/packages/builder/builder-shared/src/types/config/security.ts @@ -5,7 +5,7 @@ export type SriOptions = { }; export interface CheckSyntaxOptions { - targets: string[]; + targets?: string[]; exclude?: RegExp | Array; } diff --git a/packages/builder/builder/src/plugins/checkSyntax.ts b/packages/builder/builder/src/plugins/checkSyntax.ts index 772e7d6a76bb..08a7461f26f2 100644 --- a/packages/builder/builder/src/plugins/checkSyntax.ts +++ b/packages/builder/builder/src/plugins/checkSyntax.ts @@ -5,6 +5,7 @@ import { getBrowserslistWithDefault, DefaultBuilderPlugin, SharedNormalizedConfig, + CheckSyntaxOptions, } from '@modern-js/builder-shared'; export function builderPluginCheckSyntax(): DefaultBuilderPlugin { @@ -47,16 +48,16 @@ async function getCheckTargets( builderContext: BuilderContext, builderConfig: SharedNormalizedConfig, builderTarget: BuilderTarget, - checkSyntax: { targets: string[] } | true, + checkSyntax: CheckSyntaxOptions | true, ) { - if (checkSyntax === true) { - const browserslist = await getBrowserslistWithDefault( + const browserslist = + (await getBrowserslistWithDefault( builderContext.rootPath, builderConfig, builderTarget, - ); - - return browserslist || DEFAULT_BROWSERSLIST[builderTarget]; + )) ?? DEFAULT_BROWSERSLIST[builderTarget]; + if (checkSyntax === true) { + return browserslist; } - return checkSyntax.targets; + return checkSyntax.targets ?? browserslist; } diff --git a/packages/builder/builder/tests/plugins/__snapshots__/checkSyntax.test.ts.snap b/packages/builder/builder/tests/plugins/__snapshots__/checkSyntax.test.ts.snap index 390ae7ea71ce..630561745f2c 100644 --- a/packages/builder/builder/tests/plugins/__snapshots__/checkSyntax.test.ts.snap +++ b/packages/builder/builder/tests/plugins/__snapshots__/checkSyntax.test.ts.snap @@ -18,3 +18,24 @@ exports[`plugins/check-syntax > should add check-syntax plugin properly 1`] = ` `; exports[`plugins/check-syntax > should not add check-syntax plugin when target node 1`] = `{}`; + +exports[`plugins/check-syntax > should use default browserlist as targets when only set checksyntax.exclude 1`] = ` +{ + "plugins": [ + CheckSyntaxPlugin { + "ecmaVersion": 5, + "errors": [], + "exclude": [ + /\\$\\.html/, + ], + "targets": [ + "iOS 9", + "Android 4.4", + "last 2 versions", + "> 0.2%", + "not dead", + ], + }, + ], +} +`; diff --git a/packages/builder/builder/tests/plugins/checkSyntax.test.ts b/packages/builder/builder/tests/plugins/checkSyntax.test.ts index 936a2ce998b2..0a1131039e7d 100644 --- a/packages/builder/builder/tests/plugins/checkSyntax.test.ts +++ b/packages/builder/builder/tests/plugins/checkSyntax.test.ts @@ -63,4 +63,45 @@ describe('plugins/check-syntax', () => { expect(chain.toConfig()).toMatchSnapshot(); }); + + it('should use default browserlist as targets when only set checksyntax.exclude', async () => { + let modifyBundlerChainCb: any; + + const api: any = { + modifyBundlerChain: (fn: any) => { + modifyBundlerChainCb = fn; + }, + getNormalizedConfig: () => ({ + security: { + checkSyntax: { + exclude: [/$.html/], + }, + }, + output: { + overrideBrowserslist: [ + 'iOS 9', + 'Android 4.4', + 'last 2 versions', + '> 0.2%', + 'not dead', + ], + }, + }), + context: { + rootPath: __dirname, + }, + }; + + builderPluginCheckSyntax().setup(api); + + const chain = await shared.getBundlerChain(); + + await modifyBundlerChainCb(chain, { + CHAIN_ID, + isProd: true, + target: 'web', + }); + + expect(chain.toConfig()).toMatchSnapshot(); + }); });