slicejs-cli 2.8.2 → 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 {
|
|
@@ -447,37 +449,74 @@ export default class BundleGenerator {
|
|
|
447
449
|
analyzeDependencies(jsContent, componentPath) {
|
|
448
450
|
const dependencies = [];
|
|
449
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
|
+
|
|
450
469
|
try {
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
const resolvedPath = path.resolve(componentPath, importPath);
|
|
462
|
-
|
|
463
|
-
// If no extension, try common extensions
|
|
464
|
-
let finalPath = resolvedPath;
|
|
465
|
-
const ext = path.extname(resolvedPath);
|
|
466
|
-
if (!ext) {
|
|
467
|
-
const extensions = ['.js', '.json', '.mjs'];
|
|
468
|
-
for (const ext of extensions) {
|
|
469
|
-
if (fs.existsSync(resolvedPath + ext)) {
|
|
470
|
-
finalPath = resolvedPath + ext;
|
|
471
|
-
break;
|
|
472
|
-
}
|
|
473
|
-
}
|
|
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;
|
|
474
480
|
}
|
|
475
481
|
|
|
476
|
-
|
|
477
|
-
|
|
482
|
+
const resolvedPath = resolveImportPath(importPath);
|
|
483
|
+
if (!resolvedPath) {
|
|
484
|
+
return;
|
|
478
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
|
+
});
|
|
479
518
|
}
|
|
480
|
-
}
|
|
519
|
+
});
|
|
481
520
|
} catch (error) {
|
|
482
521
|
console.warn(`Warning: Could not analyze dependencies for ${componentPath}:`, error.message);
|
|
483
522
|
}
|
|
@@ -500,11 +539,17 @@ export default class BundleGenerator {
|
|
|
500
539
|
const dependencyContents = {};
|
|
501
540
|
|
|
502
541
|
// Read all dependency files
|
|
503
|
-
for (const
|
|
542
|
+
for (const dep of dependencies) {
|
|
543
|
+
const depPath = dep.path;
|
|
504
544
|
try {
|
|
505
545
|
const depContent = await fs.readFile(depPath, 'utf-8');
|
|
506
|
-
const depName = path
|
|
507
|
-
|
|
546
|
+
const depName = path
|
|
547
|
+
.relative(this.srcPath, depPath)
|
|
548
|
+
.replace(/\\/g, '/');
|
|
549
|
+
dependencyContents[depName] = {
|
|
550
|
+
content: depContent,
|
|
551
|
+
bindings: dep.bindings || []
|
|
552
|
+
};
|
|
508
553
|
} catch (error) {
|
|
509
554
|
console.warn(`Warning: Could not read dependency ${depPath}:`, error.message);
|
|
510
555
|
}
|