unplugin-check-syntax
A universal syntax checking plugin that supports multiple build tools (Webpack, Vite, Rollup, esbuild, Rspack, etc.), used to check ECMAScript syntax compatibility in JavaScript and HTML files. This plugin references rsbuild-plugin-check-syntax.
Features
- 🔧 Multiple Build Tool Support: Supports Webpack, Vite, Rollup, esbuild, Rspack, etc.
- 📝 Multiple File Types: Supports JavaScript files and inline scripts in HTML files
- 🎯 Precise Positioning: Supports sourcemaps, allowing you to trace back to the original file location
- ⚙️ Flexible Configuration: Supports file exclusion, manual inclusion, error type filtering, and other options
- 📁 Intelligent File Discovery: Supports glob patterns to manually include additional JS/HTML files
- 🌐 Browserslist Integration: Supports automatic conversion from browserslist configuration to ECMAScript version
Installation
npm install @winner-fed/unplugin-check-syntax --save-dev
Usage
Vite
// vite.config.ts
import { defineConfig } from 'vite'
import checkSyntax from '@winner-fed/unplugin-check-syntax/vite'export default defineConfig({
plugins: [
checkSyntax({
targets: ['> 1%', 'last 2 versions'],
// 或者直接指定 ECMAScript 版本
// ecmaVersion: 'es2015'
}),
],
})
Webpack
// webpack.config.js
const checkSyntax = require('@winner-fed/unplugin-check-syntax/webpack')module.exports = {
plugins: [
checkSyntax({
targets: ['> 1%', 'last 2 versions'],
}),
],
}
Rollup
// rollup.config.js
import checkSyntax from '@winner-fed/unplugin-check-syntax/rollup'export default {
plugins: [
checkSyntax({
targets: ['> 1%', 'last 2 versions'],
}),
],
}
esbuild
// esbuild.config.js
const { build } = require('esbuild')
const checkSyntax = require('@winner-fed/unplugin-check-syntax/esbuild')build({
plugins: [
checkSyntax({
targets: ['> 1%', 'last 2 versions'],
}),
],
})
Rspack
// rspack.config.js
const checkSyntax = require('@winner-fed/unplugin-check-syntax/rspack')module.exports = {
plugins: [
checkSyntax({
targets: ['> 1%', 'last 2 versions'],
}),
],
}
Configuration Options
interface CheckSyntaxOptions {
/
- 目标浏览器范围,使用 browserslist 格式
*/
targets?: string[];
/
- 直接指定 ECMAScript 版本(优先级高于 targets)
*/
ecmaVersion?: 'es3' | 'es5' | 'es2015' | 'es2016' | 'es2017' | 'es2018' | 'es2019' | 'es2020' | 'es2021' | 'es2022' | 'latest';
/
- 排除源文件检查,支持正则表达式
*/
exclude?: RegExp | RegExp[];
/
- 排除输出文件检查,支持正则表达式
*/
excludeOutput?: RegExp | RegExp[];
/
- 手动包含额外的 JS 或 HTML 文件进行语法检查
- 支持文件路径(相对于项目根目录)或 glob 模式
*/
include?: string[];
/
- 排除特定的错误日志类型
*/
excludeErrorLogs?: ('source' | 'output' | 'reason' | 'code')[];
}
Usage Example
Basic Configuration
// 使用 browserslist 配置
checkSyntax({
targets: ['> 1%', 'last 2 versions', 'not dead'],
})// 直接指定 ECMAScript 版本
checkSyntax({
ecmaVersion: 'es2015',
})
Exclude Files
checkSyntax({
targets: ['> 1%', 'last 2 versions'],
// 排除 node_modules 和测试文件
exclude: [/node_modules/, /\.test\.js$/],
// 排除特定输出文件
excludeOutput: [/vendor\.js$/],
})
Including Additional Files
checkSyntax({
targets: ['> 1%', 'last 2 versions'],
// 手动包含额外的文件进行检查
include: [
'src/legacy//*.js', // 包含 legacy 目录下的所有 JS 文件
'public/scripts/app.js', // 包含特定的公共脚本文件
'docs//*.html', // 包含文档中的 HTML 文件
'/absolute/path/to/file.js' // 支持绝对路径
],
})
Custom Error Output
checkSyntax({
targets: ['> 1%', 'last 2 versions'],
// 只显示错误原因,不显示代码片段
excludeErrorLogs: ['code', 'output'],
})
Migration Guide
If you are migrating from @rsbuild/plugin-check-syntax:
- import { pluginCheckSyntax } from '@rsbuild/plugin-check-syntax'
+ import checkSyntax from '@winner-fed/unplugin-check-syntax/vite' // 或其他构建工具export default defineConfig({
plugins: [
- pluginCheckSyntax({
+ checkSyntax({
targets: ['> 1%', 'last 2 versions'],
}),
],
})
Working Principle
- Post-build Check: The plugin checks the output files after the build is completed
- Syntax Parsing: Uses the Acorn parser to check JavaScript syntax
- Compatibility Validation: Validates syntax compatibility based on the configured ECMAScript version
- Error Reporting: Provides detailed error information, including file location and source code snippets
- Sourcemap Support: If a sourcemap exists, it traces back to the original source files
License
MIT License
Contribution
Issues and Pull Requests are welcome!
--- Tranlated By Open Ai Tx | Last indexed: 2025-07-21 ---