slicejs-cli 2.9.0 → 2.9.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.
|
@@ -467,7 +467,7 @@ export default class BundleGenerator {
|
|
|
467
467
|
'critical',
|
|
468
468
|
criticalFile.file
|
|
469
469
|
);
|
|
470
|
-
this.bundles.critical.integrity =
|
|
470
|
+
this.bundles.critical.integrity = `sha256:${criticalFile.hash}`;
|
|
471
471
|
this.bundles.critical.hash = criticalFile.hash;
|
|
472
472
|
files.push(criticalFile);
|
|
473
473
|
}
|
|
@@ -483,13 +483,7 @@ export default class BundleGenerator {
|
|
|
483
483
|
'route',
|
|
484
484
|
routeIdentifier
|
|
485
485
|
);
|
|
486
|
-
const routeIntegrity =
|
|
487
|
-
bundle.components,
|
|
488
|
-
'route',
|
|
489
|
-
routeIdentifier,
|
|
490
|
-
this.routeToFileName(routeIdentifier),
|
|
491
|
-
routeFile.file
|
|
492
|
-
);
|
|
486
|
+
const routeIntegrity = `sha256:${routeFile.hash}`;
|
|
493
487
|
const matchingBundle = Object.values(this.bundles.routes)
|
|
494
488
|
.find((entry) => entry.file === routeFile.file);
|
|
495
489
|
if (matchingBundle) {
|
|
@@ -540,7 +534,11 @@ export default class BundleGenerator {
|
|
|
540
534
|
return bundleContent;
|
|
541
535
|
}
|
|
542
536
|
|
|
543
|
-
const
|
|
537
|
+
const options = {
|
|
538
|
+
parse: {
|
|
539
|
+
ecma: 2022
|
|
540
|
+
},
|
|
541
|
+
ecma: 2022,
|
|
544
542
|
compress: this.options.minify ? {
|
|
545
543
|
drop_console: false,
|
|
546
544
|
drop_debugger: true,
|
|
@@ -552,12 +550,39 @@ export default class BundleGenerator {
|
|
|
552
550
|
keep_fnames: true,
|
|
553
551
|
keep_classnames: true,
|
|
554
552
|
format: {
|
|
555
|
-
comments: false
|
|
553
|
+
comments: false,
|
|
554
|
+
ecma: 2022
|
|
556
555
|
}
|
|
557
|
-
}
|
|
556
|
+
};
|
|
557
|
+
|
|
558
|
+
let result;
|
|
559
|
+
try {
|
|
560
|
+
result = await terserMinify(bundleContent, options);
|
|
561
|
+
} catch (error) {
|
|
562
|
+
const tmpDir = path.resolve(process.cwd(), '.tmp');
|
|
563
|
+
const safeName = fileName.replace(/[^a-zA-Z0-9_.-]/g, '_');
|
|
564
|
+
const tmpPath = path.join(tmpDir, `terser-fail-${safeName}`);
|
|
565
|
+
try {
|
|
566
|
+
await fs.ensureDir(tmpDir);
|
|
567
|
+
await fs.writeFile(tmpPath, bundleContent, 'utf-8');
|
|
568
|
+
} catch (writeError) {
|
|
569
|
+
console.warn(`Warning: Failed to write ${tmpPath}:`, writeError.message);
|
|
570
|
+
}
|
|
571
|
+
const message = error?.message ? `${error.message}.` : 'Unknown Terser error.';
|
|
572
|
+
throw new Error(`Terser failed for ${fileName}: ${message} Saved bundle to ${tmpPath}`);
|
|
573
|
+
}
|
|
558
574
|
|
|
559
575
|
if (result.error) {
|
|
560
|
-
|
|
576
|
+
const tmpDir = path.resolve(process.cwd(), '.tmp');
|
|
577
|
+
const safeName = fileName.replace(/[^a-zA-Z0-9_.-]/g, '_');
|
|
578
|
+
const tmpPath = path.join(tmpDir, `terser-fail-${safeName}`);
|
|
579
|
+
try {
|
|
580
|
+
await fs.ensureDir(tmpDir);
|
|
581
|
+
await fs.writeFile(tmpPath, bundleContent, 'utf-8');
|
|
582
|
+
} catch (writeError) {
|
|
583
|
+
console.warn(`Warning: Failed to write ${tmpPath}:`, writeError.message);
|
|
584
|
+
}
|
|
585
|
+
throw new Error(`Terser failed for ${fileName}: ${result.error.message}. Saved bundle to ${tmpPath}`);
|
|
561
586
|
}
|
|
562
587
|
|
|
563
588
|
return result.code || bundleContent;
|
|
@@ -737,11 +762,30 @@ export default class BundleGenerator {
|
|
|
737
762
|
// Remove imports (components will already be available)
|
|
738
763
|
code = code.replace(/import\s+.*?from\s+['"].*?['"];?\s*/g, '');
|
|
739
764
|
|
|
765
|
+
// Guard customElements.define to avoid duplicate registrations
|
|
766
|
+
code = code.replace(
|
|
767
|
+
/customElements\.define\(([^)]+)\);?/g,
|
|
768
|
+
(match, args) => {
|
|
769
|
+
const firstArg = args.split(',')[0]?.trim() || '';
|
|
770
|
+
if (!/^['"][^'"]+['"]$/.test(firstArg)) {
|
|
771
|
+
return match;
|
|
772
|
+
}
|
|
773
|
+
return `if (!customElements.get(${firstArg})) { customElements.define(${args}); }`;
|
|
774
|
+
}
|
|
775
|
+
);
|
|
776
|
+
|
|
740
777
|
// Make sure the class is available globally for bundle evaluation
|
|
741
778
|
// Preserve original customElements.define if it exists
|
|
742
779
|
if (code.includes('customElements.define')) {
|
|
743
|
-
// Add global assignment before customElements.define
|
|
744
|
-
|
|
780
|
+
// Add global assignment before guarded or direct customElements.define
|
|
781
|
+
const globalAssignment = `window.${componentName} = ${componentName};\n`;
|
|
782
|
+
const guardedDefineRegex = /if\s*\(\s*!\s*customElements\.get\([^)]*\)\s*\)\s*\{\s*customElements\.define\([^;]+\);?\s*\}\s*$/;
|
|
783
|
+
const directDefineRegex = /customElements\.define\([^;]+\);?\s*$/;
|
|
784
|
+
if (guardedDefineRegex.test(code)) {
|
|
785
|
+
code = code.replace(guardedDefineRegex, `${globalAssignment}$&`);
|
|
786
|
+
} else {
|
|
787
|
+
code = code.replace(directDefineRegex, `${globalAssignment}$&`);
|
|
788
|
+
}
|
|
745
789
|
} else {
|
|
746
790
|
// If no customElements.define found, just assign to global
|
|
747
791
|
code += `\nwindow.${componentName} = ${componentName};`;
|
|
@@ -1072,7 +1116,7 @@ if (window.slice && window.slice.controller) {
|
|
|
1072
1116
|
await fs.writeFile(filePath, finalContent, 'utf-8');
|
|
1073
1117
|
|
|
1074
1118
|
const hash = crypto.createHash('sha256').update(finalContent).digest('hex');
|
|
1075
|
-
const integrity =
|
|
1119
|
+
const integrity = `sha256:${hash}`;
|
|
1076
1120
|
|
|
1077
1121
|
return {
|
|
1078
1122
|
name: 'framework',
|