forked from vercel/next.js
/
telemetry.rs
66 lines (58 loc) · 1.78 KB
/
telemetry.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
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::Arc;
use fxhash::FxHashSet;
use next_swc::next_ssg::next_ssg;
use once_cell::sync::Lazy;
use swc_core::{
base::{try_with_handler, Compiler},
common::{comments::SingleThreadedComments, FileName, FilePathMapping, SourceMap, GLOBALS},
ecma::transforms::base::pass::noop,
};
static COMPILER: Lazy<Arc<Compiler>> = Lazy::new(|| {
let cm = Arc::new(SourceMap::new(FilePathMapping::empty()));
Arc::new(Compiler::new(cm))
});
#[test]
fn should_collect_estimated_third_part_packages() {
let eliminated_packages: Rc<RefCell<FxHashSet<String>>> = Default::default();
let fm = COMPILER.cm.new_source_file(
FileName::Real("fixture.js".into()),
r#"import http from 'http'
import { hash } from '@napi-rs/bcrypt'
import { omit } from '~/utils/omit'
import config from './data.json'
export default () => 'Hello World'
export function getServerSideProps() {
console.log(http)
console.log(config)
return { props: { digest: hash('hello') } }
}
"#
.to_owned(),
);
assert!(
try_with_handler(COMPILER.cm.clone(), Default::default(), |handler| {
GLOBALS.set(&Default::default(), || {
let comments = SingleThreadedComments::default();
COMPILER.process_js_with_custom_pass(
fm,
None,
handler,
&Default::default(),
comments,
|_| next_ssg(eliminated_packages.clone()),
|_| noop(),
)
})
})
.is_ok()
);
assert_eq!(
eliminated_packages
.borrow()
.iter()
.collect::<Vec<&String>>(),
vec!["@napi-rs/bcrypt", "http"]
);
}