Skip to content

Commit 2d2cc16

Browse files
committed
docs: init README
1 parent 50d6ee2 commit 2d2cc16

File tree

3 files changed

+125
-17
lines changed

3 files changed

+125
-17
lines changed

README.md

Lines changed: 120 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
An Rsbuild plugin to check the syntax compatibility of output files.
44

5+
This plugin tries to find incompatible syntax in the output files with the current browserslist value. If any incompatible syntax is found, it will print detailed information to the terminal and abort the build.
6+
57
<p>
6-
<a href="https://npmjs.com/package/rsbuild-plugin-example">
7-
<img src="https://img.shields.io/npm/v/rsbuild-plugin-example?style=flat-square&colorA=564341&colorB=EDED91" alt="npm version" />
8+
<a href="https://npmjs.com/package/@rsbuild/plugin-check-syntax">
9+
<img src="https://img.shields.io/npm/v/@rsbuild/plugin-check-syntax?style=flat-square&colorA=564341&colorB=EDED91" alt="npm version" />
810
</a>
911
<img src="https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square&colorA=564341&colorB=EDED91" alt="license" />
1012
</p>
@@ -21,29 +23,135 @@ Add plugin to your `rsbuild.config.ts`:
2123

2224
```ts
2325
// rsbuild.config.ts
24-
import { pluginExample } from "rsbuild-plugin-example";
26+
import { pluginCheckSyntax } from "@rsbuild/plugin-check-syntax";
2527

2628
export default {
27-
plugins: [pluginExample()],
29+
plugins: [pluginCheckSyntax()],
2830
};
2931
```
3032

31-
## Options
33+
## Enable Detection
34+
35+
After registering the Check Syntax plugin, Rsbuild will perform syntax checking after production builds.
36+
37+
When Rsbuild detects incompatible advanced syntax in the build artifacts, it will print the error logs in the terminal and exit the current build process.
3238

33-
### foo
39+
### Error Logs
3440

35-
Some description.
41+
The format of the error logs is as follows, including the source file, artifact location, error reason, and source code:
3642

37-
- Type: `string`
38-
- Default: `undefined`
39-
- Example:
43+
```bash
44+
error [Syntax Checker] Find some syntax that does not match "ecmaVersion <= 2015":
45+
46+
Error 1
47+
source: /node_modules/foo/index.js:1:0
48+
output: /dist/static/js/main.3f7a4d7e.js:2:39400
49+
reason: Unexpected token (1:178)
50+
code:
51+
9 |
52+
10 | var b = 2;
53+
11 |
54+
> 12 | console.log(() => {
55+
13 | return a + b;
56+
14 | });
57+
15 |
58+
```
59+
60+
Currently, syntax checking is implemented based on AST parser. Each time it performs a check, it can only identify the first incompatible syntax found in the file. If there are multiple incompatible syntaxes in the file, you need to fix the detected syntax and re-run the check.
61+
62+
If the corresponding source location is not shown in the log, try setting [output.minify](/config/output/minify) to false and rebuild again.
63+
64+
Note that displaying source code information depends on the source map file. You can configure the [output.sourceMap](/config/output/source-map) option to generate source map files during production builds.
4065
4166
```js
42-
pluginExample({
43-
foo: "bar",
67+
export default {
68+
output: {
69+
sourceMap: {
70+
js:
71+
process.env.NODE_ENV === "production"
72+
? "source-map"
73+
: "cheap-module-source-map",
74+
},
75+
},
76+
};
77+
```
78+
79+
### Solutions
80+
81+
If a syntax error is detected, you can handle it in the following ways:
82+
83+
- If you want to downgrade this syntax to ensure good code compatibility, you can compile the corresponding module through the `source.include` config.
84+
- If you don't want to downgrade the syntax, you can adjust the project's browserslist to match the syntax.
85+
- If you do not want to check the syntax of specific files, you can use the `checkSyntax.exclude` configuration to exclude the files to be checked.
86+
87+
## Options
88+
89+
### targets
90+
91+
- **Type:** `string[]`
92+
- **Default:** `The browserslist configuration of the current project`
93+
94+
`targets` is the target browser range, its value is a standard browserslist. Check Syntax plugin will by default read the current project's browserslist configuration, so you usually don't need to manually configure the `targets` option.
95+
96+
Rsbuild will read the value of `targets` and automatically deduce the minimum ECMAScript syntax version that can be used in the build artifacts, such as ES5 or ES2015.
97+
98+
- **Example:**
99+
100+
For example, if the target browsers to be compatible with in the project are Chrome 53 and later versions, you can add the following configuration:
101+
102+
```ts
103+
pluginCheckSyntax({
104+
targets: ["chrome >= 53"],
105+
});
106+
```
107+
108+
Rsbuild will deduce that the ECMAScript syntax version that can be used with `chrome >= 53` is ES2015. When the build artifacts contain `ES2016` or higher syntax, it triggers syntax error prompts.
109+
110+
> If you want to learn more about how to use browserslist, please refer to ["Browserslist"](https://rsbuild.dev/guide/advanced/browserslist).
111+
112+
### ecmaVersion
113+
114+
- **Type:** `3 | 5 | 6 | 2015 | 2016 | 2017 | 2018 | 2019 | 2020 | 2021 | 2022 | 'latest'`
115+
- **Default:** `Automatically analyzed based on targets`
116+
117+
`ecmaVersion` represents the minimum ECMAScript syntax version that can be used in the build artifact.
118+
119+
> If `ecmaVersion` is not set, Check Syntax plugin will infer the ECMAScript version based on `targets`. Currently, the supported inference range is `es5` ~ `es2018`.
120+
121+
- **Example:**
122+
123+
For example, if the minimum ECMAScript syntax version that can be used in the build artifacts is ES2020, you can add the following configuration:
124+
125+
```ts
126+
pluginCheckSyntax({
127+
ecmaVersion: 2020,
44128
});
45129
```
46130
131+
At this time, the build artifacts can include all syntax supported by ES2020, such as optional chaining.
132+
133+
### exclude
134+
135+
- **Type:** `RegExp | RegExp[]`
136+
- **Default:** `undefined`
137+
138+
`exclude` is used to exclude a portion of files during detection. You can pass in one or more regular expressions to match the paths of source files. Files that match the regular expression will be ignored and will not trigger syntax checking.
139+
140+
- **Example:**
141+
142+
For example, to ignore files under the `node_modules/foo` directory:
143+
144+
```ts
145+
pluginCheckSyntax({
146+
exclude: /node_modules\/foo/,
147+
});
148+
```
149+
150+
## Limitations
151+
152+
1. Check Syntax plugin only checks incompatible syntax in the outputs and cannot detect missing polyfills.
153+
2. Check Syntax plugin's detection may be inaccurate in certain cases due to the limitations of AST parser. Check Syntax is implemented based on [Acorn](https://github.com/acornjs/acorn), which targets a specific ECMA version, such as ES2018. On the other hand, SWC can target specific syntax during compilation, such as ES2018's Object Spread. This difference means that using the same browserslist configuration, SWC's compiled output is correct, but Check Syntax may still fail detection.
154+
47155
## License
48156
49157
[MIT](./LICENSE).

playground/rsbuild.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { defineConfig } from '@rsbuild/core';
2-
import { pluginExample } from '../src';
2+
import { pluginCheckSyntax } from '../src';
33

44
export default defineConfig({
5-
plugins: [pluginExample()],
5+
plugins: [pluginCheckSyntax()],
66
});

test/basic/index.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { dirname } from 'node:path';
22
import { fileURLToPath } from 'node:url';
33
import { expect, test } from '@playwright/test';
44
import { createRsbuild } from '@rsbuild/core';
5-
import { pluginExample } from '../../src';
5+
import { pluginCheckSyntax } from '../../src';
66
import { getRandomPort } from '../helper';
77

88
const __dirname = dirname(fileURLToPath(import.meta.url));
@@ -11,7 +11,7 @@ test('should render page as expected', async ({ page }) => {
1111
const rsbuild = await createRsbuild({
1212
cwd: __dirname,
1313
rsbuildConfig: {
14-
plugins: [pluginExample()],
14+
plugins: [pluginCheckSyntax()],
1515
server: {
1616
port: getRandomPort(),
1717
},
@@ -30,7 +30,7 @@ test('should build succeed', async ({ page }) => {
3030
const rsbuild = await createRsbuild({
3131
cwd: __dirname,
3232
rsbuildConfig: {
33-
plugins: [pluginExample()],
33+
plugins: [pluginCheckSyntax()],
3434
},
3535
});
3636

0 commit comments

Comments
 (0)