Lines 154-194javascript
155 Object.keys(originalDeps).forEach((dep) => {
156 if (!usedModules.has(dep)) {
157 console.log(`🗑️ Pruning unused package: ${dep}`);
158 delete updatedDeps[dep];
164 console.log("✨ No unused dependencies detected. Project is already optimized!");
168 // 5. Permanently write the optimized package.json to disk so AWS uses it
169 packageJson.dependencies = updatedDeps;
170 fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
172 // 6. Run the optimized installation execution block
173 console.log(`⚡ Running optimized npm install...`);
175 execSync('npm install --omit=dev', { stdio: 'inherit' });
176 } catch (error) {
HighRuntime Package Install
Package source invokes a package manager install command at runtime.
clean-deploy.jsView on unpkg · L174 177 console.error("❌ Error during optimized npm install:", error.message);
178 // Restore backup if npm install crashes completely
179 fs.cpSync(path.join(backupDir, 'package.json.bak'), packageJsonPath);
183 // 7. Measure the optimized size directly after installation removes the folders
184 const sizeAfter = getSizeInMB(nodeModulesPath);
186 // 8. Output final metric metrics report dashboard
187 console.log(`\n🎉 Production node_modules successfully optimized!`);
188 console.log(`💡 If anything broke, restore anytime by running your script with: --rollback`);
189 console.log(`-----------------------------------------------`);
190 console.log(`📊 Before Optimization : ${sizeBefore.toFixed(2)} MB`);
191 console.log(`📊 After Optimization : ${sizeAfter.toFixed(2)} MB`);
193 const savings = (sizeBefore - sizeAfter).toFixed(2);