pub struct NativePlugin { /* private fields */ }Expand description
The main plugin class for macro expansion with caching support.
NativePlugin is designed to be instantiated once and reused across multiple
file processing operations. It maintains a cache of expansion results keyed
by filepath and version, enabling efficient incremental processing.
§Thread Safety
The plugin is thread-safe through the use of Mutex for internal state.
However, macro expansion itself runs in a separate thread with a 32MB stack
to prevent stack overflow during deep AST recursion.
§Example
// Create a single plugin instance (typically at startup)
const plugin = new NativePlugin();
// Process files with caching
const result1 = plugin.process_file("src/foo.ts", code1, { version: "1" });
const result2 = plugin.process_file("src/foo.ts", code2, { version: "1" }); // Cache hit!
const result3 = plugin.process_file("src/foo.ts", code3, { version: "2" }); // Cache miss
// Get a mapper for position translation
const mapper = plugin.get_mapper("src/foo.ts");Implementations§
Source§impl NativePlugin
impl NativePlugin
Sourcepub fn set_log_file(&self, path: String)
pub fn set_log_file(&self, path: String)
Sourcepub fn process_file(
&self,
_env: Env,
filepath: String,
code: String,
options: Option<ProcessFileOptions>,
) -> Result<ExpandResult>
pub fn process_file( &self, _env: Env, filepath: String, code: String, options: Option<ProcessFileOptions>, ) -> Result<ExpandResult>
Processes a TypeScript file through the macro expansion system.
This is the main entry point for file processing. It handles caching, thread isolation (to prevent stack overflow), and error recovery.
§Arguments
_env- NAPI environment (unused but required by NAPI)filepath- Path to the file (used for TSX detection and caching)code- The TypeScript source code to processoptions- Optional configuration for expansion and caching
§Returns
An ExpandResult containing the expanded code, diagnostics, and source mapping.
§Errors
Returns an error if:
- Thread spawning fails
- The worker thread panics (often due to stack overflow)
- Macro expansion fails internally
§Performance
- Uses a 32MB thread stack to prevent stack overflow during deep AST recursion
- Caches results by filepath and version for efficient incremental processing
- Early bailout for files without
@derivedecorators
§Thread Safety
Macro expansion runs in a separate thread because:
- SWC AST operations can be deeply recursive, exceeding default stack limits
- Node.js thread stack is typically only 2MB
- Panics in the worker thread are caught and reported gracefully
Sourcepub fn get_mapper(&self, filepath: String) -> Option<NativeMapper>
pub fn get_mapper(&self, filepath: String) -> Option<NativeMapper>
Retrieves a position mapper for a previously processed file.
The mapper enables translation between original and expanded source positions, which is essential for IDE features like error reporting and navigation.
§Arguments
filepath- Path to the file (must have been previously processed)
§Returns
Some(NativeMapper) if the file has been processed and has source mapping data,
None if the file hasn’t been processed or has no mapping (no macros expanded).
Sourcepub fn map_diagnostics(
&self,
filepath: String,
diags: Vec<JsDiagnostic>,
) -> Vec<JsDiagnostic>
pub fn map_diagnostics( &self, filepath: String, diags: Vec<JsDiagnostic>, ) -> Vec<JsDiagnostic>
Maps diagnostics from expanded source positions back to original source positions.
This is used by IDE integrations to show errors at the correct locations in the user’s original code, rather than in the macro-expanded output.
§Arguments
filepath- Path to the file the diagnostics are fordiags- Diagnostics with positions in the expanded source
§Returns
Diagnostics with positions mapped back to the original source. If no mapper is available for the file, returns diagnostics unchanged.
Trait Implementations§
Source§impl Default for NativePlugin
impl Default for NativePlugin
Source§impl FromNapiMutRef for NativePlugin
impl FromNapiMutRef for NativePlugin
Source§unsafe fn from_napi_mut_ref(
env: napi_env,
napi_val: napi_value,
) -> Result<&'static mut Self>
unsafe fn from_napi_mut_ref( env: napi_env, napi_val: napi_value, ) -> Result<&'static mut Self>
Source§impl FromNapiRef for NativePlugin
impl FromNapiRef for NativePlugin
Source§unsafe fn from_napi_ref(
env: napi_env,
napi_val: napi_value,
) -> Result<&'static Self>
unsafe fn from_napi_ref( env: napi_env, napi_val: napi_value, ) -> Result<&'static Self>
Source§impl JavaScriptClassExt for NativePlugin
impl JavaScriptClassExt for NativePlugin
fn into_instance<'scope>( self, env: &'scope Env, ) -> Result<ClassInstance<'scope, Self>>
fn into_reference(self, env: Env) -> Result<Reference<Self>>
fn instance_of<'env, V: JsValue<'env>>(env: &Env, value: &V) -> Result<bool>
Source§impl ToNapiValue for NativePlugin
impl ToNapiValue for NativePlugin
Source§unsafe fn to_napi_value(env: napi_env, val: NativePlugin) -> Result<napi_value>
unsafe fn to_napi_value(env: napi_env, val: NativePlugin) -> Result<napi_value>
fn into_unknown(self, env: &Env) -> Result<Unknown<'_>, Error>
Source§impl TypeName for &NativePlugin
impl TypeName for &NativePlugin
Source§impl TypeName for &mut NativePlugin
impl TypeName for &mut NativePlugin
Source§impl TypeName for NativePlugin
impl TypeName for NativePlugin
Source§impl ValidateNapiValue for &NativePlugin
impl ValidateNapiValue for &NativePlugin
Source§unsafe fn validate(env: napi_env, napi_val: napi_value) -> Result<napi_value>
unsafe fn validate(env: napi_env, napi_val: napi_value) -> Result<napi_value>
Source§impl ValidateNapiValue for &mut NativePlugin
impl ValidateNapiValue for &mut NativePlugin
Source§unsafe fn validate(env: napi_env, napi_val: napi_value) -> Result<napi_value>
unsafe fn validate(env: napi_env, napi_val: napi_value) -> Result<napi_value>
Auto Trait Implementations§
impl !Freeze for NativePlugin
impl RefUnwindSafe for NativePlugin
impl Send for NativePlugin
impl Sync for NativePlugin
impl Unpin for NativePlugin
impl UnwindSafe for NativePlugin
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more