Check Syntac plugin is used to analyze whether there is incompatible advanced syntax in the build artifacts under the current browser scope. If any incompatible syntax is found, detailed information will be printed to the terminal.
You can install the plugin using the following command:
You can register the plugin in the rsbuild.config.ts
file:
After registering the Check Syntax plugin, Rsbuild will perform syntax checking after production builds.
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.
The format of the error logs is as follows, including the source file, artifact location, error reason, and source code:
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.
If the corresponding source location is not shown in the log, try setting output.disableMinimize to true and rebuild again.
Note that displaying source code information depends on the source map file. You can configure the output.sourceMap option to generate source map files during production builds.
If a syntax error is detected, you can handle it in the following ways:
source.include
config.checkSyntax.exclude
configuration to exclude the files to be checked.string[]
The browserslist configuration of the current project
targets
is the target browser range of the project. Its value is a standard browserslist array. If you are not familiar with the usage of browserslist, please refer to "Browserslist".
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 ES6
.
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:
Rsbuild will deduce that the ECMAScript syntax version that can be used with chrome >= 53
is ES6
. When the build artifacts contain ES2016
or higher syntax, it triggers syntax error prompts.
Please note that Rsbuild does not support automatic analysis of syntax versions above ES6 based on targets
. If the syntax version compatible with your build artifacts exceeds ES6, please use checkSyntax.ecmaVersion
to set it.
3 | 5 | 6 | 2015 | 2016 | 2017 | 2018 | 2019 | 2020 | 2021 | 2022 | 'latest'
Automatically analyzed based on targets
ecmaVersion
represents the minimum ECMAScript syntax version that can be used in the build artifact. The priority of ecmaVersion
is higher than targets
.
For example, if the minimum ECMAScript syntax version that can be used in the build artifacts is ES2020
, you can add the following configuration:
At this time, the build artifacts can include all syntax supported by ES2020
, such as optional chaining.
RegExp | RegExp[]
undefined
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.
For example, to ignore files under the node_modules/foo
directory: