@codepress/codepress-engine 0.4.2-dev.hmr.20251113085323 → 0.4.2-dev.hmr.20251115064638

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,12 @@
1
+ /**
2
+ * CodePress esbuild plugin - Injects tracking attributes and provider wrappers into JSX
3
+ * Replaces the Rust SWC plugin with a pure JavaScript implementation
4
+ */
5
+ import type { Plugin } from 'esbuild';
6
+ interface CodePressPluginOptions {
7
+ repo_name?: string;
8
+ branch_name?: string;
9
+ repo_root?: string;
10
+ }
11
+ export declare function createCodePressPlugin(options?: CodePressPluginOptions): Plugin;
12
+ export default createCodePressPlugin;
@@ -0,0 +1,190 @@
1
+ "use strict";
2
+ /**
3
+ * CodePress esbuild plugin - Injects tracking attributes and provider wrappers into JSX
4
+ * Replaces the Rust SWC plugin with a pure JavaScript implementation
5
+ */
6
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
7
+ if (k2 === undefined) k2 = k;
8
+ var desc = Object.getOwnPropertyDescriptor(m, k);
9
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
10
+ desc = { enumerable: true, get: function() { return m[k]; } };
11
+ }
12
+ Object.defineProperty(o, k2, desc);
13
+ }) : (function(o, m, k, k2) {
14
+ if (k2 === undefined) k2 = k;
15
+ o[k2] = m[k];
16
+ }));
17
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
18
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
19
+ }) : function(o, v) {
20
+ o["default"] = v;
21
+ });
22
+ var __importStar = (this && this.__importStar) || (function () {
23
+ var ownKeys = function(o) {
24
+ ownKeys = Object.getOwnPropertyNames || function (o) {
25
+ var ar = [];
26
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
27
+ return ar;
28
+ };
29
+ return ownKeys(o);
30
+ };
31
+ return function (mod) {
32
+ if (mod && mod.__esModule) return mod;
33
+ var result = {};
34
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
35
+ __setModuleDefault(result, mod);
36
+ return result;
37
+ };
38
+ })();
39
+ Object.defineProperty(exports, "__esModule", { value: true });
40
+ exports.createCodePressPlugin = createCodePressPlugin;
41
+ const fs = __importStar(require("fs"));
42
+ const path = __importStar(require("path"));
43
+ const SECRET = Buffer.from('codepress-file-obfuscation');
44
+ function xorEncodePath(input) {
45
+ if (!input)
46
+ return '';
47
+ const normalized = input.replace(/\\/g, '/');
48
+ const buf = Buffer.from(normalized, 'utf8');
49
+ const out = Buffer.allocUnsafe(buf.length);
50
+ for (let i = 0; i < buf.length; i++) {
51
+ out[i] = buf[i] ^ SECRET[i % SECRET.length];
52
+ }
53
+ return out
54
+ .toString('base64')
55
+ .replace(/\+/g, '-')
56
+ .replace(/\//g, '_')
57
+ .replace(/=+$/g, '');
58
+ }
59
+ /**
60
+ * Inject codepress-data-fp attributes into JSX opening tags
61
+ */
62
+ function injectJSXAttributes(source, encoded, repoName, branchName) {
63
+ const lines = source.split('\n');
64
+ const output = [];
65
+ let lineNum = 0;
66
+ for (const line of lines) {
67
+ lineNum++;
68
+ // Skip lines that are likely TypeScript type definitions/generics
69
+ // These patterns indicate type syntax, not JSX
70
+ if (/^\s*(interface|type|class|extends|implements)\b/.test(line) ||
71
+ /\bextends\s+[\w.]+</.test(line) ||
72
+ /<[\w.]+>(?!\s*[{(</])/.test(line) // Generics not followed by JSX-like syntax
73
+ ) {
74
+ output.push(line);
75
+ continue;
76
+ }
77
+ // Match JSX opening tags with context awareness
78
+ // Only match when < appears in typical JSX contexts (after whitespace, braces, parens, return, etc.)
79
+ const modifiedLine = line.replace(/(^|\s+|[\s{(>]|return\s+|=\s*|:\s*|\?\s*)<([A-Z][\w.]*|[a-z]+)([\s\/>])/g, (match, before, tagName, after) => {
80
+ // Build attributes
81
+ const attrs = [];
82
+ attrs.push(`codepress-data-fp="${encoded}:${lineNum}-${lineNum}"`);
83
+ // Add repo/branch info to container elements (divs, sections, etc.)
84
+ if (/^[a-z]/.test(tagName)) {
85
+ if (repoName) {
86
+ attrs.push(`codepress-github-repo-name="${repoName}"`);
87
+ }
88
+ if (branchName) {
89
+ attrs.push(`codepress-github-branch="${branchName}"`);
90
+ }
91
+ }
92
+ return `${before}<${tagName} ${attrs.join(' ')}${after}`;
93
+ });
94
+ output.push(modifiedLine);
95
+ }
96
+ return output.join('\n');
97
+ }
98
+ /**
99
+ * Wrap exported components with __CPProvider
100
+ */
101
+ function wrapWithProvider(source) {
102
+ var _a;
103
+ // Find default export component
104
+ const defaultExportMatch = source.match(/export\s+default\s+function\s+(\w+)/);
105
+ if (!defaultExportMatch) {
106
+ // Try: export default ComponentName;
107
+ const namedMatch = source.match(/export\s+default\s+(\w+);/);
108
+ if (!namedMatch)
109
+ return source;
110
+ }
111
+ const componentName = (defaultExportMatch === null || defaultExportMatch === void 0 ? void 0 : defaultExportMatch[1]) || ((_a = source.match(/export\s+default\s+(\w+);/)) === null || _a === void 0 ? void 0 : _a[1]);
112
+ if (!componentName)
113
+ return source;
114
+ // Inject provider wrapper code at the top
115
+ const providerCode = `
116
+ import { useSyncExternalStore } from 'react';
117
+
118
+ // Module-level version counter for HMR
119
+ let __cpvVersion = 0;
120
+
121
+ // Provider component that wraps the default export
122
+ function __CPProvider({ value, children }: { value?: any; children: React.ReactNode }) {
123
+ const __cpv = useSyncExternalStore(
124
+ (cb) => {
125
+ const h = () => {
126
+ __cpvVersion = __cpvVersion + 1;
127
+ cb();
128
+ };
129
+ if (typeof window !== 'undefined') {
130
+ window.addEventListener("CP_PREVIEW_REFRESH", h);
131
+ return () => { window.removeEventListener("CP_PREVIEW_REFRESH", h); };
132
+ }
133
+ return () => {};
134
+ },
135
+ () => __cpvVersion,
136
+ () => 0
137
+ );
138
+
139
+ return <CPX.Provider value={value} key={__cpv}>{children}</CPX.Provider>;
140
+ }
141
+
142
+ // Context for passing data through provider
143
+ const CPX = { Provider: ({ value, children }: any) => children };
144
+ `;
145
+ // Wrap the default export
146
+ const wrappedSource = source.replace(new RegExp(`export\\s+default\\s+${componentName}`), `const __Original${componentName} = ${componentName};
147
+ export default function ${componentName}(props: any) {
148
+ return <__CPProvider><__Original${componentName} {...props} /></__CPProvider>;
149
+ }`);
150
+ return providerCode + '\n' + wrappedSource;
151
+ }
152
+ function createCodePressPlugin(options = {}) {
153
+ const { repo_name = '', branch_name = '', repo_root = process.cwd(), } = options;
154
+ return {
155
+ name: 'codepress-jsx-transform',
156
+ setup(build) {
157
+ // Transform TSX/JSX files
158
+ build.onLoad({ filter: /\.[tj]sx$/ }, async (args) => {
159
+ try {
160
+ // Skip node_modules
161
+ if (args.path.includes('node_modules')) {
162
+ return null;
163
+ }
164
+ const source = await fs.promises.readFile(args.path, 'utf8');
165
+ const relPath = path.relative(repo_root, args.path);
166
+ const encoded = xorEncodePath(relPath);
167
+ if (!encoded) {
168
+ return { contents: source, loader: 'tsx' };
169
+ }
170
+ // Step 1: Inject JSX attributes
171
+ let transformed = injectJSXAttributes(source, encoded, repo_name, branch_name);
172
+ // Step 2: Wrap with provider (for default exports)
173
+ if (transformed.includes('export default')) {
174
+ transformed = wrapWithProvider(transformed);
175
+ }
176
+ return {
177
+ contents: transformed,
178
+ loader: 'tsx',
179
+ };
180
+ }
181
+ catch (err) {
182
+ console.error('[CodePress Plugin] Error transforming', args.path, err);
183
+ return null;
184
+ }
185
+ });
186
+ },
187
+ };
188
+ }
189
+ exports.default = createCodePressPlugin;
190
+ //# sourceMappingURL=esbuild-plugin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"esbuild-plugin.js","sourceRoot":"","sources":["../src/esbuild-plugin.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2IH,sDA6CC;AAtLD,uCAAyB;AACzB,2CAA6B;AAS7B,MAAM,MAAM,GAAG,MAAM,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QAAQ,CAAC,SAAS,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC;oBACpD,MAAM,OAAO,GAAG,aAAa,CAAC,OAAO,CAAC,CAAC;oBAEvC,IAAI,CAAC,OAAO,EAAE,CAAC;wBACb,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC;oBAC7C,CAAC;oBAED,gCAAgC;oBAChC,IAAI,WAAW,GAAG,mBAAmB,CAAC,MAAM,EAAE,OAAO,EAAE,SAAS,EAAE,WAAW,CAAC,CAAC;oBAE/E,mDAAmD;oBACnD,IAAI,WAAW,CAAC,QAAQ,CAAC,gBAAgB,CAAC,EAAE,CAAC;wBAC3C,WAAW,GAAG,gBAAgB,CAAC,WAAW,CAAC,CAAC;oBAC9C,CAAC;oBAED,OAAO;wBACL,QAAQ,EAAE,WAAW;wBACrB,MAAM,EAAE,KAAK;qBACd,CAAC;gBACJ,CAAC;gBAAC,OAAO,GAAG,EAAE,CAAC;oBACb,OAAO,CAAC,KAAK,CAAC,uCAAuC,EAAE,IAAI,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;oBACvE,OAAO,IAAI,CAAC;gBACd,CAAC;YACH,CAAC,CAAC,CAAC;QACL,CAAC;KACF,CAAC;AACJ,CAAC;AAED,kBAAe,qBAAqB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codepress/codepress-engine",
3
- "version": "0.4.2-dev.hmr.20251113085323",
3
+ "version": "0.4.2-dev.hmr.20251115064638",
4
4
  "description": "CodePress engine - Babel and SWC plug-ins",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -30,6 +30,11 @@
30
30
  "require": "./dist/swc/index.js",
31
31
  "default": "./dist/swc/index.js"
32
32
  },
33
+ "./esbuild": {
34
+ "types": "./dist/esbuild-plugin.d.ts",
35
+ "require": "./dist/esbuild-plugin.js",
36
+ "default": "./dist/esbuild-plugin.js"
37
+ },
33
38
  "./swc/wasm": "./swc/codepress_engine.v42.wasm",
34
39
  "./swc/wasm-v42": "./swc/codepress_engine.v42.wasm",
35
40
  "./swc/wasm-v26": "./swc/codepress_engine.v26.wasm",
@@ -90,6 +95,7 @@
90
95
  "@types/jest": "^29.5.12",
91
96
  "@types/node": "^22.9.0",
92
97
  "@types/node-fetch": "^2.6.11",
98
+ "esbuild": "^0.24.0",
93
99
  "eslint": "^9.37.0",
94
100
  "eslint-config-prettier": "^10.1.8",
95
101
  "eslint-plugin-import": "^2.32.0",
Binary file