Skip to content

Commit

Permalink
fix: tree shake use self defined idents
Browse files Browse the repository at this point in the history
  • Loading branch information
brightwwu committed May 6, 2024
1 parent 4fc6237 commit d960dd2
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 11 deletions.
29 changes: 29 additions & 0 deletions crates/compiler/tests/fixtures/tree_shake/decl/self-ref/dep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/** Used to compose unicode character classes. */
var rsAstralRange = '\\ud800-\\udfff',
rsComboMarksRange = '\\u0300-\\u036f',
reComboHalfMarksRange = '\\ufe20-\\ufe2f',
rsComboSymbolsRange = '\\u20d0-\\u20ff',
rsComboRange =
rsComboMarksRange + reComboHalfMarksRange + rsComboSymbolsRange,
rsVarRange = '\\ufe0e\\ufe0f';

/** Used to compose unicode capture groups. */
var rsZWJ = '\\u200d';

/** Used to detect strings with [zero-width joiners or code points from the astral planes](http://eev.ee/blog/2015/09/12/dark-corners-of-unicode/). */
var reHasUnicode = RegExp(
'[' + rsZWJ + rsAstralRange + rsComboRange + rsVarRange + ']'
);

/**
* Checks if `string` contains Unicode symbols.
*
* @private
* @param {string} string The string to inspect.
* @returns {boolean} Returns `true` if a symbol is found, else `false`.
*/
function hasUnicode(string) {
return reHasUnicode.test(string);
}

export default hasUnicode;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import dep from './dep.js';

console.log(dep('123'));
34 changes: 34 additions & 0 deletions crates/compiler/tests/fixtures/tree_shake/decl/self-ref/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//index.js:
(globalThis || window || global)['__farm_default_namespace__'] = {__FARM_TARGET_ENV__: 'browser'};(function(r,e){var t={};function n(r){return Promise.resolve(o(r))}function o(e){if(t[e])return t[e].exports;var i={id:e,exports:{}};r[e](i,i.exports,o,n);t[e]=i;return i.exports}o(e)})({"ec853507":function (module, exports, farmRequire, farmDynamicRequire) {
console.log("runtime/index.js")(globalThis || window || global)["__farm_default_namespace__"].__farm_module_system__.setPlugins([]);
}
,},"ec853507");(function(_){for(var r in _){_[r].__farm_resource_pot__='index_d7d4.js';(globalThis || window || global)['__farm_default_namespace__'].__farm_module_system__.register(r,_[r])}})({"3e3af5b6":function (module, exports, farmRequire, farmDynamicRequire) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "default", {
enumerable: true,
get: function() {
return _default;
}
});
var rsAstralRange = "\ud800-\udfff", rsComboMarksRange = "\\u0300-\\u036f", reComboHalfMarksRange = "\\ufe20-\\ufe2f", rsComboSymbolsRange = "\\u20d0-\\u20ff", rsComboRange = rsComboMarksRange + reComboHalfMarksRange + rsComboSymbolsRange, rsVarRange = "\\ufe0e\\ufe0f";
var rsZWJ = "\\u200d";
var reHasUnicode = RegExp("[" + rsZWJ + rsAstralRange + rsComboRange + rsVarRange + "]");
function hasUnicode(string) {
return reHasUnicode.test(string);
}
var _default = hasUnicode;
}
,
"b5d64806":function (module, exports, farmRequire, farmDynamicRequire) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _interop_require_default = farmRequire("@swc/helpers/_/_interop_require_default");
var _dep = _interop_require_default._(farmRequire("3e3af5b6"));
console.log((0, _dep.default)("123"));
}
,});(globalThis || window || global)['__farm_default_namespace__'].__farm_module_system__.setInitialLoadedResources([]);(globalThis || window || global)['__farm_default_namespace__'].__farm_module_system__.setDynamicModuleResourcesMap({ });var farmModuleSystem = (globalThis || window || global)['__farm_default_namespace__'].__farm_module_system__;farmModuleSystem.bootstrap();var entry = farmModuleSystem.require("b5d64806");
4 changes: 1 addition & 3 deletions crates/plugin_tree_shake/src/statement_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,7 @@ impl StatementGraph {
});

for (dep_stmt_id, edge_weight) in deps {
if index != dep_stmt_id {
graph.add_edge(index, dep_stmt_id, edge_weight);
}
graph.add_edge(index, dep_stmt_id, edge_weight);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ impl<'a> UsedIdentsVisitor<'a> {
}

impl Visit for UsedIdentsVisitor<'_> {
fn visit_import_decl(&mut self, _: &farmfe_core::swc_ecma_ast::ImportDecl) {
// do not visit children of import decl
}

fn visit_named_export(&mut self, n: &farmfe_core::swc_ecma_ast::NamedExport) {
if n.src.is_none() {
// make edge of `const a = 1; export { a }` be `a -> a`
Expand Down Expand Up @@ -100,19 +104,19 @@ impl Visit for UsedIdentsVisitor<'_> {
farmfe_core::swc_ecma_ast::DefaultDecl::Class(class_expr) => {
if let Some(ident) = &class_expr.ident {
self.with_ident(vec![ident.to_id()], |v| {
class_expr.class.visit_children_with(v);
class_expr.class.visit_with(v);
});
} else {
class_expr.class.visit_children_with(self);
class_expr.class.visit_with(self);
}
}
farmfe_core::swc_ecma_ast::DefaultDecl::Fn(fn_expr) => {
if let Some(ident) = &fn_expr.ident {
self.with_ident(vec![ident.to_id()], |v| {
fn_expr.function.visit_children_with(v);
fn_expr.function.visit_with(v);
});
} else {
fn_expr.function.visit_children_with(self);
fn_expr.function.visit_with(self);
}
}
farmfe_core::swc_ecma_ast::DefaultDecl::TsInterfaceDecl(_) => {}
Expand All @@ -123,7 +127,7 @@ impl Visit for UsedIdentsVisitor<'_> {
match n {
farmfe_core::swc_ecma_ast::Decl::Fn(n) => {
self.with_ident(vec![n.ident.to_id()], |v| {
n.function.visit_children_with(v);
n.function.visit_with(v);
});
}
farmfe_core::swc_ecma_ast::Decl::Var(n) => {
Expand All @@ -137,14 +141,14 @@ impl Visit for UsedIdentsVisitor<'_> {
.collect::<Vec<_>>();

self.with_ident(defined_idents, |v| {
init.visit_children_with(v);
init.visit_with(v);
});
}
}
}
farmfe_core::swc_ecma_ast::Decl::Class(n) => {
self.with_ident(vec![n.ident.to_id()], |v| {
n.class.visit_children_with(v);
n.class.visit_with(v);
});
}
_ => {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl<'a> VisitMut for UselessSpecifierRemover<'a> {

for (index, decl) in n.decls.iter_mut().enumerate() {
let mut defined_idents_collector = DefinedIdentsCollector::new();
decl.visit_with(&mut defined_idents_collector);
decl.name.visit_with(&mut defined_idents_collector);

if self
.used_defined_idents
Expand Down

0 comments on commit d960dd2

Please sign in to comment.