turbopack_ecmascript/
runtime_functions.rs1use 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_URL: &TurbopackRuntimeFunctionShortcut = make_shortcut!("q");
76pub const TURBOPACK_EXPORT_NAMESPACE: &TurbopackRuntimeFunctionShortcut = make_shortcut!("n");
77pub const TURBOPACK_CACHE: &TurbopackRuntimeFunctionShortcut = make_shortcut!("c");
78pub const TURBOPACK_MODULES: &TurbopackRuntimeFunctionShortcut = make_shortcut!("M");
79pub const TURBOPACK_LOAD: &TurbopackRuntimeFunctionShortcut = make_shortcut!("l");
80pub const TURBOPACK_LOAD_BY_URL: &TurbopackRuntimeFunctionShortcut = make_shortcut!("L");
81pub const TURBOPACK_CLEAR_CHUNK_CACHE: &TurbopackRuntimeFunctionShortcut = make_shortcut!("C");
82pub const TURBOPACK_DYNAMIC: &TurbopackRuntimeFunctionShortcut = make_shortcut!("j");
83pub const TURBOPACK_RESOLVE_ABSOLUTE_PATH: &TurbopackRuntimeFunctionShortcut = make_shortcut!("P");
84pub const TURBOPACK_RELATIVE_URL: &TurbopackRuntimeFunctionShortcut = make_shortcut!("U");
85pub const TURBOPACK_RESOLVE_MODULE_ID_PATH: &TurbopackRuntimeFunctionShortcut = make_shortcut!("R");
86pub const TURBOPACK_CREATE_WORKER: &TurbopackRuntimeFunctionShortcut = make_shortcut!("b");
87pub const TURBOPACK_ASYNC_MODULE: &TurbopackRuntimeFunctionShortcut = make_shortcut!("a");
88pub const TURBOPACK_EXTERNAL_REQUIRE: &TurbopackRuntimeFunctionShortcut = make_shortcut!("x");
89pub const TURBOPACK_EXTERNAL_IMPORT: &TurbopackRuntimeFunctionShortcut = make_shortcut!("y");
90pub const TURBOPACK_REFRESH: &TurbopackRuntimeFunctionShortcut = make_shortcut!("k");
91pub const TURBOPACK_REQUIRE_STUB: &TurbopackRuntimeFunctionShortcut = make_shortcut!("z");
92pub const TURBOPACK_REQUIRE_REAL: &TurbopackRuntimeFunctionShortcut = make_shortcut!("t");
93pub const TURBOPACK_WASM: &TurbopackRuntimeFunctionShortcut = make_shortcut!("w");
94pub const TURBOPACK_WASM_MODULE: &TurbopackRuntimeFunctionShortcut = make_shortcut!("u");
95pub const TURBOPACK_GLOBAL: &TurbopackRuntimeFunctionShortcut = make_shortcut!("g");
96
97pub const TURBOPACK_RUNTIME_FUNCTION_SHORTCUTS: [(&str, &TurbopackRuntimeFunctionShortcut); 23] = [
100 ("__turbopack_require__", TURBOPACK_REQUIRE),
101 ("__turbopack_module_context__", TURBOPACK_MODULE_CONTEXT),
102 ("__turbopack_import__", TURBOPACK_IMPORT),
103 ("__turbopack_export_value__", TURBOPACK_EXPORT_VALUE),
104 ("__turbopack_export_url__", TURBOPACK_EXPORT_URL),
105 ("__turbopack_export_namespace__", TURBOPACK_EXPORT_NAMESPACE),
106 ("__turbopack_cache__", TURBOPACK_CACHE),
107 ("__turbopack_modules__", TURBOPACK_MODULES),
108 ("__turbopack_load__", TURBOPACK_LOAD),
109 ("__turbopack_load_by_url__", TURBOPACK_LOAD_BY_URL),
110 ("__turbopack_dynamic__", TURBOPACK_DYNAMIC),
111 (
112 "__turbopack_resolve_absolute_path__",
113 TURBOPACK_RESOLVE_ABSOLUTE_PATH,
114 ),
115 ("__turbopack_relative_url__", TURBOPACK_RELATIVE_URL),
116 (
117 "__turbopack_resolve_module_id_path__",
118 TURBOPACK_RESOLVE_MODULE_ID_PATH,
119 ),
120 ("__turbopack_create_worker__", TURBOPACK_CREATE_WORKER),
121 ("__turbopack_external_require__", TURBOPACK_EXTERNAL_REQUIRE),
122 ("__turbopack_external_import__", TURBOPACK_EXTERNAL_IMPORT),
123 ("__turbopack_refresh__", TURBOPACK_REFRESH),
124 ("__turbopack_require_stub__", TURBOPACK_REQUIRE_STUB),
125 ("__turbopack_require_real__", TURBOPACK_REQUIRE_REAL),
126 (
127 "__turbopack_clear_chunk_cache__",
128 TURBOPACK_CLEAR_CHUNK_CACHE,
129 ),
130 ("__turbopack_wasm__", TURBOPACK_WASM),
131 ("__turbopack_wasm_module__", TURBOPACK_WASM_MODULE),
132];