@proteus-vue/compiler 0.3.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,54 @@
1
+ /** 编译产物校验错误(携带源文件名,便于定位) */
2
+ export class CompilerError extends Error {
3
+ constructor(filename, message) {
4
+ super(`[proteus-compiler] ${filename}: ${message}`);
5
+ this.filename = filename;
6
+ this.name = 'CompilerError';
7
+ }
8
+ }
9
+ /** JS 语法校验:new Function 仅解析不执行(产物为 Page()/Component() 调用,无 import/export) */
10
+ export function validateJs(js) {
11
+ try {
12
+ // eslint-disable-next-line no-new-func
13
+ new Function(js);
14
+ return { ok: true };
15
+ }
16
+ catch (err) {
17
+ return { ok: false, error: err.message };
18
+ }
19
+ }
20
+ /** WXML 标签配对校验(先剥离注释,避免行号注释中的标签文本干扰) */
21
+ export function validateWxml(wxml) {
22
+ const withoutComments = wxml.replace(/<!--[\s\S]*?-->/g, '');
23
+ const tagRe = /<\/?([a-zA-Z][\w-]*)(?:"[^"]*"|'[^']*'|[^>"'])*\/?>/g;
24
+ const stack = [];
25
+ let m;
26
+ while ((m = tagRe.exec(withoutComments))) {
27
+ const full = m[0];
28
+ const name = m[1];
29
+ if (full.startsWith('</')) {
30
+ const top = stack.pop();
31
+ if (top !== name) {
32
+ return { ok: false, error: `</${name}> 与 <${top ?? '(无)'}> 不匹配(位置 ${m.index})` };
33
+ }
34
+ }
35
+ else if (!full.endsWith('/>')) {
36
+ stack.push(name);
37
+ }
38
+ }
39
+ if (stack.length) {
40
+ return { ok: false, error: `<${stack[stack.length - 1]}> 未闭合` };
41
+ }
42
+ return { ok: true };
43
+ }
44
+ /** 校验整包编译结果,失败抛 CompilerError */
45
+ export function assertValidResult(result, filename) {
46
+ const jsCheck = validateJs(result.js);
47
+ if (!jsCheck.ok) {
48
+ throw new CompilerError(filename, `js 产物语法错误:${jsCheck.error}`);
49
+ }
50
+ const wxmlCheck = validateWxml(result.wxml);
51
+ if (!wxmlCheck.ok) {
52
+ throw new CompilerError(filename, `wxml 产物结构错误:${wxmlCheck.error}`);
53
+ }
54
+ }
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@proteus-vue/compiler",
3
+ "version": "0.3.0-beta.0",
4
+ "description": "Proteus 编译引擎 —— 标准 Vue SFC → 小程序四件套(纯函数、零运行时依赖、规则注册表 + AI 说明书)",
5
+ "type": "module",
6
+ "license": "Apache-2.0",
7
+ "main": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js",
13
+ "default": "./dist/index.js"
14
+ },
15
+ "./style-safety": {
16
+ "types": "./dist/style-safety/index.d.ts",
17
+ "import": "./dist/style-safety/index.js",
18
+ "default": "./dist/style-safety/index.js"
19
+ },
20
+ "./package.json": "./package.json"
21
+ },
22
+ "files": [
23
+ "dist",
24
+ "README.md"
25
+ ],
26
+ "scripts": {
27
+ "prepare": "npm run build",
28
+ "build": "tsc -p tsconfig.build.json --emitDeclarationOnly && esbuild src/index.ts src/style-safety/index.ts --bundle --format=esm --platform=node --outdir=dist --external:@vue/compiler-sfc --external:@vue/compiler-dom --external:@proteus-vue/contracts"
29
+ },
30
+ "peerDependencies": {
31
+ "@vue/compiler-sfc": "^3.4",
32
+ "@vue/compiler-dom": "^3.4"
33
+ },
34
+ "devDependencies": {
35
+ "@vue/compiler-sfc": "^3.5.42",
36
+ "@vue/compiler-dom": "^3.5.42"
37
+ },
38
+ "dependencies": {
39
+ "@proteus-vue/contracts": "0.1.0",
40
+ "@proteus-vue/types": "0.1.0"
41
+ }
42
+ }