turbopack_ecmascript/
annotations.rs

1use swc_core::{
2    common::DUMMY_SP,
3    ecma::ast::{Expr, KeyValueProp, ObjectLit, Prop, PropName, PropOrSpread},
4};
5
6/// Changes the chunking type for the annotated import
7pub const ANNOTATION_CHUNKING_TYPE: &str = "turbopack-chunking-type";
8
9/// Enables a specified transition for the annotated import
10pub const ANNOTATION_TRANSITION: &str = "turbopack-transition";
11
12pub fn with_chunking_type(chunking_type: &str) -> Box<ObjectLit> {
13    with_clause(&[(ANNOTATION_CHUNKING_TYPE, chunking_type)])
14}
15
16pub fn with_transition(transition_name: &str) -> Box<ObjectLit> {
17    with_clause(&[(ANNOTATION_TRANSITION, transition_name)])
18}
19
20pub fn with_clause<'a>(
21    entries: impl IntoIterator<Item = &'a (&'a str, &'a str)>,
22) -> Box<ObjectLit> {
23    Box::new(ObjectLit {
24        span: DUMMY_SP,
25        props: entries.into_iter().map(|(k, v)| with_prop(k, v)).collect(),
26    })
27}
28
29fn with_prop(key: &str, value: &str) -> PropOrSpread {
30    PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
31        key: PropName::Str(key.into()),
32        value: Box::new(Expr::Lit(value.into())),
33    })))
34}