/
main.rs
1328 lines (1210 loc) · 42.4 KB
/
main.rs
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#![cfg_attr(test, feature(test))]
#[macro_use]
extern crate esparse;
extern crate crossbeam;
extern crate num_cpus;
extern crate notify;
extern crate json;
extern crate memchr;
extern crate base64;
#[macro_use]
extern crate matches;
use std::{env, process, io, fs, thread, time, iter, fmt};
use std::io::prelude::*;
use std::fmt::Write;
use std::path::{self, PathBuf, Path, Component};
use std::sync::mpsc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::collections::{HashMap, HashSet};
use std::any::Any;
use std::borrow::Cow;
use std::ffi::OsString;
use crossbeam::sync::SegQueue;
use notify::Watcher;
use esparse::lex::{self, Tt};
mod opts;
mod es6;
const HEAD_JS: &str = include_str!("head.js");
const TAIL_JS: &str = include_str!("tail.js");
fn cjs_parse_deps<'f, 's>(lex: &mut lex::Lexer<'f, 's>) -> Result<HashSet<Cow<'s, str>>, CliError> {
// TODO should we panic on dynamic requires?
let mut deps = HashSet::new();
loop {
eat!(lex,
// Tt::Id(s) if s == "require" => eat!(lex,
Tt::Id("require") => eat!(lex,
Tt::Lparen => eat!(lex,
Tt::StrLitSgl(s) |
Tt::StrLitDbl(s) => eat!(lex,
Tt::Rparen => {
deps.insert(lex::str_lit_value(s)?);
},
_ => {},
),
_ => {},
),
// Tt::Dot => eat!(lex,
// Tt::Id("resolve") => eat!(lex,
// Tt::Lparen => eat!(lex,
// Tt::StrLitSgl(s) |
// Tt::StrLitDbl(s) => eat!(lex,
// Tt::Rparen => {
// // TODO handle error
// return Some(lex::str_lit_value(s).unwrap())
// },
// _ => {},
// ),
// _ => {},
// ),
// _ => {},
// ),
// _ => {},
// ),
_ => {},
),
Tt::Eof => return Ok(deps),
_ => {
lex.advance();
},
);
}
}
#[derive(Debug)]
struct Writer<'a, 'b> {
modules: HashMap<PathBuf, Module>,
entry_point: &'a Path,
map_output: &'b SourceMapOutput<'b>,
}
impl<'a, 'b> Writer<'a, 'b> {
fn sorted_modules(&self) -> Vec<(&PathBuf, &Module)> {
let mut modules = self.modules.iter().collect::<Vec<_>>();
modules.sort_by(|&(ref f, _), &(ref g, _)| f.cmp(g));
modules
}
fn write_to<W: io::Write>(&self, w: &mut W) -> io::Result<()> {
w.write_all(HEAD_JS.as_bytes())?;
// for (module, main) in self.mains {
// write!(w,
// "\n Pax.mains[{mod_path}] = {main_path}",
// mod_path = Self::js_path(&module),
// main_path = Self::js_path(&main),
// );
// }
for (file, info) in self.sorted_modules() {
let id = Self::name_path(&file);
let deps = Self::stringify_deps(&info.deps);
let filename = Self::js_path(&file);
write!(w,
"\n Pax.files[{filename}] = {id}; {id}.deps = {deps}; {id}.filename = {filename}; function {id}(module, exports, require) {{\n",
filename = filename,
id = id,
deps = deps,
)?;
if !info.source.prefix.is_empty() {
w.write_all(info.source.prefix.as_bytes())?;
w.write_all(b"\n")?;
}
w.write_all(info.source.body.as_bytes())?;
if !matches!(info.source.body.chars().last(), None | Some('\n') | Some('\r') | Some('\u{2028}') | Some('\u{2029}')) {
w.write_all(b"\n")?;
}
if !info.source.suffix.is_empty() {
w.write_all(info.source.suffix.as_bytes())?;
}
write!(w, "}}")?;
}
let main = Self::name_path(self.entry_point);
write!(w,
"\n Pax.main = {main}; Pax.makeRequire(null)()\n if (typeof module !== 'undefined') module.exports = Pax.main.module && Pax.main.module.exports\n",
main = main,
)?;
w.write_all(TAIL_JS.as_bytes())?;
match *self.map_output {
SourceMapOutput::Suppressed => {}
SourceMapOutput::Inline => {
let mut map = Vec::new();
self.write_map_to(&mut map)?;
write!(w,
"//# sourceMappingURL=data:application/json;charset=utf-8;base64,{data}\n",
data = base64::encode(&map),
)?;
}
SourceMapOutput::File(ref path, output_file) => {
// TODO handle error
let relative = path.relative_from(output_file.parent().unwrap());
let map = relative.as_ref().unwrap_or(path);
write!(w,
"//# sourceMappingURL={map}\n",
map = map.display(),
)?;
}
}
Ok(())
}
fn write_map_to<W: io::Write>(&self, w: &mut W) -> io::Result<()> {
// TODO this is borderline unmaintainable, but constructing a JsonValue is *really* wasteful
let modules = self.sorted_modules();
let dir = self.entry_point.parent().unwrap();
w.write_all(br#"{"version":3,"file":"","sourceRoot":"","sources":["#)?;
let mut comma = false;
for &(f, _) in &modules {
if comma {
w.write_all(b",")?;
}
let relative = f.relative_from(dir);
let path = relative.as_ref().unwrap_or(f);
let string = path.to_string_lossy();
write_json_string(w, string.as_ref())?;
comma = true;
}
w.write_all(br#"],"sourcesContent":["#)?;
let mut comma = false;
for &(_, module) in &modules {
if comma {
w.write_all(b",")?;
}
write_json_string(w, module.source.original.as_ref().unwrap_or_else(|| &module.source.body))?;
comma = true;
}
w.write_all(br#"],"names":[],"mappings":""#)?;
let prefix_len = count_lines(HEAD_JS); /*+ this.mains.size*/
for _ in 0..prefix_len {
w.write_all(b";")?;
}
let mut line = 0;
let mut buf = [0u8; 13];
for (index, &(_, module)) in modules.iter().enumerate() {
w.write_all(b";")?;
if !module.source.prefix.is_empty() {
for _ in 0..count_lines(&module.source.prefix) {
w.write_all(b";")?;
}
}
for i in 0..count_lines(&module.source.body) {
w.write_all(b"A")?;
if i == 0 {
if index == 0 {
w.write_all(b"AAA")?;
} else {
w.write_all(b"C")?;
w.write_all(vlq(&mut buf, -line))?;
w.write_all(b"A")?;
}
line = 0;
} else {
w.write_all(b"ACA")?;
line += 1;
}
w.write_all(b";")?;
}
if !matches!(module.source.body.chars().last(), None | Some('\n') | Some('\r') | Some('\u{2028}') | Some('\u{2029}')) {
w.write_all(b";")?;
}
for _ in 0..count_lines(&module.source.suffix)-1 {
w.write_all(b";")?;
}
}
for _ in 0..2 + count_lines(TAIL_JS) + 1 - 1 - 1 {
w.write_all(b";")?;
}
w.write_all(br#""}"#)?;
Ok(())
}
fn stringify_deps(deps: &HashMap<String, Resolved>) -> String {
let mut result = "{".to_owned();
let mut comma = false;
for (name, resolved) in deps {
match *resolved {
Resolved::Core => {}
Resolved::Normal(ref path) => {
if comma {
result.push(',');
}
result.push_str(&json::stringify(json::JsonValue::from(name.as_ref())));
result.push(':');
Self::write_name_path(path, &mut result);
comma = true;
}
}
}
result.push('}');
result
}
#[cfg(target_os = "windows")]
fn js_path(path: &Path) -> String {
// TODO untested
let string = path.to_string_lossy();
let replaced = string.as_ref().replace('\\', "/");
json::stringify(json::JsonValue::from(replaced))
}
#[cfg(not(target_os = "windows"))]
fn js_path(path: &Path) -> String {
let string = path.to_string_lossy().into_owned();
json::stringify(json::JsonValue::from(string))
}
fn name_path(path: &Path) -> String {
let mut result = String::new();
Self::write_name_path(path, &mut result);
result
}
fn write_name_path(path: &Path, result: &mut String) {
let string = path.to_string_lossy();
// let slice = string.as_ref();
let bytes = string.as_bytes();
result.push_str("file_");
for &b in bytes {
match b {
b'_' | b'a'...b'z' | b'A'...b'Z' => {
result.push(b as char);
}
_ => {
write!(result, "${:02x}", b).unwrap();
}
}
}
// let mut last_pos = 0;
// for pos in bytes.match_indices(|&b| {
// !matches!(b as char, '_' | 'a'...'z' | 'A'...'Z')
// }) {
// result.push_str(slice[last_pos..pos]);
// write!(result, "${:02x}", bytes[pos]);
// last_pos = pos + 1;
// }
}
}
fn count_lines(source: &str) -> usize {
// TODO non-ASCII line terminators?
1 + memchr::Memchr::new(b'\n', source.as_bytes()).count()
}
const QU: u8 = b'"';
const BS: u8 = b'\\';
const BB: u8 = b'b';
const TT: u8 = b't';
const NN: u8 = b'n';
const FF: u8 = b'f';
const RR: u8 = b'r';
const UU: u8 = b'u';
const __: u8 = 0;
static ESCAPED: [u8; 256] = [
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
UU, UU, UU, UU, UU, UU, UU, UU, BB, TT, NN, UU, FF, RR, UU, UU, // 0
UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, // 1
__, __, QU, __, __, __, __, __, __, __, __, __, __, __, __, __, // 2
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 3
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 4
__, __, __, __, __, __, __, __, __, __, __, __, BS, __, __, __, // 5
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 6
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 7
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 8
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 9
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // A
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // B
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // C
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // D
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // E
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // F
];
#[inline(never)]
#[cold]
fn write_json_string_complex<W: io::Write>(w: &mut W, string: &str, mut start: usize) -> io::Result<()> {
w.write_all(string[..start].as_bytes())?;
for (index, ch) in string.bytes().enumerate().skip(start) {
let escape = ESCAPED[ch as usize];
if escape > 0 {
w.write_all(string[start..index].as_bytes())?;
w.write_all(&[b'\\', escape])?;
start = index + 1;
}
if escape == b'u' {
write!(w, "{:04x}", ch)?;
}
}
w.write_all(string[start..].as_bytes())?;
w.write_all(&[b'"'])?;
Ok(())
}
#[inline]
fn write_json_string<W: io::Write>(w: &mut W, string: &str) -> io::Result<()> {
w.write_all(&[b'"'])?;
for (index, ch) in string.bytes().enumerate() {
if ESCAPED[ch as usize] > 0 {
return write_json_string_complex(w, string, index)
}
}
w.write_all(string.as_bytes())?;
w.write_all(&[b'"'])?;
Ok(())
}
fn vlq(buf: &mut [u8; 13], n: isize) -> &[u8] {
let sign = n < 0;
let n = if sign { -n } else { n } as usize;
let mut y = (n & 0xf) << 1 | sign as usize;
let mut r = n >> 4;
let mut l = 0;
while r > 0 {
y |= 0x20;
buf[l] = B64[y];
y = r & 0x1f;
r >>= 5;
l += 1;
}
buf[l] = B64[y];
&buf[0..l+1]
}
const B64: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
#[derive(Debug, Clone)]
struct Worker {
tx: mpsc::Sender<Result<WorkDone, CliError>>,
input_options: InputOptions,
queue: Arc<SegQueue<Work>>,
quit: Arc<AtomicBool>,
}
#[derive(Debug)]
enum Work {
Resolve { context: PathBuf, name: String },
Include { module: PathBuf },
}
#[derive(Debug)]
enum WorkDone {
Resolve { context: PathBuf, name: String, resolved: Resolved },
Include { module: PathBuf, info: ModuleInfo },
}
#[derive(Debug)]
enum ModuleState {
Loading,
Loaded(Module),
}
#[derive(Debug)]
pub struct Module {
pub source: Source,
pub deps: HashMap<String, Resolved>,
}
#[derive(Debug)]
struct ModuleInfo {
source: Source,
deps: Vec<String>,
}
#[derive(Debug)]
pub struct Source {
pub prefix: String,
pub body: String,
pub suffix: String,
pub original: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Resolved {
Core,
// CoreWithSubst(PathBuf),
Normal(PathBuf),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct InputOptions {
pub es6_syntax: bool,
pub es6_syntax_everywhere: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SourceMapOutput<'a> {
Suppressed,
Inline,
File(PathBuf, &'a Path),
}
impl ModuleState {
fn expect(self, message: &str) -> Module {
match self {
ModuleState::Loading => panic!("{}", message),
ModuleState::Loaded(module) => module,
}
}
fn unwrap(self) -> Module {
self.expect("unwrapped ModuleState that was still loading")
}
}
pub fn bundle(entry_point: &Path, input_options: InputOptions, output: &str, map_output: &SourceMapOutput) -> Result<HashMap<PathBuf, Module>, CliError> {
let mut pending = 0;
let thread_count = num_cpus::get();
let (tx, rx) = mpsc::channel();
let worker = Worker {
tx,
input_options,
quit: Arc::new(AtomicBool::new(false)),
queue: Arc::new(SegQueue::new()),
};
// TODO: context.require('…')
// TODO: watch for missing files on error?
let mut modules = HashMap::<PathBuf, ModuleState>::new();
worker.add_work(Work::Include { module: entry_point.to_owned() });
pending += 1;
modules.insert(entry_point.to_owned(), ModuleState::Loading);
let children: Vec<_> = (0..thread_count).map(|_| {
let worker = worker.clone();
thread::spawn(move || worker.run())
}).collect();
// let children: Vec<_> = (0..thread_count).map(|n| {
// let worker = worker.clone();
// thread::Builder::new().name(format!("worker #{}", n + 1)).spawn(move || worker.run()).unwrap()
// }).collect();
while let Ok(work_done) = rx.recv() {
// eprintln!("{:?}", work_done);
let work_done = match work_done {
Err(error) => {
worker.quit.store(true, Ordering::Relaxed);
return Err(error)
}
Ok(work_done) => {
pending -= 1;
work_done
}
};
match work_done {
WorkDone::Resolve { context, name, resolved } => {
match *modules.get_mut(&context).unwrap() {
ModuleState::Loading => unreachable!(),
ModuleState::Loaded(Module { ref mut deps, .. }) => {
deps.insert(name, resolved.clone());
}
}
match resolved {
Resolved::Core => {}
Resolved::Normal(module) => {
modules.entry(module.clone()).or_insert_with(|| {
worker.add_work(Work::Include { module });
pending += 1;
ModuleState::Loading
});
}
}
}
WorkDone::Include { module, info } => {
let old = modules.insert(module.clone(), ModuleState::Loaded(Module {
source: info.source,
deps: HashMap::new(),
}));
debug_assert_matches!(old, Some(ModuleState::Loading));
for dep in info.deps {
worker.add_work(Work::Resolve {
context: module.clone(),
name: dep,
});
pending += 1;
}
}
}
if pending == 0 {
break
}
}
worker.quit.store(true, Ordering::Relaxed);
for child in children {
child.join()?;
}
let writer = Writer {
modules: modules.into_iter()
.map(|(k, ms)| (k, ms.unwrap()))
.collect(),
entry_point,
map_output,
};
match &*output {
"-" => {
let stdout = io::stdout();
let mut handle = stdout.lock();
writer.write_to(&mut handle)?;
}
_ => {
let output = Path::new(output);
if let Some(parent) = output.parent() {
fs::create_dir_all(parent)?;
}
let file = fs::File::create(&output)?;
let mut buf_writer = io::BufWriter::new(file);
writer.write_to(&mut buf_writer)?;
}
}
match *map_output {
SourceMapOutput::Suppressed => {}
SourceMapOutput::Inline => {
// handled in Writer::write_to()
}
SourceMapOutput::File(ref path, _) => {
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)?;
}
let file = fs::File::create(path)?;
let mut buf_writer = io::BufWriter::new(file);
writer.write_map_to(&mut buf_writer)?;
}
}
// println!("entry point: {:?}", entry_point);
// println!("{:#?}", modules);
Ok(writer.modules)
}
fn run() -> Result<(), CliError> {
let entry_inst = time::Instant::now();
let mut input = None;
let mut output = None;
let mut map = None;
let mut es6_syntax = false;
let mut es6_syntax_everywhere = false;
let mut map_inline = false;
let mut no_map = false;
let mut watch = false;
let mut quiet_watch = false;
let mut iter = opts::args();
while let Some(arg) = iter.next() {
let opt = match arg {
opts::Arg::Pos(arg) => {
if input.is_none() {
input = Some(arg)
} else if output.is_none() {
output = Some(arg)
} else {
return Err(CliError::UnexpectedArg(arg))
}
continue
}
opts::Arg::Opt(opt) => opt,
};
match &*opt {
"-h" | "--help" => return Err(CliError::Help),
"-v" | "--version" => return Err(CliError::Version),
"-w" | "--watch" => watch = true,
"-W" | "--quiet-watch" => {
watch = true;
quiet_watch = true;
},
"-I" | "--map-inline" => map_inline = true,
"-M" | "--no-map" => no_map = true,
"-e" | "--es-syntax" => es6_syntax = true,
"-E" | "--es-syntax-everywhere" => {
es6_syntax = true;
es6_syntax_everywhere = true;
}
"-m" | "--map" => {
if map.is_some() {
return Err(CliError::DuplicateOption(opt))
}
map = Some(iter.next_arg().ok_or_else(|| CliError::MissingOptionValue(opt))?)
}
"-i" | "--input" => {
if input.is_some() {
return Err(CliError::DuplicateOption(opt))
}
input = Some(iter.next_arg().ok_or_else(|| CliError::MissingOptionValue(opt))?)
}
"-o" | "--output" => {
if output.is_some() {
return Err(CliError::DuplicateOption(opt))
}
output = Some(iter.next_arg().ok_or_else(|| CliError::MissingOptionValue(opt))?)
}
_ => {
return Err(CliError::UnknownOption(opt))
}
}
}
if map_inline as u8 + no_map as u8 + map.is_some() as u8 > 1 {
return Err(CliError::BadUsage("--map-inline, --map <file>, and --no-map are mutually exclusive"))
}
let input = input.ok_or(CliError::MissingFileName)?;
let input_dir = env::current_dir()?;
let output = output.unwrap_or_else(|| "-".to_owned());
let map_output = if map_inline {
SourceMapOutput::Inline
} else if no_map {
SourceMapOutput::Suppressed
} else {
match map {
Some(path) => {
SourceMapOutput::File(PathBuf::from(path), Path::new(&output))
}
None => {
if output == "-" {
SourceMapOutput::Suppressed
} else {
let mut buf = OsString::from(&output);
buf.push(".map");
SourceMapOutput::File(PathBuf::from(buf), Path::new(&output))
}
}
}
};
let input_options = InputOptions {
es6_syntax,
es6_syntax_everywhere,
};
let entry_point = Worker::resolve_main(input_options, input_dir, &input)?;
if watch {
let mut modules = bundle(&entry_point, input_options, &output, &map_output)?;
let elapsed = entry_inst.elapsed();
let ms = elapsed.as_secs() * 1_000 + u64::from(elapsed.subsec_nanos() / 1_000_000);
let (tx, rx) = mpsc::channel();
let debounce_dur = time::Duration::from_millis(5);
let mut watcher = notify::raw_watcher(tx.clone())?;
for path in modules.keys() {
watcher.watch(path, notify::RecursiveMode::NonRecursive)?;
}
eprintln!("ready in {} ms", ms);
loop {
let first_event = rx.recv().expect("notify::watcher disconnected");
thread::sleep(debounce_dur);
for event in iter::once(first_event).chain(rx.try_iter()) {
let _op = event.op?;
}
let start_inst = time::Instant::now();
match bundle(&entry_point, input_options, &output, &map_output) {
Ok(new_modules) => {
let elapsed = start_inst.elapsed();
let ms = elapsed.as_secs() * 1_000 + u64::from(elapsed.subsec_nanos() / 1_000_000);
eprintln!("generate {output} in {ms} ms",
output = output,
ms = ms);
{
let mut to_unwatch = modules.keys().collect::<HashSet<_>>();
let mut to_watch = new_modules.keys().collect::<HashSet<_>>();
for path in modules.keys() {
to_watch.remove(&path);
}
for path in new_modules.keys() {
to_unwatch.remove(&path);
}
for path in to_watch {
watcher.watch(path, notify::RecursiveMode::NonRecursive)?;
}
for path in to_unwatch {
watcher.unwatch(path)?;
}
}
modules = new_modules;
}
Err(kind) => {
eprintln!("{}error: {}", if quiet_watch { "" } else { "\x07" }, kind);
}
}
}
} else {
bundle(&entry_point, input_options, &output, &map_output).map(|_| ())
}
}
const APP_NAME: &str = env!("CARGO_PKG_NAME");
const APP_VERSION: &str = env!("CARGO_PKG_VERSION");
fn write_usage(f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "\
Usage: {0} [options] <input> [output]
{0} [-h | --help | -v | --version]", APP_NAME)
}
fn write_version(f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{0} v{1}", APP_NAME, APP_VERSION)
}
fn write_help(f: &mut fmt::Formatter) -> fmt::Result {
write_version(f)?;
write!(f, "\n\n")?;
write_usage(f)?;
write!(f, "\n\n")?;
write!(f, "\
Options:
-i, --input <input>
Use <input> as the main module.
-o, --output <output>
Write bundle to <output> and source map to <output>.map.
Default: '-' for stdout.
-m, --map <map>
Output source map to <map>.
-I, --map-inline
Output source map inline as data: URI.
-M, --no-map
Suppress source map output when it would normally be implied.
-w, --watch
Watch for changes to <input> and its dependencies.
-W, --quiet-watch
Don't emit a bell character for errors that occur while watching.
Implies --watch.
-e, --es-syntax
Support .mjs files with ECMAScript module syntax:
import itt from 'itt'
export const greeting = 'Hello, world!'
Instead of CommonJS require syntax:
const itt = require('itt')
exports.greeting = 'Hello, world!'
.mjs (ESM) files can import .js (CJS) files, in which case the
namespace object has a single `default` binding which reflects the
value of `module.exports`. CJS files can require ESM files, in which
case the resultant object is the namespace object.
-E, --es-syntax-everywhere
Implies --es-syntax. Allow ECMAScript module syntax in .js files.
CJS-style `require()` calls are also allowed.
-h, --help
Print this message.
-v, --version
Print version information.
")
}
#[derive(Debug)]
pub enum CliError {
Help,
Version,
MissingFileName,
DuplicateOption(String),
MissingOptionValue(String),
UnknownOption(String),
UnexpectedArg(String),
BadUsage(&'static str),
RequireRoot { context: Option<PathBuf>, path: PathBuf },
EmptyModuleName { context: PathBuf },
ModuleNotFound { context: PathBuf, name: String },
MainNotFound { name: String },
Io(io::Error),
Json(json::Error),
Notify(notify::Error),
Es6(es6::Error),
Lex(lex::Error),
ParseStrLit(lex::ParseStrLitError),
Box(Box<Any + Send + 'static>),
}
impl From<io::Error> for CliError {
fn from(inner: io::Error) -> CliError {
CliError::Io(inner)
}
}
impl From<json::Error> for CliError {
fn from(inner: json::Error) -> CliError {
CliError::Json(inner)
}
}
impl From<notify::Error> for CliError {
fn from(inner: notify::Error) -> CliError {
CliError::Notify(inner)
}
}
impl From<es6::Error> for CliError {
fn from(inner: es6::Error) -> CliError {
CliError::Es6(inner)
}
}
impl From<lex::Error> for CliError {
fn from(inner: lex::Error) -> CliError {
CliError::Lex(inner)
}
}
impl From<lex::ParseStrLitError> for CliError {
fn from(inner: lex::ParseStrLitError) -> CliError {
CliError::ParseStrLit(inner)
}
}
impl From<Box<Any + Send + 'static>> for CliError {
fn from(inner: Box<Any + Send + 'static>) -> CliError {
CliError::Box(inner)
}
}
impl fmt::Display for CliError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
CliError::Help => {
write_help(f)
}
CliError::Version => {
write_version(f)
}
CliError::MissingFileName => {
write_usage(f)
}
CliError::DuplicateOption(ref opt) => {
write!(f, "option {} specified more than once", opt)
}
CliError::MissingOptionValue(ref opt) => {
write!(f, "missing value for option {}", opt)
}
CliError::UnknownOption(ref opt) => {
write!(f, "unknown option {}", opt)
}
CliError::UnexpectedArg(ref arg) => {
write!(f, "unexpected argument {}", arg)
}
CliError::BadUsage(ref arg) => {
write!(f, "{}", arg)
}
CliError::RequireRoot { ref context, ref path } => {
match *context {
None => {
write!(f,
"main module is root path {}",
path.display(),
)
}
Some(ref context) => {
write!(f,
"require of root path {} in {}",
path.display(),
context.display(),
)
}
}
}
CliError::EmptyModuleName { ref context } => {
write!(f, "require('') in {}", context.display())
}
CliError::ModuleNotFound { ref context, ref name } => {
write!(f,
"module '{}' not found in {}",
name,
context.display(),
)
}
CliError::MainNotFound { ref name } => {
write!(f, "main module '{}' not found", name)
}
CliError::Io(ref inner) => {
write!(f, "{}", inner)
}
CliError::Json(ref inner) => {
write!(f, "{}", inner)
}
CliError::Notify(ref inner) => {
write!(f, "{}", inner)
}
CliError::Es6(ref inner) => {
write!(f, "{}", inner)
}
CliError::Lex(ref inner) => {
write!(f, "{}", inner)
}
CliError::ParseStrLit(ref inner) => {
write!(f, "{}", inner)
}
CliError::Box(ref inner) => {
write!(f, "{:?}", inner)
}
}
}
}
fn main() {
process::exit(match run() {
Ok(_) => 0,
Err(kind) => {
match kind {
CliError::Help |
CliError::Version |
CliError::MissingFileName => {
println!("{}", kind);
}
_ => {
println!("{}: {}", APP_NAME, kind);
}
}
1
}
})
}
trait PathBufExt {
fn append_resolving<P: AsRef<Path> + ?Sized>(&mut self, more: &P);
}
impl PathBufExt for PathBuf {
fn append_resolving<P: AsRef<Path> + ?Sized>(&mut self, more: &P) {
for c in more.as_ref().components() {
match c {
Component::Prefix(prefix) => {
*self = PathBuf::from(prefix.as_os_str().to_owned());
}
Component::RootDir => {
self.push(path::MAIN_SEPARATOR.to_string());
}
Component::CurDir => {}