fix(es/modules): Fix lazy option of common js (#4807)

This commit is contained in:
Donny/강동윤 2022-05-26 22:06:43 +09:00 committed by GitHub
parent 5d7545846b
commit f4c6a20654
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 79 additions and 42 deletions

View File

@ -69,13 +69,13 @@ pub fn common_js_with_resolver(
struct LazyIdentifierVisitor { struct LazyIdentifierVisitor {
scope: Rc<RefCell<Scope>>, scope: Rc<RefCell<Scope>>,
top_level_idents: AHashSet<JsWord>, denied_sources: AHashSet<JsWord>,
} }
impl LazyIdentifierVisitor { impl LazyIdentifierVisitor {
fn new(scope: Rc<RefCell<Scope>>) -> Self { fn new(scope: Rc<RefCell<Scope>>) -> Self {
LazyIdentifierVisitor { LazyIdentifierVisitor {
scope, scope,
top_level_idents: Default::default(), denied_sources: Default::default(),
} }
} }
} }
@ -89,41 +89,13 @@ imported specifier name.
impl Visit for LazyIdentifierVisitor { impl Visit for LazyIdentifierVisitor {
noop_visit_type!(); 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_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_class_prop(&mut self, _: &ClassProp) {}
fn visit_prop_name(&mut self, prop_name: &PropName) { fn visit_constructor(&mut self, _: &Constructor) {}
if let PropName::Computed(n) = prop_name {
n.visit_with(self) fn visit_continue_stmt(&mut self, _: &ContinueStmt) {}
}
}
fn visit_decl(&mut self, decl: &Decl) { fn visit_decl(&mut self, decl: &Decl) {
if let Decl::Class(ref c) = 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) { fn visit_ident(&mut self, ident: &Ident) {
let v = self.scope.borrow().idents.get(&ident.to_id()).cloned(); let v = self.scope.borrow().idents.get(&ident.to_id()).cloned();
if let Some((src, _)) = v { 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 { 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. // them from lazy imports.
let mut visitor = LazyIdentifierVisitor::new(self.scope.clone()); let mut visitor = LazyIdentifierVisitor::new(self.scope.clone());
items.visit_with(&mut visitor); items.visit_with(&mut visitor);
for ident in visitor.top_level_idents { for ident in visitor.denied_sources {
self.scope.borrow_mut().lazy_blacklist.insert(ident); self.scope.borrow_mut().lazy_denylist.insert(ident);
} }
for item in items { for item in items {
@ -638,7 +650,7 @@ impl Fold for CommonJs {
} }
let lazy = if let Some(ref src) = export.src { let lazy = if let Some(ref src) = export.src {
if scope.lazy_blacklist.contains(&src.value) { if scope.lazy_denylist.contains(&src.value) {
false false
} else { } else {
self.config.lazy.is_lazy(&src.value) self.config.lazy.is_lazy(&src.value)
@ -649,7 +661,7 @@ impl Fold for CommonJs {
.get(&(orig.sym.clone(), orig.span.ctxt())) .get(&(orig.sym.clone(), orig.span.ctxt()))
{ {
Some((ref src, _)) => { Some((ref src, _)) => {
if scope.lazy_blacklist.contains(src) { if scope.lazy_denylist.contains(src) {
false false
} else { } else {
self.config.lazy.is_lazy(src) self.config.lazy.is_lazy(src)
@ -816,7 +828,7 @@ impl Fold for CommonJs {
let scope = &mut *scope; let scope = &mut *scope;
for (src, (src_span, import)) in scope.imports.drain(..) { 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 false
} else { } else {
self.config.lazy.is_lazy(&src) self.config.lazy.is_lazy(&src)

View File

@ -157,7 +157,7 @@ pub struct Scope {
/// This is required to handle /// This is required to handle
/// `export * from 'foo';` /// `export * from 'foo';`
pub(crate) lazy_blacklist: AHashSet<JsWord>, pub(crate) lazy_denylist: AHashSet<JsWord>,
} }
impl Scope { impl Scope {
@ -504,7 +504,7 @@ impl Scope {
match v { match v {
None => Err(i), None => Err(i),
Some((src, prop)) => { Some((src, prop)) => {
let lazy = if folder.scope().lazy_blacklist.contains(&src) { let lazy = if folder.scope().lazy_denylist.contains(&src) {
false false
} else { } else {
folder.config().lazy.is_lazy(&src) folder.config().lazy.is_lazy(&src)

View File

@ -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")] #[testing::fixture("tests/fixture/commonjs/**/input.js")]
fn fixture(input: PathBuf) { fn fixture(input: PathBuf) {
let dir = input.parent().unwrap().to_path_buf(); let dir = input.parent().unwrap().to_path_buf();