Skip to content

Commit 51e2af8

Browse files
committed
Bundle JS & CSS
1 parent 6e08544 commit 51e2af8

File tree

6 files changed

+8197
-79
lines changed

6 files changed

+8197
-79
lines changed

demo.html

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta charset="UTF-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
66
<title>Button Showcase</title>
7-
<link rel="stylesheet" href="src/css/main.css"> <!-- Link to your CSS file -->
7+
<link rel="stylesheet" href="src/css/bundle.css"> <!-- Link to your CSS file -->
88
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css" integrity="sha512-Kc323vGBEqzTmouAECnVceyQqyqdsSiqLQISBL29aUW4U/M7pSPA/gEUZQqv1cwx4OnYxTxve5UMg5GT6L4JJg==" crossorigin="anonymous" referrerpolicy="no-referrer" />
99
<style>
1010
</style>
@@ -1339,13 +1339,12 @@ <h2 class="text-lg text-primary mb-4">Codeblock Component Showcase</h2>
13391339
</div>
13401340
</section>
13411341
</body>
1342-
<script type="module" src="src/js/main.js"></script>
1342+
<script type="module" src="src/js/bundle.js"></script>
13431343
<script>
13441344
// Wait for the DOM to load before setting up the event listener
13451345
document.addEventListener("DOMContentLoaded", () => {
13461346
// Select the calendar element
1347-
const calendarElement = document.querySelector('.calendar[data-variant="with-items"]');
1348-
1347+
const calendarElement = document.querySelector('.kroma-calendar[data-type="with-items"]');
13491348
// Check if the calendar element exists
13501349
if (calendarElement) {
13511350
// Add an event listener for the custom 'dateSelected' event

generateBundles.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import os
2+
import json
3+
4+
# Paths
5+
CSS_COMPONENTS_DIR = "src/css/components"
6+
CSS_UTILS_DIR = "src/css/utils"
7+
CSS_BASE_FILES = ["src/css/design-tokens.css", "src/css/base.css"]
8+
JS_COMPONENTS_DIR = "src/js/components"
9+
BUNDLE_CSS_PATH = "src/css/bundle.css"
10+
BUNDLE_JS_PATH = "src/js/bundle.js"
11+
PACKAGE_JSON_PATH = "package.json"
12+
13+
def get_package_metadata():
14+
"""Reads and extracts metadata from package.json"""
15+
try:
16+
with open(PACKAGE_JSON_PATH, "r", encoding="utf-8") as f:
17+
package_data = json.load(f)
18+
metadata = {
19+
"name": package_data.get("name", "Unknown"),
20+
"version": package_data.get("version", "0.0.0"),
21+
"description": package_data.get("description", "No description available."),
22+
"author": package_data.get("author", "Unknown"),
23+
"license": package_data.get("license", "Unlicensed"),
24+
}
25+
return metadata
26+
except FileNotFoundError:
27+
print(f"Error: {PACKAGE_JSON_PATH} not found.")
28+
return None
29+
30+
def add_file_to_bundle(file_path, bundle_file):
31+
"""Appends the content of a file to the bundle"""
32+
if os.path.exists(file_path):
33+
with open(file_path, "r", encoding="utf-8", errors="replace") as f:
34+
bundle_file.write(f"/* --- {os.path.basename(file_path)} --- */\n")
35+
bundle_file.write(f.read())
36+
bundle_file.write("\n\n")
37+
else:
38+
print(f"Warning: {file_path} not found and will not be included in the bundle.")
39+
40+
def add_directory_to_bundle(directory, bundle_file, file_extension):
41+
"""Appends the content of all files in a directory to the bundle"""
42+
for root, _, files in os.walk(directory):
43+
for file in sorted(files): # Sort files alphabetically for consistency
44+
if file.endswith(file_extension):
45+
file_path = os.path.join(root, file)
46+
add_file_to_bundle(file_path, bundle_file)
47+
48+
def create_css_bundle(bundle_path, metadata):
49+
"""Creates the CSS bundle with the specified order of files"""
50+
try:
51+
with open(bundle_path, "w", encoding="utf-8") as bundle_file:
52+
# Write metadata header
53+
bundle_file.write(f"/*\n")
54+
bundle_file.write(f"Project: {metadata['name']}\n")
55+
bundle_file.write(f"Version: {metadata['version']}\n")
56+
bundle_file.write(f"Description: {metadata['description']}\n")
57+
bundle_file.write(f"Author: {metadata['author']}\n")
58+
bundle_file.write(f"License: {metadata['license']}\n")
59+
bundle_file.write(f"*/\n\n")
60+
61+
# Add base CSS files
62+
for base_file in CSS_BASE_FILES:
63+
add_file_to_bundle(base_file, bundle_file)
64+
65+
# Add utility CSS files
66+
add_directory_to_bundle(CSS_UTILS_DIR, bundle_file, ".css")
67+
68+
# Add component CSS files
69+
add_directory_to_bundle(CSS_COMPONENTS_DIR, bundle_file, ".css")
70+
71+
print(f"{bundle_path} created successfully.")
72+
except Exception as e:
73+
print(f"Error creating {bundle_path}: {e}")
74+
75+
def create_js_bundle(directory, bundle_path, metadata):
76+
"""Creates a JS bundle by combining all files in the directory"""
77+
try:
78+
with open(bundle_path, "w", encoding="utf-8") as bundle_file:
79+
# Write metadata header
80+
bundle_file.write(f"/*\n")
81+
bundle_file.write(f"Project: {metadata['name']}\n")
82+
bundle_file.write(f"Version: {metadata['version']}\n")
83+
bundle_file.write(f"Description: {metadata['description']}\n")
84+
bundle_file.write(f"Author: {metadata['author']}\n")
85+
bundle_file.write(f"License: {metadata['license']}\n")
86+
bundle_file.write(f"*/\n\n")
87+
88+
# Add content from each file in the directory
89+
add_directory_to_bundle(directory, bundle_file, ".js")
90+
91+
print(f"{bundle_path} created successfully.")
92+
except Exception as e:
93+
print(f"Error creating {bundle_path}: {e}")
94+
95+
def main():
96+
# Fetch package metadata
97+
metadata = get_package_metadata()
98+
if not metadata:
99+
return
100+
101+
# Create CSS bundle
102+
create_css_bundle(BUNDLE_CSS_PATH, metadata)
103+
104+
# Create JS bundle
105+
create_js_bundle(JS_COMPONENTS_DIR, BUNDLE_JS_PATH, metadata)
106+
107+
if __name__ == "__main__":
108+
main()

0 commit comments

Comments
 (0)