@canvasengine/compiler 2.0.0-beta.3 → 2.0.0-beta.31

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/index.ts CHANGED
@@ -10,6 +10,84 @@ const { generate } = pkg;
10
10
 
11
11
  const DEV_SRC = "../../src"
12
12
 
13
+ /**
14
+ * Formats a syntax error message with visual pointer to the error location
15
+ *
16
+ * @param {string} template - The template content that failed to parse
17
+ * @param {object} error - The error object with location information
18
+ * @returns {string} - Formatted error message with a visual pointer
19
+ *
20
+ * @example
21
+ * ```
22
+ * const errorMessage = showErrorMessage("<Canvas>test(d)</Canvas>", syntaxError);
23
+ * // Returns a formatted error message with an arrow pointing to 'd'
24
+ * ```
25
+ */
26
+ function showErrorMessage(template: string, error: any): string {
27
+ if (!error.location) {
28
+ return `Syntax error: ${error.message}`;
29
+ }
30
+
31
+ const lines = template.split('\n');
32
+ const { line, column } = error.location.start;
33
+ const errorLine = lines[line - 1] || '';
34
+
35
+ // Create a visual pointer with an arrow
36
+ const pointer = ' '.repeat(column - 1) + '^';
37
+
38
+ return `Syntax error at line ${line}, column ${column}: ${error.message}\n\n` +
39
+ `${errorLine}\n${pointer}\n`;
40
+ }
41
+
42
+ /**
43
+ * Vite plugin to load shader files (.frag, .vert, .wgsl) as text strings
44
+ *
45
+ * This plugin allows importing shader files directly as string literals in your code.
46
+ * It supports fragment shaders (.frag), vertex shaders (.vert), and WebGPU shaders (.wgsl).
47
+ * The content is loaded as a raw string and can be used directly with graphics APIs.
48
+ *
49
+ * @returns {object} - Vite plugin configuration object
50
+ *
51
+ * @example
52
+ * ```typescript
53
+ * // In your vite.config.ts
54
+ * import { shaderLoader } from './path/to/compiler'
55
+ *
56
+ * export default defineConfig({
57
+ * plugins: [shaderLoader()]
58
+ * })
59
+ *
60
+ * // In your code
61
+ * import fragmentShader from './shader.frag'
62
+ * import vertexShader from './shader.vert'
63
+ * import computeShader from './shader.wgsl'
64
+ *
65
+ * console.log(fragmentShader) // Raw shader code as string
66
+ * ```
67
+ */
68
+ export function shaderLoader() {
69
+ const filter = createFilter(/\.(frag|vert|wgsl)$/);
70
+
71
+ return {
72
+ name: "vite-plugin-shader-loader",
73
+ transform(code: string, id: string) {
74
+ if (!filter(id)) return;
75
+
76
+ // Escape the shader code to be safely embedded in a JavaScript string
77
+ const escapedCode = code
78
+ .replace(/\\/g, '\\\\') // Escape backslashes
79
+ .replace(/`/g, '\\`') // Escape backticks
80
+ .replace(/\$/g, '\\$'); // Escape dollar signs
81
+
82
+ // Return the shader content as a default export string
83
+ return {
84
+ code: `export default \`${escapedCode}\`;`,
85
+ map: null,
86
+ };
87
+ },
88
+ };
89
+ }
90
+
13
91
  export default function canvasengine() {
14
92
  const filter = createFilter("**/*.ce");
15
93
 
@@ -35,14 +113,22 @@ export default function canvasengine() {
35
113
  "NineSliceSprite",
36
114
  "Rect",
37
115
  "Circle",
116
+ "Ellipse",
117
+ "Triangle",
38
118
  "TilingSprite",
39
- "svg"
119
+ "svg",
120
+ "Video",
121
+ "Mesh",
122
+ "Svg",
123
+ "DOMContainer",
124
+ "DOMElement",
125
+ "Button"
40
126
  ];
41
127
 
42
128
  return {
43
129
  name: "vite-plugin-ce",
44
130
  transform(code: string, id: string) {
45
- if (!filter(id)) return;
131
+ if (!filter(id)) return null;
46
132
 
47
133
  // Extract the script content
48
134
  const scriptMatch = code.match(/<script>([\s\S]*?)<\/script>/);
@@ -51,13 +137,14 @@ export default function canvasengine() {
51
137
  // Transform SVG tags to Svg components
52
138
  let template = code.replace(/<script>[\s\S]*?<\/script>/, "")
53
139
  .replace(/^\s+|\s+$/g, '');
54
-
55
- // Add SVG transformation
56
- template = template.replace(/<svg>([\s\S]*?)<\/svg>/g, (match, content) => {
57
- return `<Svg content="${content.trim()}" />`;
58
- });
59
-
60
- const parsedTemplate = parser.parse(template);
140
+
141
+ let parsedTemplate;
142
+ try {
143
+ parsedTemplate = parser.parse(template);
144
+ } catch (error) {
145
+ const errorMsg = showErrorMessage(template, error);
146
+ throw new Error(`Error parsing template in file ${id}:\n${errorMsg}`);
147
+ }
61
148
 
62
149
  // trick to avoid typescript remove imports in scriptContent
63
150
  scriptContent += FLAG_COMMENT + parsedTemplate
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@canvasengine/compiler",
3
- "version": "2.0.0-beta.3",
3
+ "version": "2.0.0-beta.31",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "scripts": {
8
- "build": "tsup",
8
+ "build": "tsup && cp grammar.pegjs ../../docs/public/grammar.pegjs",
9
9
  "dev": "tsup --watch"
10
10
  },
11
11
  "type": "module",