pub struct Compiler {
pub cm: Arc<SourceMap>,
/* private fields */
}
Expand description
All methods accept Handler, which is a storage for errors.
The caller should check if the handler contains any errors after calling method.
Fields§
§cm: Arc<SourceMap>
CodeMap
Implementations§
Source§impl Compiler
impl Compiler
These are low-level apis.
pub fn comments(&self) -> &SwcComments
Sourcepub fn run<R, F>(&self, op: F) -> Rwhere
F: FnOnce() -> R,
pub fn run<R, F>(&self, op: F) -> Rwhere
F: FnOnce() -> R,
Runs op
in current compiler’s context.
Note: Other methods of Compiler
already uses this internally.
Sourcepub fn parse_js(
&self,
fm: Arc<SourceFile>,
handler: &Handler,
target: EsVersion,
syntax: Syntax,
is_module: IsModule,
comments: Option<&dyn Comments>,
) -> Result<Program, Error>
pub fn parse_js( &self, fm: Arc<SourceFile>, handler: &Handler, target: EsVersion, syntax: Syntax, is_module: IsModule, comments: Option<&dyn Comments>, ) -> Result<Program, Error>
This method parses a javascript / typescript file
Sourcepub fn print<T>(
&self,
node: &T,
source_file_name: Option<&str>,
output_path: Option<PathBuf>,
inline_sources_content: bool,
target: EsVersion,
source_map: SourceMapsConfig,
source_map_names: &AHashMap<BytePos, JsWord>,
orig: Option<&SourceMap>,
minify: bool,
comments: Option<&dyn Comments>,
emit_source_map_columns: bool,
ascii_only: bool,
) -> Result<TransformOutput, Error>
pub fn print<T>( &self, node: &T, source_file_name: Option<&str>, output_path: Option<PathBuf>, inline_sources_content: bool, target: EsVersion, source_map: SourceMapsConfig, source_map_names: &AHashMap<BytePos, JsWord>, orig: Option<&SourceMap>, minify: bool, comments: Option<&dyn Comments>, emit_source_map_columns: bool, ascii_only: bool, ) -> Result<TransformOutput, Error>
Converts ast node to source string and sourcemap.
This method receives target file path, but does not write file to the path. See: https://github.com/swc-project/swc/issues/1255
Source§impl Compiler
impl Compiler
High-level apis.
pub fn new(cm: Arc<SourceMap>) -> Self
pub fn read_config( &self, opts: &Options, name: &FileName, ) -> Result<Option<Config>, Error>
Sourcepub fn parse_js_as_input<'a, P>(
&'a self,
fm: Lrc<SourceFile>,
program: Option<Program>,
handler: &'a Handler,
opts: &Options,
name: &FileName,
comments: Option<&'a SingleThreadedComments>,
before_pass: impl 'a + FnOnce(&Program) -> P,
) -> Result<Option<BuiltInput<impl 'a + Fold>>, Error>where
P: 'a + Fold,
pub fn parse_js_as_input<'a, P>(
&'a self,
fm: Lrc<SourceFile>,
program: Option<Program>,
handler: &'a Handler,
opts: &Options,
name: &FileName,
comments: Option<&'a SingleThreadedComments>,
before_pass: impl 'a + FnOnce(&Program) -> P,
) -> Result<Option<BuiltInput<impl 'a + Fold>>, Error>where
P: 'a + Fold,
This method returns None if a file should be skipped.
This method handles merging of config.
This method does not parse module.
pub fn run_transform<F, Ret>(
&self,
handler: &Handler,
external_helpers: bool,
op: F,
) -> Retwhere
F: FnOnce() -> Ret,
pub fn transform( &self, handler: &Handler, program: Program, external_helpers: bool, pass: impl Fold, ) -> Program
Sourcepub fn process_js_with_custom_pass<P1, P2>(
&self,
fm: Arc<SourceFile>,
program: Option<Program>,
handler: &Handler,
opts: &Options,
comments: SingleThreadedComments,
custom_before_pass: impl FnOnce(&Program) -> P1,
custom_after_pass: impl FnOnce(&Program) -> P2,
) -> Result<TransformOutput, Error>
pub fn process_js_with_custom_pass<P1, P2>( &self, fm: Arc<SourceFile>, program: Option<Program>, handler: &Handler, opts: &Options, comments: SingleThreadedComments, custom_before_pass: impl FnOnce(&Program) -> P1, custom_after_pass: impl FnOnce(&Program) -> P2, ) -> Result<TransformOutput, Error>
custom_after_pass
is applied after swc transforms are applied.
program
: If you already parsed Program
, you can pass it.
§Guarantee
swc
invokes custom_before_pass
after
- Handling decorators, if configured
- Applying
resolver
- Stripping typescript nodes
This means, you can use noop_visit_type
, noop_fold_type
and
noop_visit_mut_type
in your visitor to reduce the binary size.
pub fn process_js_file( &self, fm: Arc<SourceFile>, handler: &Handler, opts: &Options, ) -> Result<TransformOutput, Error>
pub fn minify( &self, fm: Arc<SourceFile>, handler: &Handler, opts: &JsMinifyOptions, ) -> Result<TransformOutput, Error>
Sourcepub fn process_js(
&self,
handler: &Handler,
program: Program,
opts: &Options,
) -> Result<TransformOutput, Error>
pub fn process_js( &self, handler: &Handler, program: Program, opts: &Options, ) -> Result<TransformOutput, Error>
You can use custom pass with this method.
There exists a PassBuilder to help building custom passes.
Auto Trait Implementations§
impl Freeze for Compiler
impl !RefUnwindSafe for Compiler
impl Send for Compiler
impl Sync for Compiler
impl Unpin for Compiler
impl !UnwindSafe for Compiler
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§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 moreSource§impl<D> OwoColorize for D
impl<D> OwoColorize for D
Source§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
Source§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
Source§fn black<'a>(&'a self) -> FgColorDisplay<'a, Black, Self>
fn black<'a>(&'a self) -> FgColorDisplay<'a, Black, Self>
Source§fn on_black<'a>(&'a self) -> BgColorDisplay<'a, Black, Self>
fn on_black<'a>(&'a self) -> BgColorDisplay<'a, Black, Self>
Source§fn red<'a>(&'a self) -> FgColorDisplay<'a, Red, Self>
fn red<'a>(&'a self) -> FgColorDisplay<'a, Red, Self>
Source§fn on_red<'a>(&'a self) -> BgColorDisplay<'a, Red, Self>
fn on_red<'a>(&'a self) -> BgColorDisplay<'a, Red, Self>
Source§fn green<'a>(&'a self) -> FgColorDisplay<'a, Green, Self>
fn green<'a>(&'a self) -> FgColorDisplay<'a, Green, Self>
Source§fn on_green<'a>(&'a self) -> BgColorDisplay<'a, Green, Self>
fn on_green<'a>(&'a self) -> BgColorDisplay<'a, Green, Self>
Source§fn yellow<'a>(&'a self) -> FgColorDisplay<'a, Yellow, Self>
fn yellow<'a>(&'a self) -> FgColorDisplay<'a, Yellow, Self>
Source§fn on_yellow<'a>(&'a self) -> BgColorDisplay<'a, Yellow, Self>
fn on_yellow<'a>(&'a self) -> BgColorDisplay<'a, Yellow, Self>
Source§fn blue<'a>(&'a self) -> FgColorDisplay<'a, Blue, Self>
fn blue<'a>(&'a self) -> FgColorDisplay<'a, Blue, Self>
Source§fn on_blue<'a>(&'a self) -> BgColorDisplay<'a, Blue, Self>
fn on_blue<'a>(&'a self) -> BgColorDisplay<'a, Blue, Self>
Source§fn magenta<'a>(&'a self) -> FgColorDisplay<'a, Magenta, Self>
fn magenta<'a>(&'a self) -> FgColorDisplay<'a, Magenta, Self>
Source§fn on_magenta<'a>(&'a self) -> BgColorDisplay<'a, Magenta, Self>
fn on_magenta<'a>(&'a self) -> BgColorDisplay<'a, Magenta, Self>
Source§fn purple<'a>(&'a self) -> FgColorDisplay<'a, Magenta, Self>
fn purple<'a>(&'a self) -> FgColorDisplay<'a, Magenta, Self>
Source§fn on_purple<'a>(&'a self) -> BgColorDisplay<'a, Magenta, Self>
fn on_purple<'a>(&'a self) -> BgColorDisplay<'a, Magenta, Self>
Source§fn cyan<'a>(&'a self) -> FgColorDisplay<'a, Cyan, Self>
fn cyan<'a>(&'a self) -> FgColorDisplay<'a, Cyan, Self>
Source§fn on_cyan<'a>(&'a self) -> BgColorDisplay<'a, Cyan, Self>
fn on_cyan<'a>(&'a self) -> BgColorDisplay<'a, Cyan, Self>
Source§fn white<'a>(&'a self) -> FgColorDisplay<'a, White, Self>
fn white<'a>(&'a self) -> FgColorDisplay<'a, White, Self>
Source§fn on_white<'a>(&'a self) -> BgColorDisplay<'a, White, Self>
fn on_white<'a>(&'a self) -> BgColorDisplay<'a, White, Self>
Source§fn default_color<'a>(&'a self) -> FgColorDisplay<'a, Default, Self>
fn default_color<'a>(&'a self) -> FgColorDisplay<'a, Default, Self>
Source§fn on_default_color<'a>(&'a self) -> BgColorDisplay<'a, Default, Self>
fn on_default_color<'a>(&'a self) -> BgColorDisplay<'a, Default, Self>
Source§fn bright_black<'a>(&'a self) -> FgColorDisplay<'a, BrightBlack, Self>
fn bright_black<'a>(&'a self) -> FgColorDisplay<'a, BrightBlack, Self>
Source§fn on_bright_black<'a>(&'a self) -> BgColorDisplay<'a, BrightBlack, Self>
fn on_bright_black<'a>(&'a self) -> BgColorDisplay<'a, BrightBlack, Self>
Source§fn bright_red<'a>(&'a self) -> FgColorDisplay<'a, BrightRed, Self>
fn bright_red<'a>(&'a self) -> FgColorDisplay<'a, BrightRed, Self>
Source§fn on_bright_red<'a>(&'a self) -> BgColorDisplay<'a, BrightRed, Self>
fn on_bright_red<'a>(&'a self) -> BgColorDisplay<'a, BrightRed, Self>
Source§fn bright_green<'a>(&'a self) -> FgColorDisplay<'a, BrightGreen, Self>
fn bright_green<'a>(&'a self) -> FgColorDisplay<'a, BrightGreen, Self>
Source§fn on_bright_green<'a>(&'a self) -> BgColorDisplay<'a, BrightGreen, Self>
fn on_bright_green<'a>(&'a self) -> BgColorDisplay<'a, BrightGreen, Self>
Source§fn bright_yellow<'a>(&'a self) -> FgColorDisplay<'a, BrightYellow, Self>
fn bright_yellow<'a>(&'a self) -> FgColorDisplay<'a, BrightYellow, Self>
Source§fn on_bright_yellow<'a>(&'a self) -> BgColorDisplay<'a, BrightYellow, Self>
fn on_bright_yellow<'a>(&'a self) -> BgColorDisplay<'a, BrightYellow, Self>
Source§fn bright_blue<'a>(&'a self) -> FgColorDisplay<'a, BrightBlue, Self>
fn bright_blue<'a>(&'a self) -> FgColorDisplay<'a, BrightBlue, Self>
Source§fn on_bright_blue<'a>(&'a self) -> BgColorDisplay<'a, BrightBlue, Self>
fn on_bright_blue<'a>(&'a self) -> BgColorDisplay<'a, BrightBlue, Self>
Source§fn bright_magenta<'a>(&'a self) -> FgColorDisplay<'a, BrightMagenta, Self>
fn bright_magenta<'a>(&'a self) -> FgColorDisplay<'a, BrightMagenta, Self>
Source§fn on_bright_magenta<'a>(&'a self) -> BgColorDisplay<'a, BrightMagenta, Self>
fn on_bright_magenta<'a>(&'a self) -> BgColorDisplay<'a, BrightMagenta, Self>
Source§fn bright_purple<'a>(&'a self) -> FgColorDisplay<'a, BrightMagenta, Self>
fn bright_purple<'a>(&'a self) -> FgColorDisplay<'a, BrightMagenta, Self>
Source§fn on_bright_purple<'a>(&'a self) -> BgColorDisplay<'a, BrightMagenta, Self>
fn on_bright_purple<'a>(&'a self) -> BgColorDisplay<'a, BrightMagenta, Self>
Source§fn bright_cyan<'a>(&'a self) -> FgColorDisplay<'a, BrightCyan, Self>
fn bright_cyan<'a>(&'a self) -> FgColorDisplay<'a, BrightCyan, Self>
Source§fn on_bright_cyan<'a>(&'a self) -> BgColorDisplay<'a, BrightCyan, Self>
fn on_bright_cyan<'a>(&'a self) -> BgColorDisplay<'a, BrightCyan, Self>
Source§fn bright_white<'a>(&'a self) -> FgColorDisplay<'a, BrightWhite, Self>
fn bright_white<'a>(&'a self) -> FgColorDisplay<'a, BrightWhite, Self>
Source§fn on_bright_white<'a>(&'a self) -> BgColorDisplay<'a, BrightWhite, Self>
fn on_bright_white<'a>(&'a self) -> BgColorDisplay<'a, BrightWhite, Self>
Source§fn bold<'a>(&'a self) -> BoldDisplay<'a, Self>
fn bold<'a>(&'a self) -> BoldDisplay<'a, Self>
Source§fn dimmed<'a>(&'a self) -> DimDisplay<'a, Self>
fn dimmed<'a>(&'a self) -> DimDisplay<'a, Self>
Source§fn italic<'a>(&'a self) -> ItalicDisplay<'a, Self>
fn italic<'a>(&'a self) -> ItalicDisplay<'a, Self>
Source§fn underline<'a>(&'a self) -> UnderlineDisplay<'a, Self>
fn underline<'a>(&'a self) -> UnderlineDisplay<'a, Self>
Source§fn blink<'a>(&'a self) -> BlinkDisplay<'a, Self>
fn blink<'a>(&'a self) -> BlinkDisplay<'a, Self>
Source§fn blink_fast<'a>(&'a self) -> BlinkFastDisplay<'a, Self>
fn blink_fast<'a>(&'a self) -> BlinkFastDisplay<'a, Self>
Source§fn reversed<'a>(&'a self) -> ReversedDisplay<'a, Self>
fn reversed<'a>(&'a self) -> ReversedDisplay<'a, Self>
Source§fn strikethrough<'a>(&'a self) -> StrikeThroughDisplay<'a, Self>
fn strikethrough<'a>(&'a self) -> StrikeThroughDisplay<'a, Self>
Source§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg
or
a color-specific method, such as OwoColorize::green
, Read moreSource§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg
or
a color-specific method, such as OwoColorize::on_yellow
, Read more