@canvasengine/compiler 2.0.0-beta.1 → 2.0.0-beta.2
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.
- package/package.json +5 -4
- package/tests/compiler.spec.ts +1 -1
- package/dist/index.d.ts +0 -9
- package/dist/index.js +0 -119
- package/dist/index.js.map +0 -1
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@canvasengine/compiler",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"build": "tsup",
|
|
9
|
-
"
|
|
9
|
+
"dev": "tsup --watch"
|
|
10
10
|
},
|
|
11
11
|
"type": "module",
|
|
12
12
|
"publishConfig": {
|
|
@@ -18,10 +18,11 @@
|
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"acorn": "^8.14.0",
|
|
20
20
|
"peggy": "^4.2.0",
|
|
21
|
+
"typescript": "^5.7.2",
|
|
21
22
|
"vite": "^6.0.3"
|
|
22
23
|
},
|
|
23
24
|
"devDependencies": {
|
|
24
|
-
"
|
|
25
|
-
"
|
|
25
|
+
"@types/node": "^22.10.2",
|
|
26
|
+
"tsup": "^8.3.5"
|
|
26
27
|
}
|
|
27
28
|
}
|
package/tests/compiler.spec.ts
CHANGED
package/dist/index.d.ts
DELETED
package/dist/index.js
DELETED
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
// index.ts
|
|
2
|
-
import { createFilter } from "vite";
|
|
3
|
-
import { parse } from "acorn";
|
|
4
|
-
import fs from "fs";
|
|
5
|
-
import pkg from "peggy";
|
|
6
|
-
import path from "path";
|
|
7
|
-
import * as ts from "typescript";
|
|
8
|
-
import { fileURLToPath } from "url";
|
|
9
|
-
var { generate } = pkg;
|
|
10
|
-
var DEV_SRC = "../../src";
|
|
11
|
-
function canvasengine() {
|
|
12
|
-
const filter = createFilter("**/*.ce");
|
|
13
|
-
const __filename = fileURLToPath(import.meta.url);
|
|
14
|
-
const __dirname = path.dirname(__filename);
|
|
15
|
-
const grammar = fs.readFileSync(
|
|
16
|
-
path.join(__dirname, "grammar.pegjs").replace("dist/grammar.pegjs", "grammar.pegjs"),
|
|
17
|
-
"utf8"
|
|
18
|
-
);
|
|
19
|
-
const parser = generate(grammar);
|
|
20
|
-
const isDev = process.env.NODE_ENV === "dev";
|
|
21
|
-
const FLAG_COMMENT = "/*--[TPL]--*/";
|
|
22
|
-
const PRIMITIVE_COMPONENTS = [
|
|
23
|
-
"Canvas",
|
|
24
|
-
"Sprite",
|
|
25
|
-
"Text",
|
|
26
|
-
"Joystick",
|
|
27
|
-
"Viewport",
|
|
28
|
-
"Graphics",
|
|
29
|
-
"Container",
|
|
30
|
-
"ImageMap",
|
|
31
|
-
"NineSliceSprite",
|
|
32
|
-
"Rect",
|
|
33
|
-
"Circle",
|
|
34
|
-
"svg"
|
|
35
|
-
];
|
|
36
|
-
return {
|
|
37
|
-
name: "vite-plugin-ce",
|
|
38
|
-
transform(code, id) {
|
|
39
|
-
if (!filter(id)) return;
|
|
40
|
-
const scriptMatch = code.match(/<script>([\s\S]*?)<\/script>/);
|
|
41
|
-
let scriptContent = scriptMatch ? scriptMatch[1].trim() : "";
|
|
42
|
-
let template = code.replace(/<script>[\s\S]*?<\/script>/, "").replace(/^\s+|\s+$/g, "");
|
|
43
|
-
template = template.replace(/<svg>([\s\S]*?)<\/svg>/g, (match, content) => {
|
|
44
|
-
return `<Svg content="${content.trim()}" />`;
|
|
45
|
-
});
|
|
46
|
-
const parsedTemplate = parser.parse(template);
|
|
47
|
-
scriptContent += FLAG_COMMENT + parsedTemplate;
|
|
48
|
-
let transpiledCode = ts.transpileModule(scriptContent, {
|
|
49
|
-
compilerOptions: {
|
|
50
|
-
module: ts.ModuleKind.Preserve
|
|
51
|
-
}
|
|
52
|
-
}).outputText;
|
|
53
|
-
transpiledCode = transpiledCode.split(FLAG_COMMENT)[0];
|
|
54
|
-
const parsed = parse(transpiledCode, {
|
|
55
|
-
sourceType: "module",
|
|
56
|
-
ecmaVersion: 2020
|
|
57
|
-
});
|
|
58
|
-
const imports = parsed.body.filter(
|
|
59
|
-
(node) => node.type === "ImportDeclaration"
|
|
60
|
-
);
|
|
61
|
-
const nonImportCode = parsed.body.filter((node) => node.type !== "ImportDeclaration").map((node) => transpiledCode.slice(node.start, node.end)).join("\n");
|
|
62
|
-
let importsCode = imports.map((imp) => {
|
|
63
|
-
let importCode = transpiledCode.slice(imp.start, imp.end);
|
|
64
|
-
if (isDev && importCode.includes("from 'canvasengine'")) {
|
|
65
|
-
importCode = importCode.replace(
|
|
66
|
-
"from 'canvasengine'",
|
|
67
|
-
`from '${DEV_SRC}'`
|
|
68
|
-
);
|
|
69
|
-
}
|
|
70
|
-
return importCode;
|
|
71
|
-
}).join("\n");
|
|
72
|
-
const requiredImports = ["h", "computed", "cond", "loop"];
|
|
73
|
-
const missingImports = requiredImports.filter(
|
|
74
|
-
(importName) => !imports.some(
|
|
75
|
-
(imp) => imp.specifiers && imp.specifiers.some(
|
|
76
|
-
(spec) => spec.type === "ImportSpecifier" && spec.imported && "name" in spec.imported && spec.imported.name === importName
|
|
77
|
-
)
|
|
78
|
-
)
|
|
79
|
-
);
|
|
80
|
-
if (missingImports.length > 0) {
|
|
81
|
-
const additionalImportCode = `import { ${missingImports.join(
|
|
82
|
-
", "
|
|
83
|
-
)} } from ${isDev ? `'${DEV_SRC}'` : "'canvasengine'"};`;
|
|
84
|
-
importsCode = `${additionalImportCode}
|
|
85
|
-
${importsCode}`;
|
|
86
|
-
}
|
|
87
|
-
const primitiveImports = PRIMITIVE_COMPONENTS.filter(
|
|
88
|
-
(component) => parsedTemplate.includes(`h(${component}`)
|
|
89
|
-
);
|
|
90
|
-
primitiveImports.forEach((component) => {
|
|
91
|
-
const importStatement = `import { ${component} } from ${isDev ? `'${DEV_SRC}'` : "'canvasengine'"};`;
|
|
92
|
-
if (!importsCode.includes(importStatement)) {
|
|
93
|
-
importsCode = `${importStatement}
|
|
94
|
-
${importsCode}`;
|
|
95
|
-
}
|
|
96
|
-
});
|
|
97
|
-
const output = String.raw`
|
|
98
|
-
${importsCode}
|
|
99
|
-
import { useProps, useDefineProps } from ${isDev ? `'${DEV_SRC}'` : "'canvasengine'"}
|
|
100
|
-
|
|
101
|
-
export default function component($$props) {
|
|
102
|
-
const $props = useProps($$props)
|
|
103
|
-
const defineProps = useDefineProps($$props)
|
|
104
|
-
${nonImportCode}
|
|
105
|
-
let $this = ${parsedTemplate}
|
|
106
|
-
return $this
|
|
107
|
-
}
|
|
108
|
-
`;
|
|
109
|
-
return {
|
|
110
|
-
code: output,
|
|
111
|
-
map: null
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
export {
|
|
117
|
-
canvasengine as default
|
|
118
|
-
};
|
|
119
|
-
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../index.ts"],"sourcesContent":["import { createFilter } from \"vite\";\nimport { parse } from \"acorn\";\nimport fs from \"fs\";\nimport pkg from \"peggy\";\nimport path from \"path\";\nimport * as ts from \"typescript\";\nimport { fileURLToPath } from 'url';\n\nconst { generate } = pkg;\n\nconst DEV_SRC = \"../../src\"\n\nexport default function canvasengine() {\n const filter = createFilter(\"**/*.ce\");\n\n // Convert import.meta.url to a file path\n const __filename = fileURLToPath(import.meta.url);\n const __dirname = path.dirname(__filename);\n\n const grammar = fs.readFileSync(\n path.join(__dirname, \"grammar.pegjs\").replace(\"dist/grammar.pegjs\", \"grammar.pegjs\"), \n \"utf8\");\n const parser = generate(grammar);\n const isDev = process.env.NODE_ENV === \"dev\";\n const FLAG_COMMENT = \"/*--[TPL]--*/\";\n\n const PRIMITIVE_COMPONENTS = [\n \"Canvas\",\n \"Sprite\",\n \"Text\",\n \"Joystick\",\n \"Viewport\",\n \"Graphics\",\n \"Container\",\n \"ImageMap\",\n \"NineSliceSprite\",\n \"Rect\",\n \"Circle\",\n \"svg\"\n ];\n\n return {\n name: \"vite-plugin-ce\",\n transform(code: string, id: string) {\n if (!filter(id)) return;\n\n // Extract the script content\n const scriptMatch = code.match(/<script>([\\s\\S]*?)<\\/script>/);\n let scriptContent = scriptMatch ? scriptMatch[1].trim() : \"\";\n \n // Transform SVG tags to Svg components\n let template = code.replace(/<script>[\\s\\S]*?<\\/script>/, \"\")\n .replace(/^\\s+|\\s+$/g, '');\n \n // Add SVG transformation\n template = template.replace(/<svg>([\\s\\S]*?)<\\/svg>/g, (match, content) => {\n return `<Svg content=\"${content.trim()}\" />`;\n });\n \n const parsedTemplate = parser.parse(template);\n\n // trick to avoid typescript remove imports in scriptContent\n scriptContent += FLAG_COMMENT + parsedTemplate\n\n let transpiledCode = ts.transpileModule(scriptContent, {\n compilerOptions: {\n module: ts.ModuleKind.Preserve,\n },\n }).outputText;\n\n // remove code after /*---*/\n transpiledCode = transpiledCode.split(FLAG_COMMENT)[0]\n\n // Use Acorn to parse the script content\n const parsed = parse(transpiledCode, {\n sourceType: \"module\",\n ecmaVersion: 2020,\n });\n\n // Extract imports\n const imports = parsed.body.filter(\n (node) => node.type === \"ImportDeclaration\"\n );\n\n // Extract non-import statements from scriptContent\n const nonImportCode = parsed.body\n .filter((node) => node.type !== \"ImportDeclaration\")\n .map((node) => transpiledCode.slice(node.start, node.end))\n .join(\"\\n\");\n\n let importsCode = imports\n .map((imp) => {\n let importCode = transpiledCode.slice(imp.start, imp.end);\n if (isDev && importCode.includes(\"from 'canvasengine'\")) {\n importCode = importCode.replace(\n \"from 'canvasengine'\",\n `from '${DEV_SRC}'`\n );\n }\n return importCode;\n })\n .join(\"\\n\");\n\n // Define an array for required imports\n const requiredImports = [\"h\", \"computed\", \"cond\", \"loop\"];\n\n // Check for missing imports\n const missingImports = requiredImports.filter(\n (importName) =>\n !imports.some(\n (imp) =>\n imp.specifiers &&\n imp.specifiers.some(\n (spec) =>\n spec.type === \"ImportSpecifier\" &&\n spec.imported && \n 'name' in spec.imported &&\n spec.imported.name === importName\n )\n )\n );\n\n // Add missing imports\n if (missingImports.length > 0) {\n const additionalImportCode = `import { ${missingImports.join(\n \", \"\n )} } from ${isDev ? `'${DEV_SRC}'` : \"'canvasengine'\"};`;\n importsCode = `${additionalImportCode}\\n${importsCode}`;\n }\n\n // Check for primitive components in parsedTemplate\n const primitiveImports = PRIMITIVE_COMPONENTS.filter((component) =>\n parsedTemplate.includes(`h(${component}`)\n );\n\n // Add missing imports for primitive components\n primitiveImports.forEach((component) => {\n const importStatement = `import { ${component} } from ${\n isDev ? `'${DEV_SRC}'` : \"'canvasengine'\"\n };`;\n if (!importsCode.includes(importStatement)) {\n importsCode = `${importStatement}\\n${importsCode}`;\n }\n });\n\n // Generate the output\n const output = String.raw`\n ${importsCode}\n import { useProps, useDefineProps } from ${isDev ? `'${DEV_SRC}'` : \"'canvasengine'\"}\n\n export default function component($$props) {\n const $props = useProps($$props)\n const defineProps = useDefineProps($$props)\n ${nonImportCode}\n let $this = ${parsedTemplate}\n return $this\n }\n `;\n\n return {\n code: output,\n map: null,\n };\n },\n };\n}\n"],"mappings":";AAAA,SAAS,oBAAoB;AAC7B,SAAS,aAAa;AACtB,OAAO,QAAQ;AACf,OAAO,SAAS;AAChB,OAAO,UAAU;AACjB,YAAY,QAAQ;AACpB,SAAS,qBAAqB;AAE9B,IAAM,EAAE,SAAS,IAAI;AAErB,IAAM,UAAU;AAED,SAAR,eAAgC;AACrC,QAAM,SAAS,aAAa,SAAS;AAGrC,QAAM,aAAa,cAAc,YAAY,GAAG;AAChD,QAAM,YAAY,KAAK,QAAQ,UAAU;AAEzC,QAAM,UAAU,GAAG;AAAA,IACjB,KAAK,KAAK,WAAW,eAAe,EAAE,QAAQ,sBAAsB,eAAe;AAAA,IACrF;AAAA,EAAM;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