mirror of
https://github.com/swc-project/swc.git
synced 2024-11-23 17:54:15 +03:00
Cache compiled helpers
This commit is contained in:
parent
e85bcce9cf
commit
b9eb110549
@ -15,8 +15,9 @@ swc_ecma_ast = { path ="../ast" }
|
||||
swc_ecma_parser = { path ="../parser" }
|
||||
either = "1.5"
|
||||
fnv = "1"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
lazy_static = "1"
|
||||
objekt = "0.1"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
|
||||
[dev-dependencies]
|
||||
testing = { path ="../../testing" }
|
||||
|
@ -6,7 +6,7 @@ use std::sync::{
|
||||
use swc_common::{
|
||||
errors::{ColorConfig, Handler},
|
||||
sync::Lrc,
|
||||
FileName, Fold, SourceMap,
|
||||
FileName, FilePathMapping, Fold, SourceMap,
|
||||
};
|
||||
use swc_ecma_parser::{Parser, Session, SourceFileInput, Syntax};
|
||||
|
||||
@ -53,36 +53,42 @@ pub struct InjectHelpers {
|
||||
|
||||
impl InjectHelpers {
|
||||
fn mk_helpers(&self) -> Vec<Stmt> {
|
||||
lazy_static! {
|
||||
static ref CM: Lrc<SourceMap> = { Lrc::new(SourceMap::new(FilePathMapping::empty())) };
|
||||
static ref HANDLER: Handler =
|
||||
{ Handler::with_tty_emitter(ColorConfig::Always, false, true, Some(CM.clone())) };
|
||||
static ref SESSION: Session<'static> = { Session { handler: &*HANDLER } };
|
||||
}
|
||||
|
||||
let mut buf = vec![];
|
||||
|
||||
let handler =
|
||||
Handler::with_tty_emitter(ColorConfig::Always, false, true, Some(self.cm.clone()));
|
||||
|
||||
let session = Session { handler: &handler };
|
||||
|
||||
let mut add = |name: &str, flag: &AtomicBool, code: &'static str| {
|
||||
let enable = flag.load(Ordering::Relaxed);
|
||||
if !enable {
|
||||
return;
|
||||
}
|
||||
let fm = self
|
||||
.cm
|
||||
.new_source_file(FileName::Custom(name.into()), code.into());
|
||||
|
||||
let mut stmts = Parser::new(session, Syntax::default(), SourceFileInput::from(&*fm))
|
||||
.parse_script()
|
||||
.map_err(|e| {
|
||||
e.emit();
|
||||
()
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
buf.append(&mut stmts);
|
||||
};
|
||||
macro_rules! add {
|
||||
($name:tt,$b:expr) => {
|
||||
add($name, $b, include_str!($name));
|
||||
};
|
||||
($name:tt, $b:expr) => {{
|
||||
lazy_static! {
|
||||
static ref STMTS: Vec<Stmt> = {
|
||||
let code = include_str!($name);
|
||||
let fm = CM.new_source_file(
|
||||
FileName::Custom(stringify!($name).into()),
|
||||
code.into(),
|
||||
);
|
||||
|
||||
let stmts =
|
||||
Parser::new(*SESSION, Syntax::default(), SourceFileInput::from(&*fm))
|
||||
.parse_script()
|
||||
.map_err(|e| {
|
||||
e.emit();
|
||||
()
|
||||
})
|
||||
.unwrap();
|
||||
stmts
|
||||
};
|
||||
}
|
||||
|
||||
let enable = $b.load(Ordering::Relaxed);
|
||||
if enable {
|
||||
buf.extend_from_slice(&STMTS)
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
||||
add!("_extends.js", &self.helpers.extends);
|
||||
|
@ -18,6 +18,8 @@ extern crate swc_ecma_ast as ast;
|
||||
#[cfg(test)]
|
||||
extern crate swc_ecma_codegen;
|
||||
extern crate swc_ecma_parser;
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
#[cfg(test)]
|
||||
#[macro_use]
|
||||
extern crate pretty_assertions;
|
||||
|
Loading…
Reference in New Issue
Block a user