slicejs-cli 2.8.1 → 2.8.3

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.
@@ -2,6 +2,8 @@
2
2
  import fs from 'fs-extra';
3
3
  import path from 'path';
4
4
  import crypto from 'crypto';
5
+ import { parse } from '@babel/parser';
6
+ import traverse from '@babel/traverse';
5
7
  import { getSrcPath, getComponentsJsPath } from '../PathHelper.js';
6
8
 
7
9
  export default class BundleGenerator {
@@ -398,10 +400,14 @@ export default class BundleGenerator {
398
400
 
399
401
  // 2. Route bundles
400
402
  for (const [routeKey, bundle] of Object.entries(this.bundles.routes)) {
403
+ const routeIdentifier = Array.isArray(bundle.path || bundle.paths)
404
+ ? routeKey
405
+ : (bundle.path || bundle.paths || routeKey);
406
+
401
407
  const routeFile = await this.createBundleFile(
402
408
  bundle.components,
403
409
  'route',
404
- bundle.path || bundle.paths || routeKey // Use routeKey as fallback for hybrid bundles
410
+ routeIdentifier
405
411
  );
406
412
  files.push(routeFile);
407
413
  }
@@ -443,37 +449,74 @@ export default class BundleGenerator {
443
449
  analyzeDependencies(jsContent, componentPath) {
444
450
  const dependencies = [];
445
451
 
452
+ const resolveImportPath = (importPath) => {
453
+ const resolvedPath = path.resolve(componentPath, importPath);
454
+ let finalPath = resolvedPath;
455
+ const ext = path.extname(resolvedPath);
456
+ if (!ext) {
457
+ const extensions = ['.js', '.json', '.mjs'];
458
+ for (const extension of extensions) {
459
+ if (fs.existsSync(resolvedPath + extension)) {
460
+ finalPath = resolvedPath + extension;
461
+ break;
462
+ }
463
+ }
464
+ }
465
+
466
+ return fs.existsSync(finalPath) ? finalPath : null;
467
+ };
468
+
446
469
  try {
447
- // Simple regex to find import statements
448
- const importRegex = /import\s+.*?\s+from\s+['"`]([^'"`]+)['"`]/g;
449
- let match;
450
-
451
- while ((match = importRegex.exec(jsContent)) !== null) {
452
- const importPath = match[1];
453
-
454
- // Only process relative imports (starting with ./ or ../)
455
- if (importPath.startsWith('./') || importPath.startsWith('../')) {
456
- // Resolve the absolute path
457
- const resolvedPath = path.resolve(componentPath, importPath);
458
-
459
- // If no extension, try common extensions
460
- let finalPath = resolvedPath;
461
- const ext = path.extname(resolvedPath);
462
- if (!ext) {
463
- const extensions = ['.js', '.json', '.mjs'];
464
- for (const ext of extensions) {
465
- if (fs.existsSync(resolvedPath + ext)) {
466
- finalPath = resolvedPath + ext;
467
- break;
468
- }
469
- }
470
+ const ast = parse(jsContent, {
471
+ sourceType: 'module',
472
+ plugins: ['jsx']
473
+ });
474
+
475
+ traverse.default(ast, {
476
+ ImportDeclaration(pathNode) {
477
+ const importPath = pathNode.node.source.value;
478
+ if (!importPath.startsWith('./') && !importPath.startsWith('../')) {
479
+ return;
470
480
  }
471
481
 
472
- if (fs.existsSync(finalPath)) {
473
- dependencies.push(finalPath);
482
+ const resolvedPath = resolveImportPath(importPath);
483
+ if (!resolvedPath) {
484
+ return;
474
485
  }
486
+
487
+ const bindings = pathNode.node.specifiers.map(spec => {
488
+ if (spec.type === 'ImportDefaultSpecifier') {
489
+ return {
490
+ type: 'default',
491
+ importedName: 'default',
492
+ localName: spec.local.name
493
+ };
494
+ }
495
+
496
+ if (spec.type === 'ImportSpecifier') {
497
+ return {
498
+ type: 'named',
499
+ importedName: spec.imported.name,
500
+ localName: spec.local.name
501
+ };
502
+ }
503
+
504
+ if (spec.type === 'ImportNamespaceSpecifier') {
505
+ return {
506
+ type: 'namespace',
507
+ localName: spec.local.name
508
+ };
509
+ }
510
+
511
+ return null;
512
+ }).filter(Boolean);
513
+
514
+ dependencies.push({
515
+ path: resolvedPath,
516
+ bindings
517
+ });
475
518
  }
476
- }
519
+ });
477
520
  } catch (error) {
478
521
  console.warn(`Warning: Could not analyze dependencies for ${componentPath}:`, error.message);
479
522
  }
@@ -496,11 +539,17 @@ export default class BundleGenerator {
496
539
  const dependencyContents = {};
497
540
 
498
541
  // Read all dependency files
499
- for (const depPath of dependencies) {
542
+ for (const dep of dependencies) {
543
+ const depPath = dep.path;
500
544
  try {
501
545
  const depContent = await fs.readFile(depPath, 'utf-8');
502
- const depName = path.basename(depPath, path.extname(depPath));
503
- dependencyContents[depName] = depContent;
546
+ const depName = path
547
+ .relative(this.srcPath, depPath)
548
+ .replace(/\\/g, '/');
549
+ dependencyContents[depName] = {
550
+ content: depContent,
551
+ bindings: dep.bindings || []
552
+ };
504
553
  } catch (error) {
505
554
  console.warn(`Warning: Could not read dependency ${depPath}:`, error.message);
506
555
  }
@@ -627,9 +676,13 @@ if (window.slice && window.slice.controller) {
627
676
  };
628
677
 
629
678
  for (const [key, bundle] of Object.entries(this.bundles.routes)) {
679
+ const routeIdentifier = Array.isArray(bundle.path || bundle.paths)
680
+ ? key
681
+ : (bundle.path || bundle.paths || key);
682
+
630
683
  config.bundles.routes[key] = {
631
684
  path: bundle.path || bundle.paths || key, // Support both single path and array of paths, fallback to key
632
- file: `slice-bundle.${this.routeToFileName(bundle.path || bundle.paths || key)}.js`,
685
+ file: `slice-bundle.${this.routeToFileName(routeIdentifier)}.js`,
633
686
  size: bundle.size,
634
687
  components: bundle.components.map(c => c.name),
635
688
  dependencies: ['critical']
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "slicejs-cli",
3
- "version": "2.8.1",
3
+ "version": "2.8.3",
4
4
  "description": "Command client for developing web applications with Slice.js framework",
5
5
  "main": "client.js",
6
6
  "bin": {