unplugin-check-syntax
Een algemene syntaxcontrole plug-in die meerdere buildtools ondersteunt (zoals Webpack, Vite, Rollup, esbuild, Rspack, etc.), bedoeld om ECMAScript-syntaxiscompatibiliteit te controleren in JavaScript- en HTML-bestanden. Deze plug-in is geïnspireerd op rsbuild-plugin-check-syntax.
Functies en kenmerken
- 🔧 Ondersteuning voor meerdere buildtools: Ondersteunt Webpack, Vite, Rollup, esbuild, Rspack, enz.
- 📝 Meerdere bestandstypen: Ondersteunt JavaScript-bestanden en inline scripts in HTML-bestanden
- 🎯 Precieze lokalisatie: Ondersteunt sourcemap, kan terugtraceren naar de bronbestandslocatie
- ⚙️ Flexibele configuratie: Ondersteunt het uitsluiten van bestanden, handmatige inclusie, filteren van fouttypes, enz.
- 📁 Slimme bestandsdetectie: Ondersteunt glob-patronen om extra JS/HTML-bestanden handmatig toe te voegen
- 🌐 Browserslist-integratie: Ondersteunt automatische conversie van browserslist-configuratie naar ECMAScript-versie
Installatie
npm install @winner-fed/unplugin-check-syntax --save-dev
Gebruiksaanwijzing
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'],
}),
],
}
Configuratie-opties
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')[];
}
Gebruiksvoorbeeld
Basisconfiguratie
// 使用 browserslist 配置
checkSyntax({
targets: ['> 1%', 'last 2 versions', 'not dead'],
})// 直接指定 ECMAScript 版本
checkSyntax({
ecmaVersion: 'es2015',
})
Uitsluitingsbestanden
checkSyntax({
targets: ['> 1%', 'last 2 versions'],
// 排除 node_modules 和测试文件
exclude: [/node_modules/, /\.test\.js$/],
// 排除特定输出文件
excludeOutput: [/vendor\.js$/],
})
Extra bestanden opnemen
checkSyntax({
targets: ['> 1%', 'last 2 versions'],
// 手动包含额外的文件进行检查
include: [
'src/legacy//*.js', // 包含 legacy 目录下的所有 JS 文件
'public/scripts/app.js', // 包含特定的公共脚本文件
'docs//*.html', // 包含文档中的 HTML 文件
'/absolute/path/to/file.js' // 支持绝对路径
],
})
Aangepaste foutuitvoer
checkSyntax({
targets: ['> 1%', 'last 2 versions'],
// 只显示错误原因,不显示代码片段
excludeErrorLogs: ['code', 'output'],
})
Migratiehandleiding
Als je migreert vanaf @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'],
}),
],
})
Werkingsprincipe
- Controle na het bouwen: De plugin controleert de uitvoerbestanden na voltooiing van de build
- Syntaxisanalyse: Gebruikt de Acorn-parser om de JavaScript-syntaxis te controleren
- Compatibiliteitscontrole: Controleert de syntaxiscompatibiliteit volgens de geconfigureerde ECMAScript-versie
- Foutrapportage: Biedt gedetailleerde foutinformatie, inclusief bestandslocatie en broncodefragment
- Sourcemap-ondersteuning: Als er een sourcemap is, wordt er teruggezocht naar het oorspronkelijke bronbestand
Licentie
MIT-licentie
Bijdragen
Issues en Pull Requests zijn van harte welkom!
--- Tranlated By Open Ai Tx | Last indexed: 2025-07-21 ---