1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// build.rs
//! Build script for sqlite-wasm crate.
//!
//! This build script handles copying of static assets and optional
//! minification of JavaScript assets in release builds.
//!
//! # Functions
//!
//! * Copies static assets from configured source paths to:
//! - `OUT_DIR` for compilation
//! - `pkg/` for distribution
//!
//! Currently copies:
//! - `static/sqlite.org/` → `pkg/static/sqlite.org/` (SQLite native files and WASM modules)
//! - `static/coi-serviceworker/` → `pkg/static/coi-serviceworker/` (COI service worker files)
//!
//! # Configuration
//!
//! To add more static assets, extend the `STATIC_ASSETS` array with `(source_path, dest_name)` tuples:
//! ```rust
//! const STATIC_ASSETS: &[(&str, &str)] = &[
//! ("static/sqlite.org", "sqlite.org"),
//! ("static/coi-serviceworker", "coi-serviceworker"),
//! ("node_modules/some-library/dist", "some-library"), // Copy from node_modules
//! ("third-party/assets", "vendor/assets"), // Custom source paths
//! ];
//! ```
//!
//! * In release mode, minifies JavaScript files using `minhtml`:
//! - All JavaScript files in the copied directories
//! - Including main glue code (`sqlite-wasm.js`)
//! - SQLite engine files (`sqlite3.js`, worker files, proxy)
//! - Service worker files
//!
//! # Dependencies
//!
//! Requires `minhtml` to be installed for minification:
//! ```bash
//! cargo install minhtml
//! ```
use fs;
use Path;
use env;
use Command;
/// List of (source_path, dest_name) tuples to copy.
///
/// Each entry specifies:
/// - Source path relative to project root
/// - Destination directory name under `static/` in OUT_DIR and pkg/
///
/// This flexible format allows copying from anywhere in the project
/// to a structured location in the output directories.
const STATIC_ASSETS: & = &;
/// Main build script entry point.
///
/// # Steps
/// 1. Copies all configured static assets to OUT_DIR and pkg/
/// 2. In release mode, minifies all JavaScript files in pkg/static/
/// Copies a static asset from source path to both OUT_DIR and pkg directory.
///
/// # Arguments
/// * `src_path` - Source path relative to project root (e.g., "static/sqlite.org")
/// * `dest_name` - Destination directory name under `static/` (e.g., "sqlite.org")
///
/// # Returns
/// * `Result<(), Box<dyn std::error::Error>>` - Success or error details
///
/// # Behavior
/// - Source path can be anywhere in the project (not limited to static/)
/// - Copies to `{OUT_DIR}/static/{dest_name}`
/// - Copies to `pkg/static/{dest_name}` if pkg directory exists/can be created
/// - Adds cargo rerun trigger for the source path
/// - Skips gracefully if source doesn't exist (with warning)
/// Recursively copies a directory and all its contents.
///
/// # Arguments
/// * `src` - Source directory path
/// * `dst` - Destination directory path
///
/// # Returns
/// * `Result<(), Box<dyn std::error::Error>>` - Success or error details
///
/// # Note
/// Creates destination directory if it doesn't exist and preserves
/// directory structure.
/// Minifies all JavaScript files in the pkg/static directory recursively.
///
/// # Returns
/// * `Result<(), Box<dyn std::error::Error>>` - Success or error details
///
/// # Note
/// Only runs in release mode and if pkg/static directory exists.
/// Recursively minifies JavaScript files in a directory.
///
/// # Arguments
/// * `dir` - Directory path to scan for JavaScript files
///
/// # Returns
/// * `Result<(), Box<dyn std::error::Error>>` - Success or error details
///
/// # Note
/// Only processes files with `.js` extension.
/// Minifies a single JavaScript file using minhtml.
///
/// # Arguments
/// * `path` - Path to the JavaScript file to minify
///
/// # Returns
/// * `Result<(), Box<dyn std::error::Error>>` - Success or error details
///
/// # Dependencies
/// Requires `minhtml` to be installed and available in PATH.
///
/// # Panics
/// Returns error if minhtml command fails or is not found.