From 99981f70c7ab6e0a2c28a9e4eda75951c2836906 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Thu, 27 Aug 2020 20:35:28 +0900 Subject: [PATCH] Add customizable api (#1006) --- ecmascript/transforms/src/pass.rs | 15 +++++++++++++-- src/builder.rs | 3 +-- src/config.rs | 2 +- src/lib.rs | 29 +++++++++++++++++++++++++++-- 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/ecmascript/transforms/src/pass.rs b/ecmascript/transforms/src/pass.rs index c220f707d95..c81588fa570 100644 --- a/ecmascript/transforms/src/pass.rs +++ b/ecmascript/transforms/src/pass.rs @@ -1,13 +1,24 @@ use swc_common::pass::CompilerPass; pub use swc_common::pass::{Optional, Repeated}; +use swc_ecma_ast::{Module, Script}; use swc_ecma_visit::Fold; pub fn noop() -> impl Fold { - struct Noop; - impl Fold for Noop {} Noop } +struct Noop; +impl Fold for Noop { + #[inline(always)] + fn fold_module(&mut self, m: Module) -> Module { + m + } + #[inline(always)] + fn fold_script(&mut self, s: Script) -> Script { + s + } +} + pub trait JsPass: CompilerPass + Fold {} impl JsPass for T where T: CompilerPass + Fold {} diff --git a/src/builder.rs b/src/builder.rs index 8ceb7a05e3c..280ce2f49bf 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -112,7 +112,6 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> { /// - fixer if enabled pub fn finalize<'cmt>( self, - root_mark: Mark, syntax: Syntax, module: Option, comments: Option<&'cmt dyn Comments>, @@ -183,7 +182,7 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> { need_interop_analysis ), helpers::inject_helpers(), - ModuleConfig::build(self.cm.clone(), root_mark, module), + ModuleConfig::build(self.cm.clone(), self.global_mark, module), Optional::new(hygiene(), self.hygiene), Optional::new(fixer(comments), self.fixer), ) diff --git a/src/config.rs b/src/config.rs index e562724c88b..418d2c5d09e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -250,7 +250,7 @@ impl Options { .hygiene(!self.disable_hygiene) .fixer(!self.disable_fixer) .preset_env(config.env) - .finalize(root_mark, syntax, config.module, comments); + .finalize(syntax, config.module, comments); BuiltConfig { minify: config.minify.unwrap_or(false), diff --git a/src/lib.rs b/src/lib.rs index 3f6a6873ec1..91104bac40a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,6 +17,7 @@ use std::{ sync::Arc, }; use swc_common::{ + chain, comments::{Comment, Comments}, errors::Handler, input::StringInput, @@ -27,6 +28,7 @@ use swc_ecma_codegen::{self, Emitter, Node}; use swc_ecma_parser::{lexer::Lexer, Parser, Syntax}; use swc_ecma_transforms::{ helpers::{self, Helpers}, + pass::noop, util, }; use swc_ecma_visit::FoldWith; @@ -386,13 +388,28 @@ impl Compiler { }) } - pub fn process_js_file( + /// `custom_after_pass` is applied after swc transforms are applied. + pub fn process_js_with_custom_pass

( &self, fm: Arc, opts: &Options, - ) -> Result { + custom_after_pass: P, + ) -> Result + where + P: swc_ecma_visit::Fold, + { self.run(|| -> Result<_, Error> { let config = self.run(|| self.config_for_file(opts, &fm.name))?; + let config = BuiltConfig { + pass: chain!(config.pass, custom_after_pass), + syntax: config.syntax, + target: config.target, + minify: config.minify, + external_helpers: config.external_helpers, + source_maps: config.source_maps, + input_source_map: config.input_source_map, + is_module: config.is_module, + }; let orig = self.get_orig_src_map(&fm, &opts.input_source_map)?; let program = self.parse_js( fm.clone(), @@ -407,6 +424,14 @@ impl Compiler { .context("failed to process js file") } + pub fn process_js_file( + &self, + fm: Arc, + opts: &Options, + ) -> Result { + self.process_js_with_custom_pass(fm, opts, noop()) + } + /// You can use custom pass with this method. /// /// There exists a [PassBuilder] to help building custom passes.