1- import { emptyDir , copy , ensureDir } from "@std/fs" ;
1+ import { copy , emptyDir , ensureDir } from "@std/fs" ;
22import { join , resolve } from "@std/path" ;
3+ import { parseArgs } from "@std/cli" ;
34import postcss from "postcss" ;
45import postcssImport from "postcss-import" ;
56import postcssPresetEnv from "postcss-preset-env" ;
67import autoprefixer from "autoprefixer" ;
78import cssnano from "cssnano" ;
89
10+ const flags = parseArgs ( Deno . args , {
11+ boolean : [ "dev" ] ,
12+ } ) ;
13+ const isDev = flags . dev ;
14+
915// --- Configuration ---
1016const ROOT_DIR = Deno . cwd ( ) ;
11- const SRC_DIR = resolve ( ROOT_DIR , 'src' ) ;
12- const PUBLIC_DIR = resolve ( ROOT_DIR , 'public' ) ;
13- const DIST_DIR = resolve ( ROOT_DIR , 'dist' ) ;
14- const SW_TEMPLATE_PATH = resolve ( SRC_DIR , 'service-worker.js' ) ;
17+ const SRC_DIR = resolve ( ROOT_DIR , "src" ) ;
18+ const PUBLIC_DIR = resolve ( ROOT_DIR , "public" ) ;
19+ const DIST_DIR = resolve ( ROOT_DIR , "dist" ) ;
20+ const SW_TEMPLATE_PATH = resolve (
21+ SRC_DIR ,
22+ isDev ? "service-worker.dev.js" : "service-worker.js" ,
23+ ) ;
1524
1625async function getGitSha ( ) {
1726 try {
@@ -24,93 +33,140 @@ async function getGitSha() {
2433 return new TextDecoder ( ) . decode ( stdout ) . trim ( ) ;
2534 }
2635 } catch ( err ) {
27- console . warn ( ' Could not get git sha' , err ) ;
28- return ' dev' ;
36+ console . warn ( " Could not get git sha" , err ) ;
37+ return " dev" ;
2938 }
3039}
3140
3241async function getAssetFiles ( dir ) {
33- const assets = [ ] ;
34- for await ( const entry of Deno . readDir ( dir ) ) {
35- const fullPath = join ( dir , entry . name ) ;
36- if ( entry . isDirectory ) {
37- assets . push ( ...await getAssetFiles ( fullPath ) ) ;
38- }
39- else {
40- assets . push ( fullPath ) ;
41- }
42+ const assets = [ ] ;
43+ for await ( const entry of Deno . readDir ( dir ) ) {
44+ const fullPath = join ( dir , entry . name ) ;
45+ if ( entry . isDirectory ) {
46+ assets . push ( ...await getAssetFiles ( fullPath ) ) ;
47+ } else {
48+ assets . push ( fullPath ) ;
4249 }
43- return assets . map ( asset => asset . replace ( DIST_DIR , '' ) . replace ( / \\ / g, '/' ) ) ;
50+ }
51+ return assets . map ( ( asset ) => asset . replace ( DIST_DIR , "" ) . replace ( / \\ / g, "/" ) ) ;
4452}
4553
4654async function processCss ( ) {
47- console . log ( '🖌️ Processing CSS...' ) ;
48- const cssSrcPath = resolve ( SRC_DIR , 'css' , 'main.css' ) ;
49- const cssDistPath = resolve ( DIST_DIR , 'css' , 'main.css' ) ;
50-
51- const css = await Deno . readTextFile ( cssSrcPath ) ;
52-
53- const processor = postcss ( [
54- postcssImport ( ) ,
55- postcssPresetEnv ( ) ,
56- autoprefixer ( ) ,
57- cssnano ( ) ,
58- ] ) ;
59-
60- const result = await processor . process ( css , { from : cssSrcPath , to : cssDistPath } ) ;
61- await ensureDir ( resolve ( DIST_DIR , 'css' ) ) ;
62- await Deno . writeTextFile ( cssDistPath , result . css ) ;
63- console . log ( '✅ CSS processed successfully' ) ;
55+ console . log ( "🖌️ Processing CSS..." ) ;
56+ const cssSrcPath = resolve ( SRC_DIR , "css" , "main.css" ) ;
57+ const cssDistPath = resolve ( DIST_DIR , "css" , "main.css" ) ;
58+
59+ const css = await Deno . readTextFile ( cssSrcPath ) ;
60+
61+ const processor = postcss ( [
62+ postcssImport ( ) ,
63+ postcssPresetEnv ( ) ,
64+ autoprefixer ( ) ,
65+ cssnano ( ) ,
66+ ] ) ;
67+
68+ const result = await processor . process ( css , {
69+ from : cssSrcPath ,
70+ to : cssDistPath ,
71+ } ) ;
72+ await ensureDir ( resolve ( DIST_DIR , "css" ) ) ;
73+ await Deno . writeTextFile ( cssDistPath , result . css ) ;
74+ console . log ( "✅ CSS processed successfully" ) ;
6475}
6576
77+ async function bundleJs ( ) {
78+ console . log ( "📦 Bundling JS with deno bundle..." ) ;
79+ const command = new Deno . Command ( "deno" , {
80+ args : [
81+ "bundle" ,
82+ "--unstable-raw-imports" ,
83+ "--outdir=dist/js" ,
84+ isDev ? "" : "--minify" ,
85+ "--platform=browser" ,
86+ "src/js/main.js" ,
87+ "src/js/sw.installer.js" ,
88+ "src/js/wc/card-id1.js" ,
89+ ] . filter ( Boolean ) ,
90+ } ) ;
91+ const { code, stderr } = await command . output ( ) ;
92+ if ( code !== 0 ) {
93+ console . error ( "Error bundling JS with deno bundle:" ) ;
94+ console . error ( new TextDecoder ( ) . decode ( stderr ) ) ;
95+ } else {
96+ console . log ( "✅ JS bundled successfully" ) ;
97+ }
98+ }
6699
67- async function build ( ) {
68- console . log ( '🚀 Starting build...' ) ;
100+ async function build ( isDev = false ) {
101+ const buildType = isDev ? "Development" : "Production" ;
102+ console . log ( `🚀 Starting ${ buildType } build...` ) ;
69103
70104 // 1. Clean output directory
71105 await emptyDir ( DIST_DIR ) ;
72- console . log ( ' ✅ Cleaned dist directory' ) ;
106+ console . log ( " ✅ Cleaned dist directory" ) ;
73107
74- // 2. Copy non-CSS src files and public files to dist
75- await copy ( SRC_DIR , DIST_DIR , { overwrite : true , filter : path => ! path . includes ( '/css/' ) } ) ;
108+ // 2. Copy static assets
76109 await copy ( PUBLIC_DIR , DIST_DIR , { overwrite : true } ) ;
77- console . log ( '✅ Copied source and public files to dist' ) ;
110+ await copy ( resolve ( SRC_DIR , "assets" ) , resolve ( DIST_DIR , "assets" ) , {
111+ overwrite : true ,
112+ } ) ;
113+ await copy ( resolve ( SRC_DIR , "lib" ) , resolve ( DIST_DIR , "lib" ) , {
114+ overwrite : true ,
115+ } ) ;
116+ await copy ( resolve ( SRC_DIR , "index.html" ) , resolve ( DIST_DIR , "index.html" ) , {
117+ overwrite : true ,
118+ } ) ;
119+ await copy (
120+ resolve ( SRC_DIR , "manifest.json" ) ,
121+ resolve ( DIST_DIR , "manifest.json" ) ,
122+ { overwrite : true } ,
123+ ) ;
124+ console . log ( "✅ Copied static assets" ) ;
125+
126+ // 3. Process CSS and bundle JS in parallel
127+ await Promise . all ( [ processCss ( ) , bundleJs ( isDev ) ] ) ;
78128
79- // 3. Process CSS
80- await processCss ( ) ;
81-
82129 // 4. Get git SHA for versioning
83130 const sha = await getGitSha ( ) ;
84131 console . log ( `✅ Git SHA: ${ sha } ` ) ;
85132
86133 // 5. Collect asset paths
87134 const assetFiles = ( await getAssetFiles ( DIST_DIR ) )
88- . filter ( file => ! file . endsWith ( ' service-worker.js' ) ) ;
135+ . filter ( ( file ) => ! file . endsWith ( " service-worker.js" ) ) ;
89136 console . log ( `✅ Collected ${ assetFiles . length } asset files` ) ;
90137
91138 // 6. Inject SHA and assets into service worker
92- let swContent = await Deno . readTextFile ( SW_TEMPLATE_PATH ) ;
139+ const swTemplatePath = resolve (
140+ SRC_DIR ,
141+ isDev ? "service-worker.dev.js" : "service-worker.js" ,
142+ ) ;
143+ let swContent = await Deno . readTextFile ( swTemplatePath ) ;
93144 swContent = swContent
94145 . replace ( / _ _ B U I L D _ S H A _ _ / g, sha )
95146 . replace ( / _ _ A S S E T S _ _ / g, JSON . stringify ( assetFiles , null , 2 ) ) ;
96147
97- const swOutputPath = resolve ( DIST_DIR , ' service-worker.js' ) ;
148+ const swOutputPath = resolve ( DIST_DIR , " service-worker.js" ) ;
98149 await Deno . writeTextFile ( swOutputPath , swContent ) ;
99- console . log ( ' ✅ Injected SHA and assets into service-worker.js' ) ;
150+ console . log ( " ✅ Injected SHA and assets into service-worker.js" ) ;
100151
101152 // 7. Inject version into index.html
102- const htmlPath = resolve ( DIST_DIR , ' index.html' ) ;
153+ const htmlPath = resolve ( DIST_DIR , " index.html" ) ;
103154 let htmlContent = await Deno . readTextFile ( htmlPath ) ;
104155 htmlContent = htmlContent . replace ( / _ _ V E R S I O N _ _ / g, sha ) ;
105156 await Deno . writeTextFile ( htmlPath , htmlContent ) ;
106- console . log ( ' ✅ Injected version into index.html' ) ;
107-
157+ console . log ( " ✅ Injected version into index.html" ) ;
158+
108159 // 8. copy CNAME file over to the dist directory
109- await copy ( resolve ( ROOT_DIR , "CNAME" ) , resolve ( DIST_DIR , ' CNAME' ) ) ;
160+ await copy ( resolve ( ROOT_DIR , "CNAME" ) , resolve ( DIST_DIR , " CNAME" ) ) ;
110161 console . log ( "✅ CNAME file copied to dist directory" ) ;
111162
163+ console . log ( "🎉 Build complete!" ) ;
164+ }
112165
113- console . log ( '🎉 Build complete!' ) ;
166+ if ( import . meta. main ) {
167+ const flags = parseArgs ( Deno . args , {
168+ boolean : [ "dev" ] ,
169+ } ) ;
170+ build ( flags . dev ) ;
114171}
115172
116- build ( ) ;
0 commit comments