|
| 1 | +import { join } from 'node:path'; |
1 | 2 | import type { CheckSyntaxOptions } from '@winner-fed/unplugin-check-syntax';
|
| 3 | +import { CheckSyntax, printErrors } from '@winner-fed/unplugin-check-syntax'; |
2 | 4 | import checkSyntaxRspack from '@winner-fed/unplugin-check-syntax/rspack';
|
3 | 5 | import checkSyntaxVite from '@winner-fed/unplugin-check-syntax/vite';
|
4 | 6 | import checkSyntaxWebpack from '@winner-fed/unplugin-check-syntax/webpack';
|
| 7 | +import { winPath } from '@winner-fed/utils'; |
5 | 8 | import type { IApi } from '@winner-fed/winjs';
|
6 | 9 |
|
7 |
| -export type CheckSyntax = boolean | CheckSyntaxOptions; |
8 |
| - |
9 | 10 | export default (api: IApi) => {
|
10 | 11 | api.describe({
|
11 | 12 | key: 'checkSyntax',
|
@@ -117,4 +118,31 @@ export default (api: IApi) => {
|
117 | 118 | ]);
|
118 | 119 | return config;
|
119 | 120 | });
|
| 121 | + |
| 122 | + // 增加检测 html 文件类型的功能 |
| 123 | + api.onBuildHtmlComplete(async (html: any) => { |
| 124 | + const htmlFiles = html?.htmlFiles || []; |
| 125 | + if (api.env === 'development' || htmlFiles.length === 0) { |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + const targets = api.config.targets; |
| 130 | + const checkerOptions = getCheckSyntaxOptions(targets); |
| 131 | + const checker = new CheckSyntax({ |
| 132 | + rootPath: api.paths.absOutputPath, |
| 133 | + ecmaVersion: 2015 as const, // 默认ES2015 |
| 134 | + ...checkerOptions, |
| 135 | + }); |
| 136 | + |
| 137 | + // 获取 dist 目录下的所有 html 文件 |
| 138 | + // 遍历 html 文件,检测是否存在语法错误 |
| 139 | + for (const htmlFile of htmlFiles) { |
| 140 | + const htmlFilePath = winPath( |
| 141 | + join(api.paths.absOutputPath, htmlFile.path), |
| 142 | + ); |
| 143 | + await checker.check(htmlFilePath); |
| 144 | + } |
| 145 | + |
| 146 | + printErrors(checker.errors, checker.ecmaVersion, checker.excludeErrorLogs); |
| 147 | + }); |
120 | 148 | };
|
0 commit comments