Skip to content

Commit

Permalink
fix(builder): checkSyntax targets should get default browserlist when…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
fireairforce committed Jun 29, 2023
1 parent f981153 commit b10045f
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 9 deletions.
8 changes: 8 additions & 0 deletions .changeset/fix-checksyntax-exclude.md
Original file line number Diff line number Diff line change
@@ -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 值
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export type SriOptions = {
};

export interface CheckSyntaxOptions {
targets: string[];
targets?: string[];
exclude?: RegExp | Array<RegExp>;
}

Expand Down
15 changes: 8 additions & 7 deletions packages/builder/builder/src/plugins/checkSyntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
getBrowserslistWithDefault,
DefaultBuilderPlugin,
SharedNormalizedConfig,
CheckSyntaxOptions,
} from '@modern-js/builder-shared';

export function builderPluginCheckSyntax(): DefaultBuilderPlugin {
Expand Down Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
},
],
}
`;
41 changes: 41 additions & 0 deletions packages/builder/builder/tests/plugins/checkSyntax.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});

0 comments on commit b10045f

Please sign in to comment.