refactor(es): Make compilation faster (#3749)

This commit is contained in:
Donny/강동윤 2022-02-26 15:41:14 +09:00 committed by GitHub
parent a8a5ca2376
commit 3d944656bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 223 additions and 256 deletions

View File

@ -7,7 +7,7 @@ use swc_common::{
comments::{Comment, CommentKind, Comments},
BytePos, DUMMY_SP,
};
use swc_ecma_parser::{lexer::Lexer, EsConfig, Parser, StringInput, Syntax};
use swc_ecma_parser::{parse_file_as_module, EsConfig, Syntax};
use testing::NormalizedOutput;
#[testing::fixture("tests/fixtures/**/*.js")]
@ -17,21 +17,21 @@ fn fixture(path: PathBuf) {
let fm = cm.load_file(&path).expect("failed to load fixture file");
let lexer = Lexer::new(
let mut errors = vec![];
if let Err(err) = parse_file_as_module(
&fm,
Syntax::Es(EsConfig {
jsx: true,
..Default::default()
}),
Default::default(),
StringInput::from(&*fm),
Some(&comments),
);
let mut p = Parser::new_from(lexer);
if let Err(err) = p.parse_module() {
&mut errors,
) {
err.into_diagnostic(&handler).emit();
}
for err in p.take_errors() {
for err in errors {
err.into_diagnostic(&handler).emit();
}
if handler.has_errors() {

View File

@ -30,7 +30,7 @@ use swc_ecma_loader::{
use swc_ecma_minifier::option::{
CompressOptions, ExtraOptions, MangleOptions, MinifyOptions, TopLevelOptions,
};
use swc_ecma_parser::{lexer::Lexer, EsConfig, Parser, StringInput, Syntax};
use swc_ecma_parser::{parse_file_as_module, EsConfig, Syntax};
use swc_ecma_transforms_base::fixer::fixer;
use swc_ecma_visit::VisitMutWith;
@ -228,17 +228,16 @@ impl Load for Loader {
_ => unreachable!(),
};
let lexer = Lexer::new(
let module = parse_file_as_module(
&fm,
Syntax::Es(EsConfig {
..Default::default()
}),
EsVersion::Es2020,
StringInput::from(&*fm),
None,
);
let mut parser = Parser::new_from(lexer);
let module = parser.parse_module().unwrap_or_else(|err| {
&mut vec![],
)
.unwrap_or_else(|err| {
let handler =
Handler::with_tty_emitter(ColorConfig::Always, false, false, Some(self.cm.clone()));
err.into_diagnostic(&handler).emit();

View File

@ -5,7 +5,7 @@ use swc_bundler::{BundleKind, Bundler, Config, Hook, Load, ModuleData, ModuleRec
use swc_common::{sync::Lrc, FileName, FilePathMapping, Globals, SourceMap, Span};
use swc_ecma_ast::KeyValueProp;
use swc_ecma_codegen::{text_writer::JsWriter, Emitter};
use swc_ecma_parser::{lexer::Lexer, EsConfig, Parser, StringInput, Syntax};
use swc_ecma_parser::{parse_file_as_module, EsConfig, Syntax};
fn main() {
let _log = testing::init();
@ -67,17 +67,17 @@ impl Load for PathLoader {
};
let fm = self.cm.load_file(file)?;
let lexer = Lexer::new(
let module = parse_file_as_module(
&fm,
Syntax::Es(EsConfig {
..Default::default()
}),
Default::default(),
StringInput::from(&*fm),
None,
);
let mut parser = Parser::new_from(lexer);
let module = parser.parse_module().expect("This should not happen");
&mut vec![],
)
.expect("This should not happen");
Ok(ModuleData {
fm,

View File

@ -3,7 +3,7 @@ use std::sync::atomic::{AtomicBool, Ordering::SeqCst};
use once_cell::sync::Lazy;
use swc_common::{FileName, FilePathMapping, SourceMap};
use swc_ecma_ast::*;
use swc_ecma_parser::{lexer::Lexer, Parser, StringInput};
use swc_ecma_parser::parse_file_as_module;
use swc_ecma_utils::{drop_span, prepend_stmts};
#[derive(Debug, Default)]
@ -12,6 +12,21 @@ pub(crate) struct Helpers {
pub require: AtomicBool,
}
fn parse(code: &'static str, name: &'static str) -> Vec<ModuleItem> {
let cm = SourceMap::new(FilePathMapping::empty());
let fm = cm.new_source_file(FileName::Custom(name.into()), code.into());
parse_file_as_module(
&fm,
Default::default(),
Default::default(),
None,
&mut vec![],
)
.map(|script| drop_span(script.body))
.map_err(|_| {})
.unwrap()
}
macro_rules! define {
(
$(
@ -23,24 +38,7 @@ macro_rules! define {
$(
fn $build(to: &mut Vec<ModuleItem>) {
static STMTS: Lazy<Vec<ModuleItem>> = Lazy::new(|| {
let cm = SourceMap::new(FilePathMapping::empty());
let code = include_str!(concat!("_", stringify!($name), ".js"));
let fm =
cm.new_source_file(FileName::Custom(stringify!($name).into()), code.into());
let lexer = Lexer::new(
Default::default(),
Default::default(),
StringInput::from(&*fm),
None,
);
let stmts = Parser::new_from(lexer)
.parse_module()
.map(|script| drop_span(script.body))
.map_err(|_| {
})
.unwrap();
stmts
parse(include_str!(concat!("_", stringify!($name), ".js")), stringify!($name))
});
to.extend((*STMTS).clone());

View File

@ -17,7 +17,7 @@ use swc_common::{
FileName, Mark, SourceMap,
};
use swc_ecma_ast::EsVersion;
use swc_ecma_parser::{lexer::Lexer, Parser, StringInput, Syntax, TsConfig};
use swc_ecma_parser::{parse_file_as_module, Syntax, TsConfig};
use swc_ecma_transforms_base::resolver::resolver_with_mark;
use swc_ecma_transforms_react::react;
use swc_ecma_transforms_typescript::strip;
@ -109,19 +109,18 @@ impl Load for Loader {
_ => unreachable!(),
};
let lexer = Lexer::new(
let module = parse_file_as_module(
&fm,
Syntax::Typescript(TsConfig {
decorators: true,
tsx,
..Default::default()
}),
EsVersion::Es2020,
StringInput::from(&*fm),
None,
);
let mut parser = Parser::new_from(lexer);
let module = parser.parse_module().unwrap_or_else(|err| {
&mut vec![],
)
.unwrap_or_else(|err| {
let handler =
Handler::with_tty_emitter(ColorConfig::Always, false, false, Some(self.cm.clone()));
err.into_diagnostic(&handler).emit();

View File

@ -1,12 +1,11 @@
use std::path::{Path, PathBuf};
use swc_common::input::SourceFileInput;
use swc_ecma_ast::EsVersion;
use swc_ecma_codegen::{
text_writer::{JsWriter, WriteJs},
Emitter,
};
use swc_ecma_parser::{lexer::Lexer, Parser, Syntax};
use swc_ecma_parser::{parse_file_as_module, Syntax};
use testing::{run_test2, NormalizedOutput};
fn run(input: &Path, minify: bool) {
@ -26,16 +25,14 @@ fn run(input: &Path, minify: bool) {
run_test2(false, |cm, _| {
let fm = cm.load_file(input).unwrap();
let lexer = Lexer::new(
let m = parse_file_as_module(
&fm,
Syntax::Typescript(Default::default()),
EsVersion::latest(),
SourceFileInput::from(&*fm),
None,
);
let mut parser = Parser::new_from(lexer);
let m = parser
.parse_module()
.expect("failed to parse input as a module");
&mut vec![],
)
.expect("failed to parse input as a module");
let mut buf = vec![];

View File

@ -1,14 +1,16 @@
#![deny(warnings)]
extern crate swc_node_base;
use std::{env::args, fs, path::Path};
use swc_common::{input::SourceFileInput, sync::Lrc, Mark, SourceMap};
use swc_common::{sync::Lrc, Mark, SourceMap};
use swc_ecma_codegen::text_writer::JsWriter;
use swc_ecma_minifier::{
optimize,
option::{ExtraOptions, MangleOptions, MinifyOptions},
};
use swc_ecma_parser::{lexer::Lexer, Parser};
use swc_ecma_parser::parse_file_as_module;
use swc_ecma_transforms::{fixer, hygiene, resolver_with_mark};
use swc_ecma_visit::FoldWith;
@ -22,21 +24,18 @@ fn main() {
let top_level_mark = Mark::fresh(Mark::root());
let lexer = Lexer::new(
let program = parse_file_as_module(
&fm,
Default::default(),
Default::default(),
SourceFileInput::from(&*fm),
None,
);
let mut parser = Parser::new_from(lexer);
let program = parser
.parse_module()
.map_err(|err| {
err.into_diagnostic(&handler).emit();
})
.map(|module| module.fold_with(&mut resolver_with_mark(top_level_mark)))
.unwrap();
&mut vec![],
)
.map_err(|err| {
err.into_diagnostic(&handler).emit();
})
.map(|module| module.fold_with(&mut resolver_with_mark(top_level_mark)))
.unwrap();
let output = optimize(
program,

View File

@ -1,8 +1,8 @@
use std::path::PathBuf;
use swc_common::{comments::SingleThreadedComments, input::SourceFileInput, Mark};
use swc_common::{comments::SingleThreadedComments, Mark};
use swc_ecma_ast::Id;
use swc_ecma_parser::{lexer::Lexer, EsConfig, Parser, Syntax};
use swc_ecma_parser::{parse_file_as_module, EsConfig, Syntax};
use swc_ecma_transforms::resolver_with_mark;
use swc_ecma_visit::FoldWith;
use testing::NormalizedOutput;
@ -22,25 +22,22 @@ fn snapshot(input: PathBuf) {
let top_level_mark = Mark::fresh(Mark::root());
let lexer = Lexer::new(
let marks = Marks::new();
let program = parse_file_as_module(
&fm,
Syntax::Es(EsConfig {
jsx: true,
..Default::default()
}),
Default::default(),
SourceFileInput::from(&*fm),
Some(&comments),
);
let marks = Marks::new();
let mut parser = Parser::new_from(lexer);
let program = parser
.parse_module()
.map_err(|err| {
err.into_diagnostic(&handler).emit();
})
.map(|module| module.fold_with(&mut resolver_with_mark(top_level_mark)));
&mut vec![],
)
.map_err(|err| {
err.into_diagnostic(&handler).emit();
})
.map(|module| module.fold_with(&mut resolver_with_mark(top_level_mark)));
let program = match program {
Ok(program) => program,

View File

@ -1,6 +1,6 @@
use swc_common::{input::SourceFileInput, util::take::Take, FileName};
use swc_common::{util::take::Take, FileName};
use swc_ecma_ast::*;
use swc_ecma_parser::{lexer::Lexer, Parser};
use swc_ecma_parser::parse_file_as_expr;
use swc_ecma_transforms::fixer;
use swc_ecma_visit::{noop_visit_mut_type, FoldWith, VisitMut, VisitMutWith};
use tracing::{info, warn};
@ -25,16 +25,14 @@ fn assert_negate_cost(s: &str, in_bool_ctx: bool, is_ret_val_ignored: bool, expe
testing::run_test2(false, |cm, handler| {
let fm = cm.new_source_file(FileName::Anon, s.to_string());
let lexer = Lexer::new(
let mut e = parse_file_as_expr(
&fm,
Default::default(),
swc_ecma_ast::EsVersion::latest(),
SourceFileInput::from(&*fm),
None,
);
let mut parser = Parser::new_from(lexer);
let mut e = parser.parse_expr().map_err(|e| {
&mut vec![],
)
.map_err(|e| {
e.into_diagnostic(&handler).emit();
})?;

View File

@ -1,11 +1,13 @@
use swc_common::{input::SourceFileInput, sync::Lrc, FileName, SourceMap};
#![deny(warnings)]
use swc_common::{sync::Lrc, FileName, SourceMap};
use swc_ecma_ast::*;
use swc_ecma_codegen::{text_writer::JsWriter, Emitter};
use swc_ecma_minifier::{
eval::{EvalResult, Evaluator},
marks::Marks,
};
use swc_ecma_parser::{lexer::Lexer, EsConfig, Parser, Syntax};
use swc_ecma_parser::{parse_file_as_expr, parse_file_as_module, EsConfig, Syntax};
use swc_ecma_transforms::resolver;
use swc_ecma_utils::ExprExt;
use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith};
@ -16,28 +18,26 @@ fn eval(module: &str, expr: &str) -> Option<String> {
let fm = cm.new_source_file(FileName::Anon, module.to_string());
let marks = Marks::new();
let module_ast = {
let lexer = Lexer::new(
Default::default(),
EsVersion::latest(),
SourceFileInput::from(&*fm),
None,
);
let mut parser = Parser::new_from(lexer);
parser.parse_module().unwrap()
};
let module_ast = parse_file_as_module(
&fm,
Default::default(),
EsVersion::latest(),
None,
&mut vec![],
)
.unwrap();
let expr_ast = {
let fm = cm.new_source_file(FileName::Anon, expr.to_string());
let lexer = Lexer::new(
parse_file_as_expr(
&fm,
Default::default(),
EsVersion::latest(),
SourceFileInput::from(&*fm),
None,
);
let mut parser = Parser::new_from(lexer);
parser.parse_expr().unwrap()
&mut vec![],
)
.unwrap()
};
let mut evaluator = Evaluator::new(module_ast, marks);
@ -81,19 +81,17 @@ impl PartialInliner {
let fm = cm.new_source_file(FileName::Anon, src.to_string());
let marks = Marks::new();
let mut module = {
let lexer = Lexer::new(
Syntax::Es(EsConfig {
jsx: true,
..Default::default()
}),
EsVersion::latest(),
SourceFileInput::from(&*fm),
None,
);
let mut parser = Parser::new_from(lexer);
parser.parse_module().unwrap()
};
let mut module = parse_file_as_module(
&fm,
Syntax::Es(EsConfig {
jsx: true,
..Default::default()
}),
EsVersion::latest(),
None,
&mut vec![],
)
.unwrap();
module.visit_mut_with(&mut resolver());
let mut inliner = PartialInliner {
@ -115,17 +113,18 @@ impl PartialInliner {
let expected_module = {
let fm = cm.new_source_file(FileName::Anon, expected.to_string());
let lexer = Lexer::new(
parse_file_as_module(
&fm,
Syntax::Es(EsConfig {
jsx: true,
..Default::default()
}),
EsVersion::latest(),
SourceFileInput::from(&*fm),
None,
);
let mut parser = Parser::new_from(lexer);
parser.parse_module().unwrap()
&mut vec![],
)
.unwrap()
};
let expected = {
let mut buf = vec![];

View File

@ -8,8 +8,7 @@ use ansi_term::Color;
use anyhow::{bail, Context, Error};
use serde::Deserialize;
use swc_common::{
comments::SingleThreadedComments, errors::Handler, input::SourceFileInput, sync::Lrc, FileName,
Mark, SourceMap,
comments::SingleThreadedComments, errors::Handler, sync::Lrc, FileName, Mark, SourceMap,
};
use swc_ecma_ast::Module;
use swc_ecma_codegen::{
@ -23,7 +22,7 @@ use swc_ecma_minifier::{
MinifyOptions,
},
};
use swc_ecma_parser::{lexer::Lexer, EsConfig, Parser, Syntax};
use swc_ecma_parser::{parse_file_as_module, EsConfig, Syntax};
use swc_ecma_transforms::{fixer, hygiene, resolver_with_mark};
use swc_ecma_visit::{FoldWith, VisitMutWith};
use testing::DebugUsingDisplay;
@ -112,23 +111,20 @@ fn run(
let top_level_mark = Mark::fresh(Mark::root());
let lexer = Lexer::new(
let program = parse_file_as_module(
&fm,
Syntax::Es(EsConfig {
jsx: true,
..Default::default()
}),
Default::default(),
SourceFileInput::from(&*fm),
Some(&comments),
);
let mut parser = Parser::new_from(lexer);
let program = parser
.parse_module()
.map_err(|err| {
err.into_diagnostic(handler).emit();
})
.map(|module| module.fold_with(&mut resolver_with_mark(top_level_mark)));
&mut vec![],
)
.map_err(|err| {
err.into_diagnostic(handler).emit();
})
.map(|module| module.fold_with(&mut resolver_with_mark(top_level_mark)));
// Ignore parser errors.
//

View File

@ -2,7 +2,7 @@
use std::path::{Path, PathBuf};
use swc_common::{input::SourceFileInput, sync::Lrc, FileName, Mark, SourceFile, SourceMap};
use swc_common::{sync::Lrc, FileName, Mark, SourceFile, SourceMap};
use swc_ecma_ast::*;
use swc_ecma_codegen::{
text_writer::{omit_trailing_semi, JsWriter, WriteJs},
@ -12,7 +12,7 @@ use swc_ecma_minifier::{
optimize,
option::{ExtraOptions, MangleOptions, ManglePropertiesOptions, MinifyOptions},
};
use swc_ecma_parser::{lexer::Lexer, Parser};
use swc_ecma_parser::parse_file_as_module;
use swc_ecma_transforms::resolver_with_mark;
use swc_ecma_visit::VisitMutWith;
use testing::NormalizedOutput;
@ -46,15 +46,14 @@ fn parse(cm: Lrc<SourceMap>, path: &Path) -> Module {
}
fn parse_fm(fm: Lrc<SourceFile>) -> Module {
let lexer = Lexer::new(
parse_file_as_module(
&fm,
Default::default(),
EsVersion::latest(),
SourceFileInput::from(&*fm),
None,
);
let mut parser = Parser::new_from(lexer);
parser.parse_module().unwrap()
&mut vec![],
)
.unwrap()
}
#[testing::fixture("tests/compress/fixture/**/output.js")]

View File

@ -3,7 +3,7 @@ extern crate test;
use swc_common::{FileName, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_parser::{lexer::Lexer, Parser, StringInput, Syntax};
use swc_ecma_parser::{lexer::Lexer, parse_file_as_module, Parser, StringInput, Syntax};
use swc_ecma_transforms_base::pass::noop;
use swc_ecma_utils::ExprFactory;
use swc_ecma_visit::{FoldWith, Visit, VisitWith};
@ -17,21 +17,21 @@ fn module_clone(b: &mut Bencher) {
let _ = ::testing::run_test(false, |cm, handler| {
let fm = cm.new_source_file(FileName::Anon, SOURCE.into());
let lexer = Lexer::new(
let mut errors = vec![];
let module = parse_file_as_module(
&fm,
Syntax::Typescript(Default::default()),
Default::default(),
StringInput::from(&*fm),
None,
);
let mut parser = Parser::new_from(lexer);
let module = parser
.parse_module()
.map_err(|e| {
e.into_diagnostic(handler).emit();
})
.unwrap();
&mut errors,
)
.map_err(|e| {
e.into_diagnostic(handler).emit();
})
.unwrap();
for e in parser.take_errors() {
for e in errors {
e.into_diagnostic(handler).emit();
}
@ -46,21 +46,21 @@ fn fold_empty(b: &mut Bencher) {
let _ = ::testing::run_test(false, |cm, handler| {
let fm = cm.new_source_file(FileName::Anon, SOURCE.into());
let lexer = Lexer::new(
let mut errors = vec![];
let module = parse_file_as_module(
&fm,
Syntax::Typescript(Default::default()),
Default::default(),
StringInput::from(&*fm),
None,
);
let mut parser = Parser::new_from(lexer);
let module = parser
.parse_module()
.map_err(|e| {
e.into_diagnostic(handler).emit();
})
.unwrap();
&mut errors,
)
.map_err(|e| {
e.into_diagnostic(handler).emit();
})
.unwrap();
for e in parser.take_errors() {
for e in errors {
e.into_diagnostic(handler).emit();
}
@ -79,19 +79,18 @@ fn fold_noop_impl_all(b: &mut Bencher) {
let _ = ::testing::run_test(false, |cm, handler| {
let fm = cm.new_source_file(FileName::Anon, SOURCE.into());
let lexer = Lexer::new(
let mut errors = vec![];
let module = parse_file_as_module(
&fm,
Syntax::Typescript(Default::default()),
Default::default(),
StringInput::from(&*fm),
None,
);
let mut parser = Parser::new_from(lexer);
let module = parser
.parse_module()
.map_err(|e| e.into_diagnostic(handler).emit())
.unwrap();
&mut errors,
)
.map_err(|e| e.into_diagnostic(handler).emit())
.unwrap();
for e in parser.take_errors() {
for e in errors {
e.into_diagnostic(handler).emit();
}
@ -109,21 +108,20 @@ fn fold_noop_impl_vec(b: &mut Bencher) {
let _ = ::testing::run_test(false, |cm, handler| {
let fm = cm.new_source_file(FileName::Anon, SOURCE.into());
let lexer = Lexer::new(
let mut errors = vec![];
let module = parse_file_as_module(
&fm,
Syntax::Typescript(Default::default()),
Default::default(),
StringInput::from(&*fm),
None,
);
let mut parser = Parser::new_from(lexer);
let module = parser
.parse_module()
.map_err(|e| {
e.into_diagnostic(handler).emit();
})
.unwrap();
&mut errors,
)
.map_err(|e| {
e.into_diagnostic(handler).emit();
})
.unwrap();
for e in parser.take_errors() {
for e in errors {
e.into_diagnostic(handler).emit();
}

View File

@ -1,8 +1,10 @@
#![deny(warnings)]
use std::path::PathBuf;
use swc_common::{input::SourceFileInput, Mark, SyntaxContext};
use swc_common::{Mark, SyntaxContext};
use swc_ecma_ast::*;
use swc_ecma_parser::{lexer::Lexer, Parser, Syntax, TsConfig};
use swc_ecma_parser::{parse_file_as_module, Syntax, TsConfig};
use swc_ecma_transforms_base::resolver::ts_resolver;
use swc_ecma_visit::{FoldWith, Visit, VisitWith};
use testing::fixture;
@ -15,7 +17,8 @@ fn no_empty(input: PathBuf) {
testing::run_test2(false, |cm, _handler| {
let fm = cm.load_file(&input).expect("failed to load file");
let lexer = Lexer::new(
let module = match parse_file_as_module(
&fm,
Syntax::Typescript(TsConfig {
tsx: input.ends_with("tsx"),
decorators: true,
@ -23,12 +26,9 @@ fn no_empty(input: PathBuf) {
..Default::default()
}),
EsVersion::latest(),
SourceFileInput::from(&*fm),
None,
);
let mut parser = Parser::new_from(lexer);
let module = match parser.parse_module() {
&mut vec![],
) {
Ok(v) => v,
// We are not testing parser
Err(..) => return Ok(()),

View File

@ -17,7 +17,7 @@ use swc_atoms::js_word;
use swc_bundler::{Bundler, Load, ModuleData, ModuleRecord};
use swc_common::{sync::Lrc, FileName, Mark, SourceMap, Span, GLOBALS};
use swc_ecma_ast::*;
use swc_ecma_parser::{lexer::Lexer, Parser, StringInput, Syntax, TsConfig};
use swc_ecma_parser::{parse_file_as_module, Syntax, TsConfig};
use swc_ecma_transforms::{resolver_with_mark, typescript::strip};
use swc_ecma_visit::FoldWith;
use test::Bencher;
@ -76,19 +76,18 @@ impl Load for Loader {
_ => unreachable!(),
};
let lexer = Lexer::new(
let module = parse_file_as_module(
&fm,
Syntax::Typescript(TsConfig {
decorators: true,
tsx,
..Default::default()
}),
EsVersion::Es2020,
StringInput::from(&*fm),
None,
);
let mut parser = Parser::new_from(lexer);
let module = parser.parse_module().unwrap();
&mut vec![],
)
.unwrap();
let mark = Mark::fresh(Mark::root());
let module = module

View File

@ -1,20 +1,12 @@
use std::sync::Arc;
use anyhow::{anyhow, Error};
use swc_common::{input::SourceFileInput, SourceFile, DUMMY_SP};
use swc_common::{SourceFile, DUMMY_SP};
use swc_ecma_ast::{EsVersion, *};
use swc_ecma_parser::{lexer::Lexer, Parser, Syntax};
use swc_ecma_parser::{parse_file_as_expr, Syntax};
pub(super) fn load_json_as_module(fm: &Arc<SourceFile>) -> Result<Module, Error> {
let lexer = Lexer::new(
Syntax::default(),
EsVersion::Es2020,
SourceFileInput::from(&**fm),
None,
);
let mut parser = Parser::new_from(lexer);
let expr = parser
.parse_expr()
let expr = parse_file_as_expr(fm, Syntax::default(), EsVersion::Es2020, None, &mut vec![])
.map_err(|err| anyhow!("failed parse json as javascript object: {:#?}", err))?;
let export = ModuleItem::Stmt(Stmt::Expr(ExprStmt {

View File

@ -15,7 +15,7 @@ use swc_common::{
FileName, DUMMY_SP,
};
use swc_ecma_ast::{EsVersion, Expr, Lit, Module, Program, Str};
use swc_ecma_parser::{lexer::Lexer, Parser, StringInput, Syntax};
use swc_ecma_parser::{parse_file_as_module, Syntax};
use swc_ecma_transforms::{
helpers,
optimization::{
@ -108,14 +108,15 @@ impl SwcLoader {
.compiler
.cm
.new_source_file(name.clone(), "module.exports = {}".to_string());
let lexer = Lexer::new(
let module = parse_file_as_module(
&fm,
Syntax::Es(Default::default()),
Default::default(),
StringInput::from(&*fm),
None,
);
let mut parser = Parser::new_from(lexer);
let module = parser.parse_module().unwrap();
&mut vec![],
)
.unwrap();
return Ok(ModuleData {
fm,
module,

View File

@ -7,7 +7,7 @@ use std::{
use anyhow::{anyhow, Error};
use swc_common::{errors::HANDLER, plugin::Serialized, sync::Lazy, FileName};
use swc_ecma_ast::{CallExpr, Callee, EsVersion, Expr, Lit, MemberExpr, Program, Str};
use swc_ecma_parser::{lexer::Lexer, EsConfig, Parser, StringInput, Syntax};
use swc_ecma_parser::{parse_file_as_program, EsConfig, Syntax};
use swc_ecma_visit::{Visit, VisitWith};
use swc_plugin_runner::cache::PluginModuleCache;
@ -69,17 +69,16 @@ fn internal() -> Result<(), Error> {
testing::run_test(false, |cm, _handler| {
let fm = cm.new_source_file(FileName::Anon, "console.log(foo)".into());
let lexer = Lexer::new(
let program = parse_file_as_program(
&fm,
Syntax::Es(EsConfig {
..Default::default()
}),
EsVersion::latest(),
StringInput::from(&*fm),
None,
);
let mut parser = Parser::new_from(lexer);
let program = parser.parse_program().unwrap();
&mut vec![],
)
.unwrap();
let program = Serialized::serialize(&program).expect("Should serializable");
let config = Serialized::serialize(&"{}".to_string()).expect("Should serializable");
@ -116,17 +115,16 @@ fn internal() -> Result<(), Error> {
testing::run_test2(false, |cm, handler| {
let fm = cm.new_source_file(FileName::Anon, "console.log(foo)".into());
let lexer = Lexer::new(
let program = parse_file_as_program(
&fm,
Syntax::Es(EsConfig {
..Default::default()
}),
EsVersion::latest(),
StringInput::from(&*fm),
None,
);
let mut parser = Parser::new_from(lexer);
let program = parser.parse_program().unwrap();
&mut vec![],
)
.unwrap();
let program = Serialized::serialize(&program).expect("Should serializable");
let config = Serialized::serialize(&"{}".to_string()).expect("Should serializable");
@ -156,17 +154,16 @@ fn internal() -> Result<(), Error> {
testing::run_test(false, |cm, _handler| {
let fm = cm.new_source_file(FileName::Anon, "console.log(foo)".into());
let lexer = Lexer::new(
let program = parse_file_as_program(
&fm,
Syntax::Es(EsConfig {
..Default::default()
}),
EsVersion::latest(),
StringInput::from(&*fm),
None,
);
let mut parser = Parser::new_from(lexer);
let program = parser.parse_program().unwrap();
&mut vec![],
)
.unwrap();
let mut serialized_program = Serialized::serialize(&program).expect("Should serializable");
let cache: Lazy<PluginModuleCache> = Lazy::new(PluginModuleCache::new);

View File

@ -7,7 +7,7 @@ use swc_common::{
GLOBALS,
};
use swc_ecma_ast::*;
use swc_ecma_parser::{lexer::Lexer, EsConfig, Parser, StringInput, Syntax, TsConfig};
use swc_ecma_parser::{parse_file_as_module, EsConfig, Syntax, TsConfig};
use swc_ecma_transforms_base::resolver::resolver_with_mark;
use swc_ecma_visit::VisitMutWith;
use swc_estree_ast::flavor::Flavor;
@ -62,15 +62,14 @@ where
};
let module = {
let lexer = Lexer::new(syntax, EsVersion::latest(), StringInput::from(&*fm), None);
let mut parser = Parser::new_from(lexer);
parser.parse_module().map_err(|err| {
HANDLER.with(|h| {
err.into_diagnostic(h).emit();
anyhow!("failed to parse module")
})
})?
parse_file_as_module(&fm, syntax, EsVersion::latest(), None, &mut vec![]).map_err(
|err| {
HANDLER.with(|h| {
err.into_diagnostic(h).emit();
anyhow!("failed to parse module")
})
},
)?
};
let ast = GLOBALS.set(&globals, || webpack_ast(cm.clone(), fm.clone(), module))?;