mirror of
https://github.com/swc-project/swc.git
synced 2024-11-28 02:29:04 +03:00
fix(es/modules): Fix lazy option of common js (#4807)
This commit is contained in:
parent
5d7545846b
commit
f4c6a20654
@ -69,13 +69,13 @@ pub fn common_js_with_resolver(
|
||||
|
||||
struct LazyIdentifierVisitor {
|
||||
scope: Rc<RefCell<Scope>>,
|
||||
top_level_idents: AHashSet<JsWord>,
|
||||
denied_sources: AHashSet<JsWord>,
|
||||
}
|
||||
impl LazyIdentifierVisitor {
|
||||
fn new(scope: Rc<RefCell<Scope>>) -> Self {
|
||||
LazyIdentifierVisitor {
|
||||
scope,
|
||||
top_level_idents: Default::default(),
|
||||
denied_sources: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -89,41 +89,13 @@ imported specifier name.
|
||||
impl Visit for LazyIdentifierVisitor {
|
||||
noop_visit_type!();
|
||||
|
||||
fn visit_import_decl(&mut self, _: &ImportDecl) {}
|
||||
|
||||
fn visit_export_decl(&mut self, _: &ExportDecl) {}
|
||||
|
||||
fn visit_named_export(&mut self, _: &NamedExport) {}
|
||||
|
||||
fn visit_export_default_decl(&mut self, _: &ExportDefaultDecl) {}
|
||||
|
||||
fn visit_export_default_expr(&mut self, _: &ExportDefaultExpr) {}
|
||||
|
||||
fn visit_export_all(&mut self, export: &ExportAll) {
|
||||
self.top_level_idents.insert(export.src.value.clone());
|
||||
}
|
||||
|
||||
fn visit_labeled_stmt(&mut self, _: &LabeledStmt) {}
|
||||
|
||||
fn visit_continue_stmt(&mut self, _: &ContinueStmt) {}
|
||||
|
||||
fn visit_arrow_expr(&mut self, _: &ArrowExpr) {}
|
||||
|
||||
fn visit_function(&mut self, _: &Function) {}
|
||||
|
||||
fn visit_constructor(&mut self, _: &Constructor) {}
|
||||
|
||||
fn visit_setter_prop(&mut self, _: &SetterProp) {}
|
||||
|
||||
fn visit_getter_prop(&mut self, _: &GetterProp) {}
|
||||
|
||||
fn visit_class_prop(&mut self, _: &ClassProp) {}
|
||||
|
||||
fn visit_prop_name(&mut self, prop_name: &PropName) {
|
||||
if let PropName::Computed(n) = prop_name {
|
||||
n.visit_with(self)
|
||||
}
|
||||
}
|
||||
fn visit_constructor(&mut self, _: &Constructor) {}
|
||||
|
||||
fn visit_continue_stmt(&mut self, _: &ContinueStmt) {}
|
||||
|
||||
fn visit_decl(&mut self, decl: &Decl) {
|
||||
if let Decl::Class(ref c) = decl {
|
||||
@ -132,12 +104,52 @@ impl Visit for LazyIdentifierVisitor {
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_export_all(&mut self, export: &ExportAll) {
|
||||
self.denied_sources.insert(export.src.value.clone());
|
||||
}
|
||||
|
||||
fn visit_export_decl(&mut self, _: &ExportDecl) {}
|
||||
|
||||
fn visit_export_default_decl(&mut self, _: &ExportDefaultDecl) {}
|
||||
|
||||
fn visit_export_default_expr(&mut self, _: &ExportDefaultExpr) {}
|
||||
|
||||
fn visit_export_default_specifier(&mut self, _: &ExportDefaultSpecifier) {}
|
||||
|
||||
fn visit_export_named_specifier(&mut self, _: &ExportNamedSpecifier) {}
|
||||
|
||||
fn visit_export_namespace_specifier(&mut self, _: &ExportNamespaceSpecifier) {}
|
||||
|
||||
fn visit_function(&mut self, _: &Function) {}
|
||||
|
||||
fn visit_getter_prop(&mut self, _: &GetterProp) {}
|
||||
|
||||
fn visit_ident(&mut self, ident: &Ident) {
|
||||
let v = self.scope.borrow().idents.get(&ident.to_id()).cloned();
|
||||
if let Some((src, _)) = v {
|
||||
self.top_level_idents.insert(src);
|
||||
self.denied_sources.insert(src);
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_import_decl(&mut self, _: &ImportDecl) {}
|
||||
|
||||
fn visit_labeled_stmt(&mut self, _: &LabeledStmt) {}
|
||||
|
||||
fn visit_member_prop(&mut self, n: &MemberProp) {
|
||||
if let MemberProp::Computed(n) = n {
|
||||
n.visit_with(self);
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_named_export(&mut self, _: &NamedExport) {}
|
||||
|
||||
fn visit_prop_name(&mut self, prop_name: &PropName) {
|
||||
if let PropName::Computed(n) = prop_name {
|
||||
n.visit_with(self)
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_setter_prop(&mut self, _: &SetterProp) {}
|
||||
}
|
||||
|
||||
struct CommonJs {
|
||||
@ -245,12 +257,12 @@ impl Fold for CommonJs {
|
||||
}
|
||||
}
|
||||
|
||||
// Map all top-level identifiers that match imported specifiers, and blacklist
|
||||
// Map all top-level identifiers that match imported specifiers, and denylist
|
||||
// them from lazy imports.
|
||||
let mut visitor = LazyIdentifierVisitor::new(self.scope.clone());
|
||||
items.visit_with(&mut visitor);
|
||||
for ident in visitor.top_level_idents {
|
||||
self.scope.borrow_mut().lazy_blacklist.insert(ident);
|
||||
for ident in visitor.denied_sources {
|
||||
self.scope.borrow_mut().lazy_denylist.insert(ident);
|
||||
}
|
||||
|
||||
for item in items {
|
||||
@ -638,7 +650,7 @@ impl Fold for CommonJs {
|
||||
}
|
||||
|
||||
let lazy = if let Some(ref src) = export.src {
|
||||
if scope.lazy_blacklist.contains(&src.value) {
|
||||
if scope.lazy_denylist.contains(&src.value) {
|
||||
false
|
||||
} else {
|
||||
self.config.lazy.is_lazy(&src.value)
|
||||
@ -649,7 +661,7 @@ impl Fold for CommonJs {
|
||||
.get(&(orig.sym.clone(), orig.span.ctxt()))
|
||||
{
|
||||
Some((ref src, _)) => {
|
||||
if scope.lazy_blacklist.contains(src) {
|
||||
if scope.lazy_denylist.contains(src) {
|
||||
false
|
||||
} else {
|
||||
self.config.lazy.is_lazy(src)
|
||||
@ -816,7 +828,7 @@ impl Fold for CommonJs {
|
||||
|
||||
let scope = &mut *scope;
|
||||
for (src, (src_span, import)) in scope.imports.drain(..) {
|
||||
let lazy = if scope.lazy_blacklist.contains(&src) {
|
||||
let lazy = if scope.lazy_denylist.contains(&src) {
|
||||
false
|
||||
} else {
|
||||
self.config.lazy.is_lazy(&src)
|
||||
|
@ -157,7 +157,7 @@ pub struct Scope {
|
||||
|
||||
/// This is required to handle
|
||||
/// `export * from 'foo';`
|
||||
pub(crate) lazy_blacklist: AHashSet<JsWord>,
|
||||
pub(crate) lazy_denylist: AHashSet<JsWord>,
|
||||
}
|
||||
|
||||
impl Scope {
|
||||
@ -504,7 +504,7 @@ impl Scope {
|
||||
match v {
|
||||
None => Err(i),
|
||||
Some((src, prop)) => {
|
||||
let lazy = if folder.scope().lazy_blacklist.contains(&src) {
|
||||
let lazy = if folder.scope().lazy_denylist.contains(&src) {
|
||||
false
|
||||
} else {
|
||||
folder.config().lazy.is_lazy(&src)
|
||||
|
@ -5422,6 +5422,31 @@ exports.format = format = '123';
|
||||
"#
|
||||
);
|
||||
|
||||
test!(
|
||||
syntax(),
|
||||
|_| tr(Config {
|
||||
lazy: Lazy::Bool(true),
|
||||
..Default::default()
|
||||
}),
|
||||
issue_4799,
|
||||
r#"
|
||||
export { createP } from './St';
|
||||
"#,
|
||||
r#"
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
Object.defineProperty(exports, "createP", {
|
||||
enumerable: true,
|
||||
get: function() {
|
||||
return _st.createP;
|
||||
}
|
||||
});
|
||||
var _st = require("./St");
|
||||
"#
|
||||
);
|
||||
|
||||
#[testing::fixture("tests/fixture/commonjs/**/input.js")]
|
||||
fn fixture(input: PathBuf) {
|
||||
let dir = input.parent().unwrap().to_path_buf();
|
||||
|
Loading…
Reference in New Issue
Block a user