Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions 99-Tree Shaking for Unused Code/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="styles.css" />
<title>Tree Shaking App</title>
</head>
<body>
<div class="container">
<h1>Tree Shaking App</h1>
<textarea
id="inputCode"
placeholder="Paste your JavaScript code here..."
></textarea>
<button onclick="performTreeShaking()">Perform Tree Shaking</button>
<div id="outputCode"></div>
</div>

<script src="script.js"></script>
</body>
</html>
44 changes: 44 additions & 0 deletions 99-Tree Shaking for Unused Code/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
function performTreeShaking() {
const inputCode = document.getElementById("inputCode").value;

if (!inputCode.trim()) {
alert("Please enter JavaScript code for tree shaking.");
return;
}

// In a real-world scenario, you would use a build tool like Webpack with Babel for tree shaking.
// Here, we'll use a simplified example to illustrate the process.

try {
const treeShakenCode = treeShakeCode(inputCode);
displayResult(treeShakenCode);
} catch (error) {
alert(`Error during tree shaking: ${error.message}`);
}
}

function treeShakeCode(code) {
// Placeholder for tree shaking logic using Babel
// In practice, you'd need a build tool like Webpack with Babel and proper configuration for tree shaking.
// Here, we'll use a simple logic to illustrate the concept.

const { transform } = require("@babel/core");
const presetEnv = require("@babel/preset-env");

const result = transform(code, {
presets: [
[presetEnv, { modules: false }], // Set modules to false to enable tree shaking
],
});

if (result && result.code) {
return result.code;
} else {
throw new Error("Failed to perform tree shaking.");
}
}

function displayResult(result) {
const outputCode = document.getElementById("outputCode");
outputCode.textContent = result;
}
29 changes: 29 additions & 0 deletions 99-Tree Shaking for Unused Code/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
body {
font-family: Arial, sans-serif;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
}

.container {
text-align: center;
}

textarea {
width: 100%;
height: 150px;
margin: 10px 0;
}

button {
padding: 10px 20px;
font-size: 16px;
margin: 10px;
}

#outputCode {
margin-top: 20px;
font-size: 18px;
}