turbopack_ecmascript/
runtime_functions.rs

1use std::fmt::{Display, Formatter};
2
3use swc_core::{
4    atoms::atom,
5    ecma::ast::{Expr, MemberExpr, MemberProp},
6};
7use turbo_rcstr::rcstr;
8use turbopack_core::compile_time_info::FreeVarReference;
9
10pub struct TurbopackRuntimeFunctionShortcut {
11    pub shortcut: &'static str,
12    pub full: &'static str,
13}
14
15impl TurbopackRuntimeFunctionShortcut {
16    pub const fn new(full: &'static str, shortcut: &'static str) -> Self {
17        Self { full, shortcut }
18    }
19
20    pub fn bound(&self) -> String {
21        format!(
22            "__turbopack_context__.{}.bind(__turbopack_context__)",
23            self.shortcut
24        )
25    }
26}
27
28impl Display for TurbopackRuntimeFunctionShortcut {
29    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
30        f.write_str(self.full)
31    }
32}
33
34impl From<&TurbopackRuntimeFunctionShortcut> for FreeVarReference {
35    fn from(val: &TurbopackRuntimeFunctionShortcut) -> Self {
36        FreeVarReference::Member(rcstr!("__turbopack_context__"), val.shortcut.into())
37    }
38}
39
40impl From<&TurbopackRuntimeFunctionShortcut> for Expr {
41    fn from(val: &TurbopackRuntimeFunctionShortcut) -> Self {
42        Expr::Member(MemberExpr {
43            obj: Box::new(Expr::Ident(atom!("__turbopack_context__").into())),
44            prop: MemberProp::Ident(val.shortcut.into()),
45            ..Default::default()
46        })
47    }
48}
49
50impl<'l> From<&'l TurbopackRuntimeFunctionShortcut> for &'l str {
51    fn from(val: &TurbopackRuntimeFunctionShortcut) -> Self {
52        val.full
53    }
54}
55
56macro_rules! make_shortcut {
57    ($shortcut:expr) => {
58        const {
59            &TurbopackRuntimeFunctionShortcut::new(
60                concat!("__turbopack_context__.", $shortcut),
61                $shortcut,
62            )
63        }
64    };
65}
66
67pub const TURBOPACK_EXPORTS: &TurbopackRuntimeFunctionShortcut = make_shortcut!("e");
68pub const TURBOPACK_MODULE: &TurbopackRuntimeFunctionShortcut = make_shortcut!("m");
69pub const TURBOPACK_REQUIRE: &TurbopackRuntimeFunctionShortcut = make_shortcut!("r");
70pub const TURBOPACK_ASYNC_LOADER: &TurbopackRuntimeFunctionShortcut = make_shortcut!("A");
71pub const TURBOPACK_MODULE_CONTEXT: &TurbopackRuntimeFunctionShortcut = make_shortcut!("f");
72pub const TURBOPACK_IMPORT: &TurbopackRuntimeFunctionShortcut = make_shortcut!("i");
73pub const TURBOPACK_ESM: &TurbopackRuntimeFunctionShortcut = make_shortcut!("s");
74pub const TURBOPACK_EXPORT_VALUE: &TurbopackRuntimeFunctionShortcut = make_shortcut!("v");
75pub const TURBOPACK_EXPORT_NAMESPACE: &TurbopackRuntimeFunctionShortcut = make_shortcut!("n");
76pub const TURBOPACK_CACHE: &TurbopackRuntimeFunctionShortcut = make_shortcut!("c");
77pub const TURBOPACK_MODULES: &TurbopackRuntimeFunctionShortcut = make_shortcut!("M");
78pub const TURBOPACK_LOAD: &TurbopackRuntimeFunctionShortcut = make_shortcut!("l");
79pub const TURBOPACK_LOAD_BY_URL: &TurbopackRuntimeFunctionShortcut = make_shortcut!("L");
80pub const TURBOPACK_CLEAR_CHUNK_CACHE: &TurbopackRuntimeFunctionShortcut = make_shortcut!("C");
81pub const TURBOPACK_DYNAMIC: &TurbopackRuntimeFunctionShortcut = make_shortcut!("j");
82pub const TURBOPACK_RESOLVE_ABSOLUTE_PATH: &TurbopackRuntimeFunctionShortcut = make_shortcut!("P");
83pub const TURBOPACK_RELATIVE_URL: &TurbopackRuntimeFunctionShortcut = make_shortcut!("U");
84pub const TURBOPACK_RESOLVE_MODULE_ID_PATH: &TurbopackRuntimeFunctionShortcut = make_shortcut!("R");
85pub const TURBOPACK_WORKER_BLOB_URL: &TurbopackRuntimeFunctionShortcut = make_shortcut!("b");
86pub const TURBOPACK_ASYNC_MODULE: &TurbopackRuntimeFunctionShortcut = make_shortcut!("a");
87pub const TURBOPACK_EXTERNAL_REQUIRE: &TurbopackRuntimeFunctionShortcut = make_shortcut!("x");
88pub const TURBOPACK_EXTERNAL_IMPORT: &TurbopackRuntimeFunctionShortcut = make_shortcut!("y");
89pub const TURBOPACK_REFRESH: &TurbopackRuntimeFunctionShortcut = make_shortcut!("k");
90pub const TURBOPACK_REQUIRE_STUB: &TurbopackRuntimeFunctionShortcut = make_shortcut!("z");
91pub const TURBOPACK_REQUIRE_REAL: &TurbopackRuntimeFunctionShortcut = make_shortcut!("t");
92pub const TURBOPACK_WASM: &TurbopackRuntimeFunctionShortcut = make_shortcut!("w");
93pub const TURBOPACK_WASM_MODULE: &TurbopackRuntimeFunctionShortcut = make_shortcut!("u");
94pub const TURBOPACK_GLOBAL: &TurbopackRuntimeFunctionShortcut = make_shortcut!("g");
95
96/// Adding an entry to this list will automatically ensure that `__turbopack_XXX__` can be called
97/// from user code (by inserting a replacement into free_var_references)
98pub const TURBOPACK_RUNTIME_FUNCTION_SHORTCUTS: [(&str, &TurbopackRuntimeFunctionShortcut); 22] = [
99    ("__turbopack_require__", TURBOPACK_REQUIRE),
100    ("__turbopack_module_context__", TURBOPACK_MODULE_CONTEXT),
101    ("__turbopack_import__", TURBOPACK_IMPORT),
102    ("__turbopack_export_value__", TURBOPACK_EXPORT_VALUE),
103    ("__turbopack_export_namespace__", TURBOPACK_EXPORT_NAMESPACE),
104    ("__turbopack_cache__", TURBOPACK_CACHE),
105    ("__turbopack_modules__", TURBOPACK_MODULES),
106    ("__turbopack_load__", TURBOPACK_LOAD),
107    ("__turbopack_load_by_url__", TURBOPACK_LOAD_BY_URL),
108    ("__turbopack_dynamic__", TURBOPACK_DYNAMIC),
109    (
110        "__turbopack_resolve_absolute_path__",
111        TURBOPACK_RESOLVE_ABSOLUTE_PATH,
112    ),
113    ("__turbopack_relative_url__", TURBOPACK_RELATIVE_URL),
114    (
115        "__turbopack_resolve_module_id_path__",
116        TURBOPACK_RESOLVE_MODULE_ID_PATH,
117    ),
118    ("__turbopack_worker_blob_url__", TURBOPACK_WORKER_BLOB_URL),
119    ("__turbopack_external_require__", TURBOPACK_EXTERNAL_REQUIRE),
120    ("__turbopack_external_import__", TURBOPACK_EXTERNAL_IMPORT),
121    ("__turbopack_refresh__", TURBOPACK_REFRESH),
122    ("__turbopack_require_stub__", TURBOPACK_REQUIRE_STUB),
123    ("__turbopack_require_real__", TURBOPACK_REQUIRE_REAL),
124    (
125        "__turbopack_clear_chunk_cache__",
126        TURBOPACK_CLEAR_CHUNK_CACHE,
127    ),
128    ("__turbopack_wasm__", TURBOPACK_WASM),
129    ("__turbopack_wasm_module__", TURBOPACK_WASM_MODULE),
130];