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

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,181 @@
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
+ // Match JSX opening tags: <ComponentName or <div or <Component.Sub
69
+ // This regex matches: < followed by tag name, capturing what comes after
70
+ const modifiedLine = line.replace(/<([A-Z][\w.]*|[a-z]+)([\s\/>])/g, (match, tagName, after) => {
71
+ // Build attributes
72
+ const attrs = [];
73
+ attrs.push(`codepress-data-fp="${encoded}:${lineNum}-${lineNum}"`);
74
+ // Add repo/branch info to container elements (divs, sections, etc.)
75
+ if (/^[a-z]/.test(tagName)) {
76
+ if (repoName) {
77
+ attrs.push(`codepress-github-repo-name="${repoName}"`);
78
+ }
79
+ if (branchName) {
80
+ attrs.push(`codepress-github-branch="${branchName}"`);
81
+ }
82
+ }
83
+ return `<${tagName} ${attrs.join(' ')}${after}`;
84
+ });
85
+ output.push(modifiedLine);
86
+ }
87
+ return output.join('\n');
88
+ }
89
+ /**
90
+ * Wrap exported components with __CPProvider
91
+ */
92
+ function wrapWithProvider(source) {
93
+ var _a;
94
+ // Find default export component
95
+ const defaultExportMatch = source.match(/export\s+default\s+function\s+(\w+)/);
96
+ if (!defaultExportMatch) {
97
+ // Try: export default ComponentName;
98
+ const namedMatch = source.match(/export\s+default\s+(\w+);/);
99
+ if (!namedMatch)
100
+ return source;
101
+ }
102
+ 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]);
103
+ if (!componentName)
104
+ return source;
105
+ // Inject provider wrapper code at the top
106
+ const providerCode = `
107
+ import { useSyncExternalStore } from 'react';
108
+
109
+ // Module-level version counter for HMR
110
+ let __cpvVersion = 0;
111
+
112
+ // Provider component that wraps the default export
113
+ function __CPProvider({ value, children }: { value?: any; children: React.ReactNode }) {
114
+ const __cpv = useSyncExternalStore(
115
+ (cb) => {
116
+ const h = () => {
117
+ __cpvVersion = __cpvVersion + 1;
118
+ cb();
119
+ };
120
+ if (typeof window !== 'undefined') {
121
+ window.addEventListener("CP_PREVIEW_REFRESH", h);
122
+ return () => { window.removeEventListener("CP_PREVIEW_REFRESH", h); };
123
+ }
124
+ return () => {};
125
+ },
126
+ () => __cpvVersion,
127
+ () => 0
128
+ );
129
+
130
+ return <CPX.Provider value={value} key={__cpv}>{children}</CPX.Provider>;
131
+ }
132
+
133
+ // Context for passing data through provider
134
+ const CPX = { Provider: ({ value, children }: any) => children };
135
+ `;
136
+ // Wrap the default export
137
+ const wrappedSource = source.replace(new RegExp(`export\\s+default\\s+${componentName}`), `const __Original${componentName} = ${componentName};
138
+ export default function ${componentName}(props: any) {
139
+ return <__CPProvider><__Original${componentName} {...props} /></__CPProvider>;
140
+ }`);
141
+ return providerCode + '\n' + wrappedSource;
142
+ }
143
+ function createCodePressPlugin(options = {}) {
144
+ const { repo_name = '', branch_name = '', repo_root = process.cwd(), } = options;
145
+ return {
146
+ name: 'codepress-jsx-transform',
147
+ setup(build) {
148
+ // Transform TSX/JSX files
149
+ build.onLoad({ filter: /\.[tj]sx$/ }, async (args) => {
150
+ try {
151
+ // Skip node_modules
152
+ if (args.path.includes('node_modules')) {
153
+ return null;
154
+ }
155
+ const source = await fs.promises.readFile(args.path, 'utf8');
156
+ const relPath = path.relative(repo_root, args.path);
157
+ const encoded = xorEncodePath(relPath);
158
+ if (!encoded) {
159
+ return { contents: source, loader: 'tsx' };
160
+ }
161
+ // Step 1: Inject JSX attributes
162
+ let transformed = injectJSXAttributes(source, encoded, repo_name, branch_name);
163
+ // Step 2: Wrap with provider (for default exports)
164
+ if (transformed.includes('export default')) {
165
+ transformed = wrapWithProvider(transformed);
166
+ }
167
+ return {
168
+ contents: transformed,
169
+ loader: 'tsx',
170
+ };
171
+ }
172
+ catch (err) {
173
+ console.error('[CodePress Plugin] Error transforming', args.path, err);
174
+ return null;
175
+ }
176
+ });
177
+ },
178
+ };
179
+ }
180
+ exports.default = createCodePressPlugin;
181
+ //# 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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgIH,sDA6CC;AA3KD,uCAAyB;AACzB,2CAA6B;AAS7B,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,4BAA4B,CAAC,CAAC;AAEzD,SAAS,aAAa,CAAC,KAAa;IAClC,IAAI,CAAC,KAAK;QAAE,OAAO,EAAE,CAAC;IACtB,MAAM,UAAU,GAAG,KAAK,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;IAC7C,MAAM,GAAG,GAAG,MAAM,CAAC,IAAI,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;IAC5C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,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.20251115061558",
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