juxscript 1.1.243 → 1.1.245

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,281 @@
1
+ /**
2
+ * Include - Simplified resource injection for bundled and external resources
3
+ * Supports page-specific scoping and cleanup
4
+ */
5
+
6
+ type ResourceType = 'css' | 'js' | 'module';
7
+
8
+ interface IncludeOptions {
9
+ type?: ResourceType;
10
+ target?: string; // CSS selector for target container (default: 'head')
11
+ async?: boolean;
12
+ defer?: boolean;
13
+ crossOrigin?: 'anonymous' | 'use-credentials';
14
+ integrity?: string;
15
+ pageScoped?: boolean; // If true, tracks for cleanup
16
+ }
17
+
18
+ // Global registry for page-scoped resources
19
+ const scopedResources: Map<string, Set<HTMLElement>> = new Map();
20
+
21
+ export class Include {
22
+ private url: string;
23
+ private options: IncludeOptions;
24
+ private element: HTMLElement | null = null;
25
+ private pageId: string | null = null;
26
+
27
+ constructor(url: string, options: IncludeOptions = {}) {
28
+ this.url = url;
29
+ this.options = options;
30
+
31
+ // Auto-detect type from extension if not provided
32
+ if (!options.type) {
33
+ if (url.endsWith('.css')) {
34
+ this.options.type = 'css';
35
+ } else if (url.endsWith('.mjs') || url.endsWith('.module.js')) {
36
+ this.options.type = 'module';
37
+ } else {
38
+ this.options.type = 'js';
39
+ }
40
+ }
41
+ }
42
+
43
+ /* -------------------------
44
+ * Fluent API
45
+ * ------------------------- */
46
+
47
+ css(): this {
48
+ this.options.type = 'css';
49
+ return this;
50
+ }
51
+
52
+ js(): this {
53
+ this.options.type = 'js';
54
+ return this;
55
+ }
56
+
57
+ module(): this {
58
+ this.options.type = 'module';
59
+ return this;
60
+ }
61
+
62
+ async(): this {
63
+ this.options.async = true;
64
+ return this;
65
+ }
66
+
67
+ defer(): this {
68
+ this.options.defer = true;
69
+ return this;
70
+ }
71
+
72
+ /**
73
+ * Inject into specific container instead of <head>
74
+ * Useful for page-specific styles
75
+ *
76
+ * @example
77
+ * jux.include('/css/page1.css').into('#page1-container');
78
+ */
79
+ into(selector: string): this {
80
+ this.options.target = selector;
81
+ return this;
82
+ }
83
+
84
+ /**
85
+ * Mark as page-scoped for automatic cleanup
86
+ *
87
+ * @example
88
+ * jux.include('/css/dashboard.css').forPage('dashboard');
89
+ * // Later: Include.cleanupPage('dashboard');
90
+ */
91
+ forPage(pageId: string): this {
92
+ this.pageId = pageId;
93
+ this.options.pageScoped = true;
94
+ return this;
95
+ }
96
+
97
+ /* -------------------------
98
+ * Render
99
+ * ------------------------- */
100
+
101
+ render(): this {
102
+ if (typeof document === 'undefined') return this;
103
+
104
+ try {
105
+ // Check if already loaded
106
+ if (this.isAlreadyLoaded()) {
107
+ console.log(`⚠️ Resource already loaded: ${this.url}`);
108
+ return this;
109
+ }
110
+
111
+ // Create element based on type
112
+ let element: HTMLElement;
113
+
114
+ switch (this.options.type) {
115
+ case 'css':
116
+ element = this.createStylesheet();
117
+ break;
118
+ case 'js':
119
+ case 'module':
120
+ element = this.createScript();
121
+ break;
122
+ default:
123
+ throw new Error(`Unknown resource type: ${this.options.type}`);
124
+ }
125
+
126
+ // Get target container
127
+ const container = this.getContainer();
128
+ container.appendChild(element);
129
+
130
+ this.element = element;
131
+
132
+ // Register for page cleanup if needed
133
+ if (this.options.pageScoped && this.pageId) {
134
+ if (!scopedResources.has(this.pageId)) {
135
+ scopedResources.set(this.pageId, new Set());
136
+ }
137
+ scopedResources.get(this.pageId)!.add(element);
138
+ }
139
+
140
+ console.log(`✓ Loaded ${this.options.type}: ${this.url}`);
141
+ } catch (error: any) {
142
+ console.error(`✗ Failed to load ${this.options.type}: ${this.url}`, error);
143
+ throw error;
144
+ }
145
+
146
+ return this;
147
+ }
148
+
149
+ /* -------------------------
150
+ * Element Creation
151
+ * ------------------------- */
152
+
153
+ private createStylesheet(): HTMLLinkElement {
154
+ const link = document.createElement('link');
155
+ link.rel = 'stylesheet';
156
+ link.href = this.url;
157
+ link.dataset.juxInclude = this.url;
158
+
159
+ if (this.options.crossOrigin) link.crossOrigin = this.options.crossOrigin;
160
+ if (this.options.integrity) link.integrity = this.options.integrity;
161
+
162
+ link.onload = () => console.log(`✓ Stylesheet loaded: ${this.url}`);
163
+ link.onerror = () => {
164
+ throw new Error(`Failed to load stylesheet: ${this.url}`);
165
+ };
166
+
167
+ return link;
168
+ }
169
+
170
+ private createScript(): HTMLScriptElement {
171
+ const script = document.createElement('script');
172
+ script.src = this.url;
173
+ script.dataset.juxInclude = this.url;
174
+
175
+ if (this.options.type === 'module') script.type = 'module';
176
+ if (this.options.async) script.async = true;
177
+ if (this.options.defer) script.defer = true;
178
+ if (this.options.crossOrigin) script.crossOrigin = this.options.crossOrigin;
179
+ if (this.options.integrity) script.integrity = this.options.integrity;
180
+
181
+ script.onload = () => console.log(`✓ Script loaded: ${this.url}`);
182
+ script.onerror = () => {
183
+ throw new Error(`Failed to load script: ${this.url}`);
184
+ };
185
+
186
+ return script;
187
+ }
188
+
189
+ /* -------------------------
190
+ * Helpers
191
+ * ------------------------- */
192
+
193
+ private getContainer(): HTMLElement {
194
+ if (this.options.target) {
195
+ const container = document.querySelector(this.options.target);
196
+ if (!container) {
197
+ throw new Error(`Target container not found: ${this.options.target}`);
198
+ }
199
+ return container as HTMLElement;
200
+ }
201
+ return document.head;
202
+ }
203
+
204
+ private isAlreadyLoaded(): boolean {
205
+ const selector = `[data-jux-include="${this.url}"]`;
206
+ return document.querySelector(selector) !== null;
207
+ }
208
+
209
+ remove(): this {
210
+ if (this.element?.parentNode) {
211
+ this.element.parentNode.removeChild(this.element);
212
+ this.element = null;
213
+ }
214
+ return this;
215
+ }
216
+
217
+ /* -------------------------
218
+ * Static Page Cleanup
219
+ * ------------------------- */
220
+
221
+ /**
222
+ * Remove all resources for a specific page
223
+ *
224
+ * @example
225
+ * Include.cleanupPage('dashboard');
226
+ */
227
+ static cleanupPage(pageId: string): void {
228
+ const resources = scopedResources.get(pageId);
229
+ if (!resources) return;
230
+
231
+ resources.forEach(element => {
232
+ element.parentNode?.removeChild(element);
233
+ });
234
+
235
+ scopedResources.delete(pageId);
236
+ console.log(`✓ Cleaned up page resources: ${pageId}`);
237
+ }
238
+
239
+ /**
240
+ * Remove all page-scoped resources
241
+ */
242
+ static cleanupAll(): void {
243
+ scopedResources.forEach((resources, pageId) => {
244
+ resources.forEach(element => {
245
+ element.parentNode?.removeChild(element);
246
+ });
247
+ });
248
+ scopedResources.clear();
249
+ console.log('✓ Cleaned up all page-scoped resources');
250
+ }
251
+ }
252
+
253
+ /**
254
+ * Factory function - simplified usage
255
+ *
256
+ * Usage:
257
+ * // Basic (auto-detects from extension)
258
+ * jux.include('/css/styles.css');
259
+ * jux.include('/js/app.js');
260
+ *
261
+ * // Page-specific with cleanup
262
+ * jux.include('/css/dashboard.css').forPage('dashboard');
263
+ * jux.include('/js/dashboard.js').forPage('dashboard');
264
+ *
265
+ * // Later cleanup:
266
+ * Include.cleanupPage('dashboard');
267
+ *
268
+ * // Inject into specific container
269
+ * jux.include('/css/widget.css').into('#widget-container');
270
+ *
271
+ * // External CDN
272
+ * jux.include('https://cdn.tailwindcss.com').js();
273
+ *
274
+ * // Module
275
+ * jux.include('/js/app.mjs').module();
276
+ */
277
+ export function include(url: string, options?: IncludeOptions): Include {
278
+ const inc = new Include(url, options);
279
+ inc.render();
280
+ return inc;
281
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "juxscript",
3
- "version": "1.1.243",
3
+ "version": "1.1.245",
4
4
  "type": "module",
5
5
  "description": "A JavaScript UX authorship platform",
6
6
  "main": "./dist/index.js",