next_swc_napi/
lib.rs

1/*
2Copyright (c) 2017 The swc Project Developers
3
4Permission is hereby granted, free of charge, to any
5person obtaining a copy of this software and associated
6documentation files (the "Software"), to deal in the
7Software without restriction, including without
8limitation the rights to use, copy, modify, merge,
9publish, distribute, sublicense, and/or sell copies of
10the Software, and to permit persons to whom the Software
11is furnished to do so, subject to the following
12conditions:
13
14The above copyright notice and this permission notice
15shall be included in all copies or substantial portions
16of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
19ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
20TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
21PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
22SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
25IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26DEALINGS IN THE SOFTWARE.
27*/
28
29#![recursion_limit = "2048"]
30//#![deny(clippy::all)]
31#![feature(arbitrary_self_types)]
32#![feature(arbitrary_self_types_pointers)]
33
34#[macro_use]
35extern crate napi_derive;
36
37use std::sync::{Arc, Once};
38
39use napi::bindgen_prelude::*;
40use rustc_hash::{FxHashMap, FxHashSet};
41use swc_core::{
42    atoms::Atom,
43    base::{Compiler, TransformOutput},
44    common::{FilePathMapping, SourceMap},
45};
46#[cfg(not(target_arch = "wasm32"))]
47pub mod css;
48pub mod mdx;
49pub mod minify;
50#[cfg(not(target_arch = "wasm32"))]
51pub mod next_api;
52pub mod parse;
53pub mod react_compiler;
54pub mod transform;
55#[cfg(not(target_arch = "wasm32"))]
56pub mod turbo_trace_server;
57#[cfg(not(target_arch = "wasm32"))]
58pub mod turbopack;
59pub mod util;
60
61#[cfg(not(any(feature = "__internal_dhat-heap", feature = "__internal_dhat-ad-hoc")))]
62#[global_allocator]
63static ALLOC: turbo_tasks_malloc::TurboMalloc = turbo_tasks_malloc::TurboMalloc;
64
65#[cfg(feature = "__internal_dhat-heap")]
66#[global_allocator]
67static ALLOC: dhat::Alloc = dhat::Alloc;
68
69#[cfg(not(target_arch = "wasm32"))]
70#[napi::module_init]
71fn init() {
72    use std::panic::{set_hook, take_hook};
73
74    use tokio::runtime::Builder;
75    use turbo_tasks::handle_panic;
76    use turbo_tasks_malloc::TurboMalloc;
77
78    let prev_hook = take_hook();
79    set_hook(Box::new(move |info| {
80        handle_panic(info);
81        prev_hook(info);
82    }));
83
84    let rt = Builder::new_multi_thread()
85        .enable_all()
86        .on_thread_stop(|| {
87            TurboMalloc::thread_stop();
88        })
89        .disable_lifo_slot()
90        .build()
91        .unwrap();
92    create_custom_tokio_runtime(rt);
93}
94
95#[inline]
96fn get_compiler() -> Compiler {
97    let cm = Arc::new(SourceMap::new(FilePathMapping::empty()));
98
99    Compiler::new(cm)
100}
101
102pub fn complete_output(
103    env: &Env,
104    output: TransformOutput,
105    eliminated_packages: FxHashSet<Atom>,
106    use_cache_telemetry_tracker: FxHashMap<String, usize>,
107) -> napi::Result<Object> {
108    let mut js_output = env.create_object()?;
109    js_output.set_named_property("code", env.create_string_from_std(output.code)?)?;
110    if let Some(map) = output.map {
111        js_output.set_named_property("map", env.create_string_from_std(map)?)?;
112    }
113    if !eliminated_packages.is_empty() {
114        js_output.set_named_property(
115            "eliminatedPackages",
116            env.create_string_from_std(serde_json::to_string(&eliminated_packages)?)?,
117        )?;
118    }
119    if !use_cache_telemetry_tracker.is_empty() {
120        js_output.set_named_property(
121            "useCacheTelemetryTracker",
122            env.create_string_from_std(serde_json::to_string(
123                &use_cache_telemetry_tracker
124                    .iter()
125                    .map(|(k, v)| (k.clone(), *v))
126                    .collect::<Vec<_>>(),
127            )?)?,
128        )?;
129    }
130
131    Ok(js_output)
132}
133
134static REGISTER_ONCE: Once = Once::new();
135
136#[cfg(not(target_arch = "wasm32"))]
137fn register() {
138    REGISTER_ONCE.call_once(|| {
139        ::next_api::register();
140        next_core::register();
141        include!(concat!(env!("OUT_DIR"), "/register.rs"));
142    });
143}
144
145#[cfg(target_arch = "wasm32")]
146fn register() {
147    //noop
148}