Skip to content

Commit d68f4c8

Browse files
Merge pull request #130 from pradipchaudhary/project99
init basic tree shaking for unused code app [project99]
2 parents 3430c8b + dfbd1ab commit d68f4c8

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<link rel="stylesheet" href="styles.css" />
7+
<title>Tree Shaking App</title>
8+
</head>
9+
<body>
10+
<div class="container">
11+
<h1>Tree Shaking App</h1>
12+
<textarea
13+
id="inputCode"
14+
placeholder="Paste your JavaScript code here..."
15+
></textarea>
16+
<button onclick="performTreeShaking()">Perform Tree Shaking</button>
17+
<div id="outputCode"></div>
18+
</div>
19+
20+
<script src="script.js"></script>
21+
</body>
22+
</html>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
function performTreeShaking() {
2+
const inputCode = document.getElementById("inputCode").value;
3+
4+
if (!inputCode.trim()) {
5+
alert("Please enter JavaScript code for tree shaking.");
6+
return;
7+
}
8+
9+
// In a real-world scenario, you would use a build tool like Webpack with Babel for tree shaking.
10+
// Here, we'll use a simplified example to illustrate the process.
11+
12+
try {
13+
const treeShakenCode = treeShakeCode(inputCode);
14+
displayResult(treeShakenCode);
15+
} catch (error) {
16+
alert(`Error during tree shaking: ${error.message}`);
17+
}
18+
}
19+
20+
function treeShakeCode(code) {
21+
// Placeholder for tree shaking logic using Babel
22+
// In practice, you'd need a build tool like Webpack with Babel and proper configuration for tree shaking.
23+
// Here, we'll use a simple logic to illustrate the concept.
24+
25+
const { transform } = require("@babel/core");
26+
const presetEnv = require("@babel/preset-env");
27+
28+
const result = transform(code, {
29+
presets: [
30+
[presetEnv, { modules: false }], // Set modules to false to enable tree shaking
31+
],
32+
});
33+
34+
if (result && result.code) {
35+
return result.code;
36+
} else {
37+
throw new Error("Failed to perform tree shaking.");
38+
}
39+
}
40+
41+
function displayResult(result) {
42+
const outputCode = document.getElementById("outputCode");
43+
outputCode.textContent = result;
44+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
body {
2+
font-family: Arial, sans-serif;
3+
display: flex;
4+
align-items: center;
5+
justify-content: center;
6+
height: 100vh;
7+
margin: 0;
8+
}
9+
10+
.container {
11+
text-align: center;
12+
}
13+
14+
textarea {
15+
width: 100%;
16+
height: 150px;
17+
margin: 10px 0;
18+
}
19+
20+
button {
21+
padding: 10px 20px;
22+
font-size: 16px;
23+
margin: 10px;
24+
}
25+
26+
#outputCode {
27+
margin-top: 20px;
28+
font-size: 18px;
29+
}

0 commit comments

Comments
 (0)