@portel/photon-core 2.5.2 → 2.5.4
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/dist/compiler.d.ts +0 -8
- package/dist/compiler.d.ts.map +1 -1
- package/dist/compiler.js +74 -1
- package/dist/compiler.js.map +1 -1
- package/package.json +1 -1
- package/src/compiler.ts +98 -1
package/dist/compiler.d.ts
CHANGED
|
@@ -7,14 +7,6 @@
|
|
|
7
7
|
* NOTE: No esbuild dependency in package.json — the consumer must provide it.
|
|
8
8
|
* Uses `await import('esbuild')` for dynamic resolution.
|
|
9
9
|
*/
|
|
10
|
-
/**
|
|
11
|
-
* Compile a .photon.ts file to JavaScript and cache the result
|
|
12
|
-
*
|
|
13
|
-
* @param tsFilePath - Absolute path to the TypeScript source file
|
|
14
|
-
* @param options.cacheDir - Directory to store compiled output
|
|
15
|
-
* @param options.content - Optional pre-read file content (avoids extra read)
|
|
16
|
-
* @returns Absolute path to the compiled .mjs file
|
|
17
|
-
*/
|
|
18
10
|
export declare function compilePhotonTS(tsFilePath: string, options: {
|
|
19
11
|
cacheDir: string;
|
|
20
12
|
content?: string;
|
package/dist/compiler.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compiler.d.ts","sourceRoot":"","sources":["../src/compiler.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;
|
|
1
|
+
{"version":3,"file":"compiler.d.ts","sourceRoot":"","sources":["../src/compiler.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AA2GH,wBAAsB,eAAe,CACnC,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,OAAO,CAAC,EAAE,MAAM,CAAA;CAAE,GAC9C,OAAO,CAAC,MAAM,CAAC,CAmCjB"}
|
package/dist/compiler.js
CHANGED
|
@@ -18,8 +18,81 @@ import * as crypto from 'crypto';
|
|
|
18
18
|
* @param options.content - Optional pre-read file content (avoids extra read)
|
|
19
19
|
* @returns Absolute path to the compiled .mjs file
|
|
20
20
|
*/
|
|
21
|
+
/**
|
|
22
|
+
* Transform arrays to reactive collections for PhotonMCP classes
|
|
23
|
+
*
|
|
24
|
+
* ZERO-EFFORT REACTIVITY: If a class extends PhotonMCP and has array properties,
|
|
25
|
+
* this transform automatically:
|
|
26
|
+
* 1. Injects `import { Array as ReactiveArray } from '@portel/photon-core'`
|
|
27
|
+
* 2. Transforms `= []` to `= new ReactiveArray()` for class properties
|
|
28
|
+
*
|
|
29
|
+
* Result: Developers write normal code, arrays are automatically reactive.
|
|
30
|
+
*
|
|
31
|
+
* ```typescript
|
|
32
|
+
* // Developer writes this (normal TypeScript):
|
|
33
|
+
* export default class TodoList extends PhotonMCP {
|
|
34
|
+
* items: Task[] = [];
|
|
35
|
+
* async add(text: string) { this.items.push({...}); }
|
|
36
|
+
* }
|
|
37
|
+
*
|
|
38
|
+
* // Compiler transforms to:
|
|
39
|
+
* import { Array as ReactiveArray } from '@portel/photon-core';
|
|
40
|
+
* export default class TodoList extends PhotonMCP {
|
|
41
|
+
* items = new ReactiveArray();
|
|
42
|
+
* async add(text: string) { this.items.push({...}); } // Auto-emits!
|
|
43
|
+
* }
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
function transformReactiveCollections(source) {
|
|
47
|
+
// Check if this is a PhotonMCP class (extends PhotonMCP)
|
|
48
|
+
const isPhotonMCP = /class\s+\w+\s+extends\s+PhotonMCP\b/.test(source);
|
|
49
|
+
if (!isPhotonMCP)
|
|
50
|
+
return source;
|
|
51
|
+
// Check if there are array properties with = [] that need transformation
|
|
52
|
+
// Look for patterns like: `items: Type[] = []` or `items = []` (class properties)
|
|
53
|
+
const hasArrayLiterals = /\w+\s*:\s*[^=\n]+\[\]\s*=\s*\[\s*\]/.test(source) || // typed: Type[] = []
|
|
54
|
+
/^\s+\w+\s*=\s*\[\s*\](?=\s*[;\n])/m.test(source); // simple: prop = []
|
|
55
|
+
if (!hasArrayLiterals)
|
|
56
|
+
return source;
|
|
57
|
+
let transformed = source;
|
|
58
|
+
// Check if Array is already imported from photon-core
|
|
59
|
+
const hasArrayImport = /import\s*\{[^}]*\bArray\b[^}]*\}\s*from\s*['"]@portel\/photon-core['"]/.test(source);
|
|
60
|
+
if (!hasArrayImport) {
|
|
61
|
+
// Inject ReactiveArray import (using alias to avoid shadowing issues)
|
|
62
|
+
// Find the photon-core import and add ReactiveArray to it, or add new import
|
|
63
|
+
const photonCoreImport = source.match(/import\s*\{([^}]+)\}\s*from\s*['"]@portel\/photon-core['"]/);
|
|
64
|
+
if (photonCoreImport) {
|
|
65
|
+
// Add to existing import
|
|
66
|
+
const existingImports = photonCoreImport[1];
|
|
67
|
+
transformed = transformed.replace(photonCoreImport[0], `import { ${existingImports}, Array as ReactiveArray } from '@portel/photon-core'`);
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
// Add new import at the top (after any existing imports)
|
|
71
|
+
const lastImportMatch = source.match(/^import\s+.+$/gm);
|
|
72
|
+
if (lastImportMatch) {
|
|
73
|
+
const lastImport = lastImportMatch[lastImportMatch.length - 1];
|
|
74
|
+
transformed = transformed.replace(lastImport, `${lastImport}\nimport { Array as ReactiveArray } from '@portel/photon-core';`);
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
// No imports, add at top
|
|
78
|
+
transformed = `import { Array as ReactiveArray } from '@portel/photon-core';\n${transformed}`;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
// Determine the Array constructor name to use
|
|
83
|
+
const arrayConstructor = hasArrayImport ? 'Array' : 'ReactiveArray';
|
|
84
|
+
// Transform class property declarations with type annotation = []
|
|
85
|
+
// Handles: items: Task[] = []; | items: Array<T> = [];
|
|
86
|
+
transformed = transformed.replace(/(\w+)\s*:\s*(?:Array<[^>]+>|[^=\n]+\[\])\s*=\s*\[\s*\]/g, `$1 = new ${arrayConstructor}()`);
|
|
87
|
+
// Transform class property without type annotation (indented, at start of line)
|
|
88
|
+
// Skip local variables (const/let/var)
|
|
89
|
+
transformed = transformed.replace(/^(\s+)(\w+)\s*=\s*\[\s*\](?=\s*[;\n])/gm, `$1$2 = new ${arrayConstructor}()`);
|
|
90
|
+
return transformed;
|
|
91
|
+
}
|
|
21
92
|
export async function compilePhotonTS(tsFilePath, options) {
|
|
22
|
-
|
|
93
|
+
let source = options.content ?? (await fs.readFile(tsFilePath, 'utf-8'));
|
|
94
|
+
// Transform reactive collection literals before compilation
|
|
95
|
+
source = transformReactiveCollections(source);
|
|
23
96
|
const hash = crypto.createHash('sha256').update(source).digest('hex').slice(0, 16);
|
|
24
97
|
const fileName = path.basename(tsFilePath, '.ts');
|
|
25
98
|
const cachedJsPath = path.join(options.cacheDir, `${fileName}.${hash}.mjs`);
|
package/dist/compiler.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compiler.js","sourceRoot":"","sources":["../src/compiler.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,MAAM,aAAa,CAAC;AAClC,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAC7B,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AAEjC;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CACnC,UAAkB,EAClB,OAA+C;IAE/C,
|
|
1
|
+
{"version":3,"file":"compiler.js","sourceRoot":"","sources":["../src/compiler.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,MAAM,aAAa,CAAC;AAClC,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAC7B,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AAEjC;;;;;;;GAOG;AACH;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,SAAS,4BAA4B,CAAC,MAAc;IAClD,yDAAyD;IACzD,MAAM,WAAW,GAAG,qCAAqC,CAAC,IAA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AM,OAAO,CAAC,SAAS,CAAC,MAAM,EAAE;QAC7C,MAAM,EAAE,IAAI;QACZ,MAAM,EAAE,KAAK;QACb,MAAM,EAAE,QAAQ;QAChB,SAAS,EAAE,QAAQ;KACpB,CAAC,CAAC;IAEH,gCAAgC;IAChC,MAAM,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;IAEtD,4BAA4B;IAC5B,MAAM,EAAE,CAAC,SAAS,CAAC,YAAY,EAAE,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;IAEvD,OAAO,YAAY,CAAC;AACtB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@portel/photon-core",
|
|
3
|
-
"version": "2.5.
|
|
3
|
+
"version": "2.5.4",
|
|
4
4
|
"description": "Core library for parsing, loading, and managing .photon.ts files - runtime-agnostic foundation for building custom Photon runtimes",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
package/src/compiler.ts
CHANGED
|
@@ -20,11 +20,108 @@ import * as crypto from 'crypto';
|
|
|
20
20
|
* @param options.content - Optional pre-read file content (avoids extra read)
|
|
21
21
|
* @returns Absolute path to the compiled .mjs file
|
|
22
22
|
*/
|
|
23
|
+
/**
|
|
24
|
+
* Transform arrays to reactive collections for PhotonMCP classes
|
|
25
|
+
*
|
|
26
|
+
* ZERO-EFFORT REACTIVITY: If a class extends PhotonMCP and has array properties,
|
|
27
|
+
* this transform automatically:
|
|
28
|
+
* 1. Injects `import { Array as ReactiveArray } from '@portel/photon-core'`
|
|
29
|
+
* 2. Transforms `= []` to `= new ReactiveArray()` for class properties
|
|
30
|
+
*
|
|
31
|
+
* Result: Developers write normal code, arrays are automatically reactive.
|
|
32
|
+
*
|
|
33
|
+
* ```typescript
|
|
34
|
+
* // Developer writes this (normal TypeScript):
|
|
35
|
+
* export default class TodoList extends PhotonMCP {
|
|
36
|
+
* items: Task[] = [];
|
|
37
|
+
* async add(text: string) { this.items.push({...}); }
|
|
38
|
+
* }
|
|
39
|
+
*
|
|
40
|
+
* // Compiler transforms to:
|
|
41
|
+
* import { Array as ReactiveArray } from '@portel/photon-core';
|
|
42
|
+
* export default class TodoList extends PhotonMCP {
|
|
43
|
+
* items = new ReactiveArray();
|
|
44
|
+
* async add(text: string) { this.items.push({...}); } // Auto-emits!
|
|
45
|
+
* }
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
function transformReactiveCollections(source: string): string {
|
|
49
|
+
// Check if this is a PhotonMCP class (extends PhotonMCP)
|
|
50
|
+
const isPhotonMCP = /class\s+\w+\s+extends\s+PhotonMCP\b/.test(source);
|
|
51
|
+
if (!isPhotonMCP) return source;
|
|
52
|
+
|
|
53
|
+
// Check if there are array properties with = [] that need transformation
|
|
54
|
+
// Look for patterns like: `items: Type[] = []` or `items = []` (class properties)
|
|
55
|
+
const hasArrayLiterals =
|
|
56
|
+
/\w+\s*:\s*[^=\n]+\[\]\s*=\s*\[\s*\]/.test(source) || // typed: Type[] = []
|
|
57
|
+
/^\s+\w+\s*=\s*\[\s*\](?=\s*[;\n])/m.test(source); // simple: prop = []
|
|
58
|
+
|
|
59
|
+
if (!hasArrayLiterals) return source;
|
|
60
|
+
|
|
61
|
+
let transformed = source;
|
|
62
|
+
|
|
63
|
+
// Check if Array is already imported from photon-core
|
|
64
|
+
const hasArrayImport = /import\s*\{[^}]*\bArray\b[^}]*\}\s*from\s*['"]@portel\/photon-core['"]/.test(source);
|
|
65
|
+
|
|
66
|
+
if (!hasArrayImport) {
|
|
67
|
+
// Inject ReactiveArray import (using alias to avoid shadowing issues)
|
|
68
|
+
// Find the photon-core import and add ReactiveArray to it, or add new import
|
|
69
|
+
const photonCoreImport = source.match(
|
|
70
|
+
/import\s*\{([^}]+)\}\s*from\s*['"]@portel\/photon-core['"]/
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
if (photonCoreImport) {
|
|
74
|
+
// Add to existing import
|
|
75
|
+
const existingImports = photonCoreImport[1];
|
|
76
|
+
transformed = transformed.replace(
|
|
77
|
+
photonCoreImport[0],
|
|
78
|
+
`import { ${existingImports}, Array as ReactiveArray } from '@portel/photon-core'`
|
|
79
|
+
);
|
|
80
|
+
} else {
|
|
81
|
+
// Add new import at the top (after any existing imports)
|
|
82
|
+
const lastImportMatch = source.match(/^import\s+.+$/gm);
|
|
83
|
+
if (lastImportMatch) {
|
|
84
|
+
const lastImport = lastImportMatch[lastImportMatch.length - 1];
|
|
85
|
+
transformed = transformed.replace(
|
|
86
|
+
lastImport,
|
|
87
|
+
`${lastImport}\nimport { Array as ReactiveArray } from '@portel/photon-core';`
|
|
88
|
+
);
|
|
89
|
+
} else {
|
|
90
|
+
// No imports, add at top
|
|
91
|
+
transformed = `import { Array as ReactiveArray } from '@portel/photon-core';\n${transformed}`;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Determine the Array constructor name to use
|
|
97
|
+
const arrayConstructor = hasArrayImport ? 'Array' : 'ReactiveArray';
|
|
98
|
+
|
|
99
|
+
// Transform class property declarations with type annotation = []
|
|
100
|
+
// Handles: items: Task[] = []; | items: Array<T> = [];
|
|
101
|
+
transformed = transformed.replace(
|
|
102
|
+
/(\w+)\s*:\s*(?:Array<[^>]+>|[^=\n]+\[\])\s*=\s*\[\s*\]/g,
|
|
103
|
+
`$1 = new ${arrayConstructor}()`
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
// Transform class property without type annotation (indented, at start of line)
|
|
107
|
+
// Skip local variables (const/let/var)
|
|
108
|
+
transformed = transformed.replace(
|
|
109
|
+
/^(\s+)(\w+)\s*=\s*\[\s*\](?=\s*[;\n])/gm,
|
|
110
|
+
`$1$2 = new ${arrayConstructor}()`
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
return transformed;
|
|
114
|
+
}
|
|
115
|
+
|
|
23
116
|
export async function compilePhotonTS(
|
|
24
117
|
tsFilePath: string,
|
|
25
118
|
options: { cacheDir: string; content?: string },
|
|
26
119
|
): Promise<string> {
|
|
27
|
-
|
|
120
|
+
let source = options.content ?? (await fs.readFile(tsFilePath, 'utf-8'));
|
|
121
|
+
|
|
122
|
+
// Transform reactive collection literals before compilation
|
|
123
|
+
source = transformReactiveCollections(source);
|
|
124
|
+
|
|
28
125
|
const hash = crypto.createHash('sha256').update(source).digest('hex').slice(0, 16);
|
|
29
126
|
|
|
30
127
|
const fileName = path.basename(tsFilePath, '.ts');
|