slicejs-web-framework 3.1.4 → 3.2.1

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.
@@ -17,6 +17,7 @@ export default class Controller {
17
17
  this.bundleConfig = null;
18
18
  this.criticalBundleLoaded = false;
19
19
  this.bundleImportPromises = new Map();
20
+ this.bundleLoadPromises = new Map();
20
21
 
21
22
  this.idCounter = 0;
22
23
  }
@@ -64,6 +65,20 @@ export default class Controller {
64
65
  return importPromise;
65
66
  }
66
67
 
68
+ buildBundleImportPath(bundleInfo) {
69
+ if (!bundleInfo || typeof bundleInfo.file !== 'string' || bundleInfo.file.length === 0) {
70
+ throw new Error('Bundle file is required');
71
+ }
72
+
73
+ const basePath = `/bundles/${bundleInfo.file}`;
74
+ const bundleHash = typeof bundleInfo.hash === 'string' ? bundleInfo.hash.trim() : '';
75
+ if (!bundleHash) {
76
+ return basePath;
77
+ }
78
+
79
+ return `${basePath}?v=${encodeURIComponent(bundleHash)}`;
80
+ }
81
+
67
82
  /**
68
83
  * Validate Bundling V2 module contract.
69
84
  * Requires named exports: SLICE_BUNDLE_META and registerAll.
@@ -88,51 +103,204 @@ export default class Controller {
88
103
  * 📦 Loads a bundle by name or category
89
104
  */
90
105
  async loadBundle(bundleName) {
91
- if (this.loadedBundles.has(bundleName)) {
106
+ const resolvedBundleName = this.resolveBundleName(bundleName);
107
+ if (this.loadedBundles.has(resolvedBundleName)) {
92
108
  return; // Already loaded
109
+ }
110
+
111
+ return this.loadBundleWithDependencies(resolvedBundleName, new Set());
93
112
  }
94
113
 
95
- let bundleInfo = null;
114
+ async loadBundleWithDependencies(bundleName, loadingStack = new Set()) {
115
+ const resolvedBundleName = this.resolveBundleName(bundleName);
96
116
 
97
- if (bundleName === 'critical') {
98
- bundleInfo = this.bundleConfig?.bundles?.critical;
99
- } else {
100
- bundleInfo = this.bundleConfig?.bundles?.routes?.[bundleName];
117
+ if (this.loadedBundles.has(resolvedBundleName)) {
118
+ return;
101
119
  }
102
120
 
103
- if (!bundleInfo && this.bundleConfig?.bundles?.routes && bundleName !== 'critical') {
104
- const normalizedName = bundleName?.toLowerCase();
105
- const matchedKey = Object.keys(this.bundleConfig.bundles.routes).find(
106
- (key) => key.toLowerCase() === normalizedName
107
- );
108
- if (matchedKey) {
109
- bundleInfo = this.bundleConfig.bundles.routes[matchedKey];
121
+ if (loadingStack.has(resolvedBundleName)) {
122
+ throw new Error(`Circular bundle dependency detected: ${Array.from(loadingStack).join(' -> ')} -> ${resolvedBundleName}`);
123
+ }
124
+
125
+ if (this.bundleLoadPromises.has(resolvedBundleName)) {
126
+ return this.bundleLoadPromises.get(resolvedBundleName);
127
+ }
128
+
129
+ const loadPromise = (async () => {
130
+ loadingStack.add(resolvedBundleName);
131
+ try {
132
+ const bundleInfo = this.getBundleInfo(resolvedBundleName);
133
+ if (!bundleInfo) {
134
+ console.warn(`Bundle ${resolvedBundleName} not found in configuration`);
135
+ return;
136
+ }
137
+
138
+ const dependencies = this.getBundleDependencies(bundleInfo);
139
+ for (const dependencyName of dependencies) {
140
+ await this.loadBundleWithDependencies(dependencyName, loadingStack);
141
+ }
142
+
143
+ const bundlePath = this.buildBundleImportPath(bundleInfo);
144
+ const bundleModule = await this.importBundleOnce(bundlePath);
145
+ const { metadata, registerAll } = await this.validateBundleModule(bundleModule, resolvedBundleName);
146
+
147
+ const registerResult = await registerAll(this, slice.stylesManager);
148
+ this.registerVendorSharedDependencies(bundleModule, metadata, resolvedBundleName, registerResult);
149
+
150
+ this.loadedBundles.add(resolvedBundleName);
151
+ const loadedBundleKey = metadata.bundleKey;
152
+ if (loadedBundleKey && loadedBundleKey !== resolvedBundleName) {
153
+ this.loadedBundles.add(loadedBundleKey);
154
+ }
155
+
156
+ if (metadata.type === 'critical' || resolvedBundleName === 'critical') {
157
+ this.criticalBundleLoaded = true;
158
+ }
159
+ } finally {
160
+ loadingStack.delete(resolvedBundleName);
110
161
  }
162
+ })();
163
+
164
+ this.bundleLoadPromises.set(resolvedBundleName, loadPromise);
165
+ try {
166
+ return await loadPromise;
167
+ } finally {
168
+ this.bundleLoadPromises.delete(resolvedBundleName);
169
+ }
170
+ }
171
+
172
+ resolveBundleName(bundleName) {
173
+ if (typeof bundleName !== 'string' || bundleName.length === 0) {
174
+ return bundleName;
111
175
  }
112
176
 
113
- if (!bundleInfo) {
114
- console.warn(`Bundle ${bundleName} not found in configuration`);
115
- return;
116
- }
177
+ if (bundleName.toLowerCase() === 'critical') {
178
+ return 'critical';
179
+ }
117
180
 
118
- const bundlePath = `/bundles/${bundleInfo.file}`;
181
+ if (this.isVendorSharedAlias(bundleName) && this.getVendorSharedBundleInfo()) {
182
+ return 'vendor-shared';
183
+ }
119
184
 
120
- const bundleModule = await this.importBundleOnce(bundlePath);
121
- const { metadata, registerAll } = await this.validateBundleModule(bundleModule, bundleName);
185
+ const routeBundleName = this.findBundleNameByAlias(this.bundleConfig?.bundles?.routes, bundleName);
186
+ if (routeBundleName) {
187
+ return routeBundleName;
188
+ }
122
189
 
123
- await registerAll(this, slice.stylesManager);
190
+ const sharedBundleName = this.findBundleNameByAlias(this.bundleConfig?.bundles?.shared, bundleName);
191
+ if (sharedBundleName) {
192
+ return sharedBundleName;
193
+ }
124
194
 
125
- this.loadedBundles.add(bundleName);
126
- const loadedBundleKey = metadata.bundleKey;
127
- if (loadedBundleKey && loadedBundleKey !== bundleName) {
128
- this.loadedBundles.add(loadedBundleKey);
129
- }
195
+ return bundleName;
196
+ }
130
197
 
131
- if (metadata.type === 'critical' || bundleName === 'critical') {
132
- this.criticalBundleLoaded = true;
133
- }
198
+ findBundleNameByAlias(bundleRegistry, bundleName) {
199
+ if (!bundleRegistry || typeof bundleRegistry !== 'object') {
200
+ return null;
201
+ }
202
+
203
+ if (bundleRegistry[bundleName]) {
204
+ return bundleName;
205
+ }
206
+
207
+ const normalizedName = bundleName?.toLowerCase();
208
+ if (!normalizedName) {
209
+ return null;
134
210
  }
135
211
 
212
+ return Object.keys(bundleRegistry).find((key) => key.toLowerCase() === normalizedName) || null;
213
+ }
214
+
215
+ getBundleDependencies(bundleInfo) {
216
+ if (!bundleInfo || !Array.isArray(bundleInfo.dependencies)) {
217
+ return [];
218
+ }
219
+
220
+ return bundleInfo.dependencies.filter((dependency) => typeof dependency === 'string' && dependency.length > 0);
221
+ }
222
+
223
+ findBundleEntryByName(bundleRegistry, bundleName) {
224
+ if (!bundleRegistry || typeof bundleRegistry !== 'object') {
225
+ return null;
226
+ }
227
+
228
+ if (bundleRegistry[bundleName]) {
229
+ return bundleRegistry[bundleName];
230
+ }
231
+
232
+ const normalizedName = bundleName?.toLowerCase();
233
+ if (!normalizedName) {
234
+ return null;
235
+ }
236
+
237
+ const matchedKey = Object.keys(bundleRegistry).find((key) => key.toLowerCase() === normalizedName);
238
+ return matchedKey ? bundleRegistry[matchedKey] : null;
239
+ }
240
+
241
+ getBundleInfo(bundleName) {
242
+ if (bundleName === 'critical') {
243
+ return this.bundleConfig?.bundles?.critical || null;
244
+ }
245
+
246
+ if (this.isVendorSharedAlias(bundleName)) {
247
+ return this.getVendorSharedBundleInfo();
248
+ }
249
+
250
+ return (
251
+ this.findBundleEntryByName(this.bundleConfig?.bundles?.routes, bundleName)
252
+ || this.findBundleEntryByName(this.bundleConfig?.bundles?.shared, bundleName)
253
+ );
254
+ }
255
+
256
+ getVendorSharedBundleInfo() {
257
+ if (this.bundleConfig?.bundles?.vendorShared && typeof this.bundleConfig.bundles.vendorShared === 'object') {
258
+ return this.bundleConfig.bundles.vendorShared;
259
+ }
260
+
261
+ return this.findBundleEntryByName(this.bundleConfig?.bundles?.shared, 'vendor-shared');
262
+ }
263
+
264
+ isVendorSharedAlias(bundleName) {
265
+ if (typeof bundleName !== 'string') {
266
+ return false;
267
+ }
268
+
269
+ const normalized = bundleName.toLowerCase();
270
+ return normalized === 'vendor-shared' || normalized === 'vendorshared';
271
+ }
272
+
273
+ registerVendorSharedDependencies(bundleModule, metadata, bundleName, registerResult) {
274
+ const isVendorShared = this.isVendorSharedBundleName(metadata?.bundleKey)
275
+ || this.isVendorSharedBundleName(bundleName)
276
+ || metadata?.registerVendorSharedDependencies === true;
277
+
278
+ if (!isVendorShared) {
279
+ return;
280
+ }
281
+
282
+ let sharedDeps = bundleModule?.SLICE_SHARED_DEPS;
283
+ if (!sharedDeps && registerResult && typeof registerResult === 'object') {
284
+ sharedDeps = registerResult.SLICE_SHARED_DEPS
285
+ || registerResult.SLICE_BUNDLE_DEPENDENCIES
286
+ || registerResult;
287
+ }
288
+
289
+ if (!sharedDeps || typeof sharedDeps !== 'object') {
290
+ return;
291
+ }
292
+
293
+ if (!window.__SLICE_SHARED_DEPS__ || typeof window.__SLICE_SHARED_DEPS__ !== 'object') {
294
+ window.__SLICE_SHARED_DEPS__ = {};
295
+ }
296
+
297
+ Object.assign(window.__SLICE_SHARED_DEPS__, sharedDeps);
298
+ }
299
+
300
+ isVendorSharedBundleName(bundleName) {
301
+ return typeof bundleName === 'string' && bundleName.toLowerCase() === 'vendor-shared';
302
+ }
303
+
136
304
  /**
137
305
  * 📦 Registers a bundle's components (called automatically by bundle files)
138
306
  */
package/Slice/Slice.js CHANGED
@@ -364,6 +364,9 @@ async function init() {
364
364
 
365
365
  if (resolvedMode === 'production' && window.slice.controller.bundleConfig) {
366
366
  const config = window.slice.controller.bundleConfig;
367
+ if (!window.__SLICE_SHARED_DEPS__ || typeof window.__SLICE_SHARED_DEPS__ !== 'object') {
368
+ window.__SLICE_SHARED_DEPS__ = {};
369
+ }
367
370
  const criticalFile = config?.bundles?.critical?.file;
368
371
  if (criticalFile) {
369
372
  try {
@@ -112,6 +112,466 @@ test('loadBundle marks requested bundle key and metadata bundleKey as loaded', a
112
112
  }
113
113
  });
114
114
 
115
+ test('loadBundle resolves dependencies first and registers vendor-shared exports once', async () => {
116
+ const tempDir = await mkdtemp(path.join(tmpdir(), 'slice-controller-loader-'));
117
+ const loaderPath = path.join(tempDir, 'components-alias-loader.mjs');
118
+ await writeFile(
119
+ loaderPath,
120
+ `export async function resolve(specifier, context, nextResolve) {
121
+ if (specifier === '/Components/components.js') {
122
+ return {
123
+ shortCircuit: true,
124
+ url: 'data:text/javascript,export default {};',
125
+ };
126
+ }
127
+ return nextResolve(specifier, context);
128
+ }
129
+ `,
130
+ 'utf8'
131
+ );
132
+ register(pathToFileURL(loaderPath).href);
133
+
134
+ const controllerModuleUrl = new URL('../Components/Structural/Controller/Controller.js', import.meta.url).href;
135
+ const { default: Controller } = await import(controllerModuleUrl);
136
+ const controller = new Controller();
137
+ const originalSlice = globalThis.slice;
138
+ const originalWindow = globalThis.window;
139
+
140
+ controller.bundleConfig = {
141
+ bundles: {
142
+ shared: {
143
+ 'vendor-shared': {
144
+ file: 'slice-bundle.vendor-shared.js',
145
+ },
146
+ },
147
+ routes: {
148
+ dashboard: {
149
+ file: 'slice-bundle.dashboard.js',
150
+ dependencies: ['vendor-shared'],
151
+ },
152
+ },
153
+ },
154
+ };
155
+
156
+ const importOrder = [];
157
+ const registerOrder = [];
158
+ const importsByPath = {
159
+ '/bundles/slice-bundle.vendor-shared.js': {
160
+ SLICE_BUNDLE_META: {
161
+ bundleKey: 'vendor-shared',
162
+ type: 'shared',
163
+ },
164
+ SLICE_SHARED_DEPS: {
165
+ 'vendors/dompurify': {
166
+ sanitize: () => 'ok',
167
+ },
168
+ },
169
+ registerAll: async () => {
170
+ registerOrder.push('vendor-shared');
171
+ },
172
+ },
173
+ '/bundles/slice-bundle.dashboard.js': {
174
+ SLICE_BUNDLE_META: {
175
+ bundleKey: 'dashboard',
176
+ type: 'route',
177
+ },
178
+ registerAll: async () => {
179
+ registerOrder.push('dashboard');
180
+ },
181
+ },
182
+ };
183
+
184
+ controller.importBundleOnce = async (bundlePath) => {
185
+ importOrder.push(bundlePath);
186
+ return importsByPath[bundlePath];
187
+ };
188
+
189
+ try {
190
+ globalThis.slice = {
191
+ stylesManager: {},
192
+ };
193
+ globalThis.window = {
194
+ __SLICE_SHARED_DEPS__: {},
195
+ };
196
+
197
+ await controller.loadBundle('dashboard');
198
+ await controller.loadBundle('dashboard');
199
+
200
+ assert.deepEqual(importOrder, ['/bundles/slice-bundle.vendor-shared.js', '/bundles/slice-bundle.dashboard.js']);
201
+ assert.deepEqual(registerOrder, ['vendor-shared', 'dashboard']);
202
+ assert.equal(typeof globalThis.window.__SLICE_SHARED_DEPS__['vendors/dompurify']?.sanitize, 'function');
203
+ } finally {
204
+ globalThis.slice = originalSlice;
205
+ globalThis.window = originalWindow;
206
+ await rm(tempDir, { recursive: true, force: true });
207
+ }
208
+ });
209
+
210
+ test('loadBundle resolves vendor-shared dependency from bundles.vendorShared config shape', async () => {
211
+ const tempDir = await mkdtemp(path.join(tmpdir(), 'slice-controller-loader-'));
212
+ const loaderPath = path.join(tempDir, 'components-alias-loader.mjs');
213
+ await writeFile(
214
+ loaderPath,
215
+ `export async function resolve(specifier, context, nextResolve) {
216
+ if (specifier === '/Components/components.js') {
217
+ return {
218
+ shortCircuit: true,
219
+ url: 'data:text/javascript,export default {};',
220
+ };
221
+ }
222
+ return nextResolve(specifier, context);
223
+ }
224
+ `,
225
+ 'utf8'
226
+ );
227
+ register(pathToFileURL(loaderPath).href);
228
+
229
+ const controllerModuleUrl = new URL('../Components/Structural/Controller/Controller.js', import.meta.url).href;
230
+ const { default: Controller } = await import(controllerModuleUrl);
231
+ const controller = new Controller();
232
+ const originalSlice = globalThis.slice;
233
+ const originalWindow = globalThis.window;
234
+
235
+ controller.bundleConfig = {
236
+ bundles: {
237
+ vendorShared: {
238
+ file: 'slice-bundle.vendor-shared.js',
239
+ },
240
+ routes: {
241
+ dashboard: {
242
+ file: 'slice-bundle.dashboard.js',
243
+ dependencies: ['vendor-shared'],
244
+ },
245
+ },
246
+ },
247
+ };
248
+
249
+ const importOrder = [];
250
+ const registerOrder = [];
251
+ const importsByPath = {
252
+ '/bundles/slice-bundle.vendor-shared.js': {
253
+ SLICE_BUNDLE_META: {
254
+ bundleKey: 'vendor-shared',
255
+ type: 'shared',
256
+ },
257
+ SLICE_SHARED_DEPS: {
258
+ 'vendors/dompurify': {
259
+ sanitize: () => 'ok',
260
+ },
261
+ },
262
+ registerAll: async () => {
263
+ registerOrder.push('vendor-shared');
264
+ },
265
+ },
266
+ '/bundles/slice-bundle.dashboard.js': {
267
+ SLICE_BUNDLE_META: {
268
+ bundleKey: 'dashboard',
269
+ type: 'route',
270
+ },
271
+ registerAll: async () => {
272
+ registerOrder.push('dashboard');
273
+ },
274
+ },
275
+ };
276
+
277
+ controller.importBundleOnce = async (bundlePath) => {
278
+ importOrder.push(bundlePath);
279
+ return importsByPath[bundlePath];
280
+ };
281
+
282
+ try {
283
+ globalThis.slice = {
284
+ stylesManager: {},
285
+ };
286
+ globalThis.window = {
287
+ __SLICE_SHARED_DEPS__: {},
288
+ };
289
+
290
+ await controller.loadBundle('dashboard');
291
+
292
+ assert.deepEqual(importOrder, ['/bundles/slice-bundle.vendor-shared.js', '/bundles/slice-bundle.dashboard.js']);
293
+ assert.deepEqual(registerOrder, ['vendor-shared', 'dashboard']);
294
+ assert.equal(typeof globalThis.window.__SLICE_SHARED_DEPS__['vendors/dompurify']?.sanitize, 'function');
295
+ } finally {
296
+ globalThis.slice = originalSlice;
297
+ globalThis.window = originalWindow;
298
+ await rm(tempDir, { recursive: true, force: true });
299
+ }
300
+ });
301
+
302
+ test('loadBundle registers vendor-shared dependencies from registerAll return when SLICE_SHARED_DEPS export is absent', async () => {
303
+ const tempDir = await mkdtemp(path.join(tmpdir(), 'slice-controller-loader-'));
304
+ const loaderPath = path.join(tempDir, 'components-alias-loader.mjs');
305
+ await writeFile(
306
+ loaderPath,
307
+ `export async function resolve(specifier, context, nextResolve) {
308
+ if (specifier === '/Components/components.js') {
309
+ return {
310
+ shortCircuit: true,
311
+ url: 'data:text/javascript,export default {};',
312
+ };
313
+ }
314
+ return nextResolve(specifier, context);
315
+ }
316
+ `,
317
+ 'utf8'
318
+ );
319
+ register(pathToFileURL(loaderPath).href);
320
+
321
+ const controllerModuleUrl = new URL('../Components/Structural/Controller/Controller.js', import.meta.url).href;
322
+ const { default: Controller } = await import(controllerModuleUrl);
323
+ const controller = new Controller();
324
+ const originalSlice = globalThis.slice;
325
+ const originalWindow = globalThis.window;
326
+
327
+ controller.bundleConfig = {
328
+ bundles: {
329
+ vendorShared: {
330
+ file: 'slice-bundle.vendor-shared.js',
331
+ },
332
+ routes: {
333
+ dashboard: {
334
+ file: 'slice-bundle.dashboard.js',
335
+ dependencies: ['vendor-shared'],
336
+ },
337
+ },
338
+ },
339
+ };
340
+
341
+ const importsByPath = {
342
+ '/bundles/slice-bundle.vendor-shared.js': {
343
+ SLICE_BUNDLE_META: {
344
+ bundleKey: 'vendor-shared',
345
+ type: 'shared',
346
+ },
347
+ registerAll: async () => ({
348
+ 'vendors/dompurify': {
349
+ sanitize: () => 'ok',
350
+ },
351
+ }),
352
+ },
353
+ '/bundles/slice-bundle.dashboard.js': {
354
+ SLICE_BUNDLE_META: {
355
+ bundleKey: 'dashboard',
356
+ type: 'route',
357
+ },
358
+ registerAll: async () => {},
359
+ },
360
+ };
361
+
362
+ controller.importBundleOnce = async (bundlePath) => importsByPath[bundlePath];
363
+
364
+ try {
365
+ globalThis.slice = {
366
+ stylesManager: {},
367
+ };
368
+ globalThis.window = {
369
+ __SLICE_SHARED_DEPS__: {},
370
+ };
371
+
372
+ await controller.loadBundle('dashboard');
373
+
374
+ assert.equal(typeof globalThis.window.__SLICE_SHARED_DEPS__['vendors/dompurify']?.sanitize, 'function');
375
+ } finally {
376
+ globalThis.slice = originalSlice;
377
+ globalThis.window = originalWindow;
378
+ await rm(tempDir, { recursive: true, force: true });
379
+ }
380
+ });
381
+
382
+ test('loadBundle appends bundle hash as query param when importing bundle files', async () => {
383
+ const tempDir = await mkdtemp(path.join(tmpdir(), 'slice-controller-loader-'));
384
+ const loaderPath = path.join(tempDir, 'components-alias-loader.mjs');
385
+ await writeFile(
386
+ loaderPath,
387
+ `export async function resolve(specifier, context, nextResolve) {
388
+ if (specifier === '/Components/components.js') {
389
+ return {
390
+ shortCircuit: true,
391
+ url: 'data:text/javascript,export default {};',
392
+ };
393
+ }
394
+ return nextResolve(specifier, context);
395
+ }
396
+ `,
397
+ 'utf8'
398
+ );
399
+ register(pathToFileURL(loaderPath).href);
400
+
401
+ const controllerModuleUrl = new URL('../Components/Structural/Controller/Controller.js', import.meta.url).href;
402
+ const { default: Controller } = await import(controllerModuleUrl);
403
+ const controller = new Controller();
404
+ const originalSlice = globalThis.slice;
405
+
406
+ controller.bundleConfig = {
407
+ bundles: {
408
+ routes: {
409
+ dashboard: {
410
+ file: 'slice-bundle.dashboard.js',
411
+ hash: 'abc123hash',
412
+ },
413
+ },
414
+ },
415
+ };
416
+
417
+ let importedPath = null;
418
+ controller.importBundleOnce = async (bundlePath) => {
419
+ importedPath = bundlePath;
420
+ return {
421
+ SLICE_BUNDLE_META: {
422
+ bundleKey: 'dashboard',
423
+ type: 'route',
424
+ },
425
+ registerAll: async () => {},
426
+ };
427
+ };
428
+
429
+ try {
430
+ globalThis.slice = {
431
+ stylesManager: {},
432
+ };
433
+
434
+ await controller.loadBundle('dashboard');
435
+ assert.equal(importedPath, '/bundles/slice-bundle.dashboard.js?v=abc123hash');
436
+ } finally {
437
+ globalThis.slice = originalSlice;
438
+ await rm(tempDir, { recursive: true, force: true });
439
+ }
440
+ });
441
+
442
+ test('loadBundle dedupes concurrent and repeated alias/case requests using canonical bundle key', async () => {
443
+ const tempDir = await mkdtemp(path.join(tmpdir(), 'slice-controller-loader-'));
444
+ const loaderPath = path.join(tempDir, 'components-alias-loader.mjs');
445
+ await writeFile(
446
+ loaderPath,
447
+ `export async function resolve(specifier, context, nextResolve) {
448
+ if (specifier === '/Components/components.js') {
449
+ return {
450
+ shortCircuit: true,
451
+ url: 'data:text/javascript,export default {};',
452
+ };
453
+ }
454
+ return nextResolve(specifier, context);
455
+ }
456
+ `,
457
+ 'utf8'
458
+ );
459
+ register(pathToFileURL(loaderPath).href);
460
+
461
+ const controllerModuleUrl = new URL('../Components/Structural/Controller/Controller.js', import.meta.url).href;
462
+ const { default: Controller } = await import(controllerModuleUrl);
463
+ const controller = new Controller();
464
+ const originalSlice = globalThis.slice;
465
+
466
+ controller.bundleConfig = {
467
+ bundles: {
468
+ routes: {
469
+ dashboard: {
470
+ file: 'slice-bundle.dashboard.js',
471
+ },
472
+ },
473
+ },
474
+ };
475
+
476
+ let registerCallCount = 0;
477
+ controller.importBundleOnce = async () => ({
478
+ SLICE_BUNDLE_META: {
479
+ bundleKey: 'routes.dashboard.v2',
480
+ type: 'route',
481
+ },
482
+ registerAll: async () => {
483
+ registerCallCount += 1;
484
+ await new Promise((resolve) => setTimeout(resolve, 10));
485
+ },
486
+ });
487
+
488
+ try {
489
+ globalThis.slice = {
490
+ stylesManager: {},
491
+ };
492
+
493
+ await Promise.all([
494
+ controller.loadBundle('dashboard'),
495
+ controller.loadBundle('DASHBOARD'),
496
+ ]);
497
+
498
+ await controller.loadBundle('Dashboard');
499
+
500
+ assert.equal(registerCallCount, 1);
501
+ assert.equal(controller.loadedBundles.has('dashboard'), true);
502
+ assert.equal(controller.loadedBundles.has('routes.dashboard.v2'), true);
503
+ assert.equal(controller.bundleLoadPromises.size, 0);
504
+ } finally {
505
+ globalThis.slice = originalSlice;
506
+ await rm(tempDir, { recursive: true, force: true });
507
+ }
508
+ });
509
+
510
+ test('registerVendorSharedDependencies only runs for vendor-shared canonical bundle or explicit metadata flag', async () => {
511
+ const tempDir = await mkdtemp(path.join(tmpdir(), 'slice-controller-loader-'));
512
+ const loaderPath = path.join(tempDir, 'components-alias-loader.mjs');
513
+ await writeFile(
514
+ loaderPath,
515
+ `export async function resolve(specifier, context, nextResolve) {
516
+ if (specifier === '/Components/components.js') {
517
+ return {
518
+ shortCircuit: true,
519
+ url: 'data:text/javascript,export default {};',
520
+ };
521
+ }
522
+ return nextResolve(specifier, context);
523
+ }
524
+ `,
525
+ 'utf8'
526
+ );
527
+ register(pathToFileURL(loaderPath).href);
528
+
529
+ const controllerModuleUrl = new URL('../Components/Structural/Controller/Controller.js', import.meta.url).href;
530
+ const { default: Controller } = await import(controllerModuleUrl);
531
+ const controller = new Controller();
532
+ const originalWindow = globalThis.window;
533
+
534
+ try {
535
+ globalThis.window = {
536
+ __SLICE_SHARED_DEPS__: {},
537
+ };
538
+
539
+ controller.registerVendorSharedDependencies(
540
+ {
541
+ SLICE_SHARED_DEPS: {
542
+ shouldNotLoad: true,
543
+ },
544
+ },
545
+ {
546
+ type: 'shared',
547
+ bundleKey: 'app-shared',
548
+ },
549
+ 'app-shared'
550
+ );
551
+
552
+ assert.equal(globalThis.window.__SLICE_SHARED_DEPS__.shouldNotLoad, undefined);
553
+
554
+ controller.registerVendorSharedDependencies(
555
+ {
556
+ SLICE_SHARED_DEPS: {
557
+ explicitFlagDependency: true,
558
+ },
559
+ },
560
+ {
561
+ type: 'shared',
562
+ bundleKey: 'app-shared',
563
+ registerVendorSharedDependencies: true,
564
+ },
565
+ 'app-shared'
566
+ );
567
+
568
+ assert.equal(globalThis.window.__SLICE_SHARED_DEPS__.explicitFlagDependency, true);
569
+ } finally {
570
+ globalThis.window = originalWindow;
571
+ await rm(tempDir, { recursive: true, force: true });
572
+ }
573
+ });
574
+
115
575
  test('Slice init fails fast with contextual error on invalid Bundling V2 contract path', async () => {
116
576
  const tempDir = await mkdtemp(path.join(tmpdir(), 'slice-init-loader-'));
117
577
  const loaderPath = path.join(tempDir, 'components-alias-loader.mjs');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "slicejs-web-framework",
3
- "version": "3.1.4",
3
+ "version": "3.2.1",
4
4
  "description": "",
5
5
  "engines": {
6
6
  "node": ">=20"