mini-build 0.1.0

Builds the directory a static server serves: CSS/JS bundling and minification via external tools, plus asset mirroring.
Documentation
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//! End-to-end tests against the real `lightningcss` and `esbuild` binaries.
//!
//! Unlike the `#[ignore]`d suite these replace, these run by default — they detect the
//! tools and skip loudly when they are absent, so a developer without them installed
//! still gets a passing suite while CI, which installs both, actually exercises them. An
//! `#[ignore]`d test does not run even on a machine that could run it, which is the worse
//! failure mode: the coverage exists and nobody benefits from it.
//!
//! ```sh
//! npm install -g lightningcss-cli esbuild
//! cargo test --test real_tools
//! ```

use std::fs;
use std::process::Command;

use mini_build::{Builder, CssOptions, CssTool, JsOptions, JsTool};
use tempfile::TempDir;

/// Whether `binary` can be run. Returns false and says so when it cannot, rather than
/// failing: absence is an environment fact, not a defect in this crate.
fn available(binary: &str) -> bool {
    let found = Command::new(binary)
        .arg("--version")
        .output()
        .map(|out| out.status.success())
        .unwrap_or(false);
    if !found {
        eprintln!("SKIPPING: {binary} is not on PATH — install it to run this test");
    }
    found
}

#[test]
fn css_bundle_and_minify_produces_minified_concatenated_output() {
    if !available("lightningcss") {
        return;
    }
    let src = TempDir::new().unwrap();
    let out = TempDir::new().unwrap();
    fs::write(
        src.path().join("reset.css"),
        "* {\n  margin: 0;\n  padding: 0;\n}\n",
    )
    .unwrap();
    fs::write(
        src.path().join("theme.css"),
        "body {\n  background: white;\n}\n",
    )
    .unwrap();

    Builder::new(out.path())
        .unwrap()
        .source_folder(src.path())
        .unwrap()
        .css_tool(
            CssTool::LightningCss,
            CssOptions::new().bundle(true).minify(true),
        )
        .build()
        .expect("bundle+minify build");

    let content = fs::read_to_string(out.path().join("styles.css")).unwrap();
    assert!(content.contains("margin"), "expected reset CSS in bundle");
    assert!(
        content.contains("background"),
        "expected theme CSS in bundle"
    );
    assert!(
        !content.contains('\n'),
        "minified output should not contain source formatting newlines, got: {content}"
    );
}

#[test]
fn js_bundle_resolves_the_entrys_module_graph() {
    if !available("esbuild") {
        return;
    }
    let src = TempDir::new().unwrap();
    let out = TempDir::new().unwrap();
    fs::write(
        src.path().join("helper.js"),
        "export const greeting = 'hello from helper';\n",
    )
    .unwrap();
    fs::write(
        src.path().join("main.js"),
        "import { greeting } from './helper.js';\nconsole.log(greeting);\n",
    )
    .unwrap();

    Builder::new(out.path())
        .unwrap()
        .source_folder(src.path())
        .unwrap()
        .js_tool(
            JsTool::Esbuild,
            JsOptions::new().bundle_entry(&src.path().join("main.js"), "bundle.js"),
        )
        .unwrap()
        .build()
        .expect("js bundle build");

    let content = fs::read_to_string(out.path().join("bundle.js")).unwrap();
    assert!(
        content.contains("hello from helper"),
        "the imported module's content must be inlined into the bundle, got: {content}"
    );
    assert!(
        !content.contains("import {"),
        "the import statement should be resolved away, got: {content}"
    );
}

/// Per-file mode degrades a tool failure to a raw copy rather than failing the build —
/// deliberate, so one malformed file cannot take a whole site offline. Verified here
/// against the real tool actually rejecting the input, not a stub pretending to.
#[test]
fn malformed_css_degrades_to_a_raw_copy_in_per_file_mode() {
    if !available("lightningcss") {
        return;
    }
    let src = TempDir::new().unwrap();
    let out = TempDir::new().unwrap();
    let malformed = "this is not valid css at all {{{ ;;; ";
    fs::write(src.path().join("broken.css"), malformed).unwrap();

    Builder::new(out.path())
        .unwrap()
        .source_folder(src.path())
        .unwrap()
        .css_tool(CssTool::LightningCss, CssOptions::new().minify(true))
        .build()
        .expect("a malformed source must not fail the build");

    assert_eq!(
        fs::read_to_string(out.path().join("broken.css")).unwrap(),
        malformed,
        "a file the tool rejected should reach the output as its original bytes"
    );
}

#[test]
fn prune_removes_a_stale_bundle_when_no_css_sources_remain() {
    if !available("lightningcss") {
        return;
    }
    let src = TempDir::new().unwrap();
    let out = TempDir::new().unwrap();
    fs::write(out.path().join("styles.css"), "/* stale */").unwrap();

    Builder::new(out.path())
        .unwrap()
        .source_folder(src.path())
        .unwrap()
        .css_tool(
            CssTool::LightningCss,
            CssOptions::new().bundle(true).minify(true),
        )
        .prune_output()
        .build()
        .expect("build with no css sources");

    assert!(
        !out.path().join("styles.css").exists(),
        "prune should delete the bundle when no sources remain to regenerate it"
    );
}

/// Batching sends several files to one tool invocation writing into a shared directory,
/// where results are told apart by basename only — `lightningcss --output-dir` flattens.
/// Two files named the same in different directories are therefore the exact case that
/// batching can silently lose, and the reason inputs are grouped by parent directory
/// rather than batched all at once.
#[test]
fn same_basename_in_different_directories_survives_batching() {
    if !available("lightningcss") {
        return;
    }
    let src = TempDir::new().unwrap();
    let out = TempDir::new().unwrap();
    fs::create_dir_all(src.path().join("one")).unwrap();
    fs::create_dir_all(src.path().join("two")).unwrap();
    fs::write(
        src.path().join("one/shared.css"),
        ".from-one { color: red }\n",
    )
    .unwrap();
    fs::write(
        src.path().join("two/shared.css"),
        ".from-two { color: blue }\n",
    )
    .unwrap();

    Builder::new(out.path())
        .unwrap()
        .source_folder(src.path())
        .unwrap()
        .css_tool(
            CssTool::LightningCss,
            CssOptions::new().bundle(true).minify(true),
        )
        .build()
        .expect("bundle build");

    let bundle = fs::read_to_string(out.path().join("styles.css")).unwrap();
    assert!(
        bundle.contains("from-one"),
        "the first shared.css is missing from the bundle: {bundle}"
    );
    assert!(
        bundle.contains("from-two"),
        "the second shared.css was lost — one output overwrote the other: {bundle}"
    );
}

/// The bundle's contents must be the same regardless of how files were grouped into
/// invocations. Sorted-path order is the contract; batch order is an implementation
/// detail that must not leak into the output.
#[test]
fn bundle_order_is_sorted_by_path_not_by_batch() {
    if !available("lightningcss") {
        return;
    }
    let src = TempDir::new().unwrap();
    let out = TempDir::new().unwrap();
    fs::create_dir_all(src.path().join("b-dir")).unwrap();
    fs::create_dir_all(src.path().join("a-dir")).unwrap();
    fs::write(src.path().join("b-dir/z.css"), ".b-dir-z{color:red}\n").unwrap();
    fs::write(src.path().join("a-dir/a.css"), ".a-dir-a{color:blue}\n").unwrap();

    Builder::new(out.path())
        .unwrap()
        .source_folder(src.path())
        .unwrap()
        .css_tool(CssTool::LightningCss, CssOptions::new().bundle(true))
        .build()
        .expect("bundle build");

    let bundle = fs::read_to_string(out.path().join("styles.css")).unwrap();
    let a_pos = bundle.find("a-dir-a").expect("a-dir content present");
    let b_pos = bundle.find("b-dir-z").expect("b-dir content present");
    assert!(
        a_pos < b_pos,
        "a-dir/a.css sorts before b-dir/z.css and must appear first: {bundle}"
    );
}

/// The case batching could quietly regress. Per-file mode promises that a malformed
/// source degrades to a raw copy *and its neighbours are still minified*. Batched, one
/// bad file fails the whole invocation, so the fallback has to rebuild the group
/// individually — not give up and copy everything raw.
#[test]
fn one_malformed_file_does_not_stop_its_neighbours_being_minified() {
    if !available("lightningcss") {
        return;
    }
    let src = TempDir::new().unwrap();
    let out = TempDir::new().unwrap();
    let malformed = "this is not valid css at all {{{ ;;; ";
    fs::write(src.path().join("broken.css"), malformed).unwrap();
    fs::write(
        src.path().join("good.css"),
        ".good {\n  color: red;\n  margin: 0;\n}\n",
    )
    .unwrap();

    Builder::new(out.path())
        .unwrap()
        .source_folder(src.path())
        .unwrap()
        .css_tool(CssTool::LightningCss, CssOptions::new().minify(true))
        .build()
        .expect("a malformed source must not fail the build");

    assert_eq!(
        fs::read_to_string(out.path().join("broken.css")).unwrap(),
        malformed,
        "the rejected file should reach the output as its original bytes"
    );

    let good = fs::read_to_string(out.path().join("good.css")).unwrap();
    assert!(
        !good.contains('\n') && good.contains("good"),
        "the valid neighbour must still be minified, not copied raw: {good:?}"
    );
}

/// The JS batch path mirrors the CSS one, and a mirrored implementation is exactly the
/// kind that drifts silently. These cover it directly rather than trusting the copy.
#[test]
fn js_per_file_mode_minifies_several_files_in_one_batch() {
    if !available("esbuild") {
        return;
    }
    let src = TempDir::new().unwrap();
    let out = TempDir::new().unwrap();
    fs::write(
        src.path().join("a.js"),
        "export const alpha = {\n  value: 1,\n};\n",
    )
    .unwrap();
    fs::write(
        src.path().join("b.js"),
        "export const beta = {\n  value: 2,\n};\n",
    )
    .unwrap();

    Builder::new(out.path())
        .unwrap()
        .source_folder(src.path())
        .unwrap()
        .js_tool(JsTool::Esbuild, JsOptions::new().minify(true))
        .unwrap()
        .build()
        .expect("js per-file build");

    for (name, marker) in [("a.js", "alpha"), ("b.js", "beta")] {
        let content = fs::read_to_string(out.path().join(name)).unwrap();
        assert!(
            content.contains(marker),
            "{name} lost its content: {content}"
        );
        assert!(
            !content.contains("  value"),
            "{name} was copied rather than minified: {content}"
        );
    }
}

#[test]
fn js_same_basename_in_different_directories_survives_batching() {
    if !available("esbuild") {
        return;
    }
    let src = TempDir::new().unwrap();
    let out = TempDir::new().unwrap();
    fs::create_dir_all(src.path().join("one")).unwrap();
    fs::create_dir_all(src.path().join("two")).unwrap();
    fs::write(
        src.path().join("one/shared.js"),
        "export const fromOne = 1;\n",
    )
    .unwrap();
    fs::write(
        src.path().join("two/shared.js"),
        "export const fromTwo = 2;\n",
    )
    .unwrap();

    Builder::new(out.path())
        .unwrap()
        .source_folder(src.path())
        .unwrap()
        .js_tool(JsTool::Esbuild, JsOptions::new().minify(true))
        .unwrap()
        .build()
        .expect("js per-file build");

    assert!(
        fs::read_to_string(out.path().join("one/shared.js"))
            .unwrap()
            .contains("fromOne"),
        "one/shared.js is missing or holds the wrong content"
    );
    assert!(
        fs::read_to_string(out.path().join("two/shared.js"))
            .unwrap()
            .contains("fromTwo"),
        "two/shared.js was overwritten by its namesake"
    );
}

#[test]
fn one_malformed_js_file_does_not_stop_its_neighbours_being_minified() {
    if !available("esbuild") {
        return;
    }
    let src = TempDir::new().unwrap();
    let out = TempDir::new().unwrap();
    let malformed = "function ( { this is not javascript\n";
    fs::write(src.path().join("broken.js"), malformed).unwrap();
    fs::write(
        src.path().join("good.js"),
        "export const good = {\n  value: 1,\n};\n",
    )
    .unwrap();

    Builder::new(out.path())
        .unwrap()
        .source_folder(src.path())
        .unwrap()
        .js_tool(JsTool::Esbuild, JsOptions::new().minify(true))
        .unwrap()
        .build()
        .expect("a malformed source must not fail the build");

    assert_eq!(
        fs::read_to_string(out.path().join("broken.js")).unwrap(),
        malformed,
        "the rejected file should reach the output as its original bytes"
    );
    let good = fs::read_to_string(out.path().join("good.js")).unwrap();
    assert!(
        !good.contains("  value") && good.contains("good"),
        "the valid neighbour must still be minified: {good:?}"
    );
}