Skip to content

Commit

Permalink
fix: namespace fallback when use literal computed (#2022)
Browse files Browse the repository at this point in the history
  • Loading branch information
shulandmimi authored Dec 24, 2024
1 parent 83fb24e commit 9e9e94e
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/cyan-boxes-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@farmfe/core": patch
---

fix namespace fallback when use literal computed
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const foo = 'foo';
export const bar = 'bar';
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as n1 from './foo';

const field = 'foo';

console.log(n1[field]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//index.js:
window['__farm_default_namespace__'] = {__FARM_TARGET_ENV__: 'browser'};function _interop_require_default(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}function _export_star(from, to) {
Object.keys(from).forEach(function(k) {
if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
Object.defineProperty(to, k, {
enumerable: true,
get: function() {
return from[k];
}
});
}
});
return from;
}function _interop_require_wildcard(obj, nodeInterop) {
if (!nodeInterop && obj && obj.__esModule) return obj;
if (obj === null || typeof obj !== "object" && typeof obj !== "function") return {
default: obj
};
var cache = _getRequireWildcardCache(nodeInterop);
if (cache && cache.has(obj)) return cache.get(obj);
var newObj = {
__proto__: null
};
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
for(var key in obj){
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
if (desc && (desc.get || desc.set)) Object.defineProperty(newObj, key, desc);
else newObj[key] = obj[key];
}
}
newObj.default = obj;
if (cache) cache.set(obj, newObj);
return newObj;
}function _getRequireWildcardCache(nodeInterop) {
if (typeof WeakMap !== "function") return null;
var cacheBabelInterop = new WeakMap();
var cacheNodeInterop = new WeakMap();
return (_getRequireWildcardCache = function(nodeInterop) {
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
})(nodeInterop);
}function __commonJs(mod) {
var module;
return () => {
if (module) {
return module.exports;
}
module = {
exports: {},
};
if(typeof mod === "function") {
mod(module, module.exports);
}else {
mod[Object.keys(mod)[0]](module, module.exports);
}
return module.exports;
};
}((function(){var index_js_cjs = __commonJs((module, exports)=>{
"use strict";
console.log('runtime/index.js');
window['__farm_default_namespace__'].__farm_module_system__.setPlugins([]);
});
index_js_cjs();
})());(function(_){var filename = ((function(){var _documentCurrentScript = typeof document !== "undefined" ? document.currentScript : null;return typeof document === "undefined" ? require("url").pathToFileURL(__filename).href : _documentCurrentScript && _documentCurrentScript.src || new URL("index_e094.js", document.baseURI).href})());for(var r in _){_[r].__farm_resource_pot__=filename;window['__farm_default_namespace__'].__farm_module_system__.register(r,_[r])}})({"6d686e48":function (module, exports, farmRequire, farmDynamicRequire) {
module._m(exports);
module.o(exports, "foo", function() {
return foo;
});
module.o(exports, "bar", function() {
return bar;
});
var foo = 'foo';
var bar = 'bar';
}
,
"b5d64806":function (module, exports, farmRequire, farmDynamicRequire) {
module._m(exports);
var _f_foo = module.w(farmRequire("6d686e48"));
var n1 = _f_foo;
const field = 'foo';
console.log(n1[field]);
}
,});window['__farm_default_namespace__'].__farm_module_system__.setInitialLoadedResources([]);window['__farm_default_namespace__'].__farm_module_system__.setDynamicModuleResourcesMap([],{ });var farmModuleSystem = window['__farm_default_namespace__'].__farm_module_system__;farmModuleSystem.bootstrap();var entry = farmModuleSystem.require("b5d64806");
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,16 @@ pub fn update_used_import_all_fields_of_edges(
.iter()
.find(|i| matches!(i, ImportSpecifierInfo::Namespace(_)));

if let Some(import_all_specifier) = import_all_specifier {
if let ImportSpecifierInfo::Namespace(ns) = import_all_specifier {
let mut used_import_all_fields_collector = UsedImportAllCollector::new(ns);
module_item.visit_with(&mut used_import_all_fields_collector);
let used_import_all_fields = used_import_all_fields_collector.used_import_all_fields;
if let Some(ImportSpecifierInfo::Namespace(ns)) = import_all_specifier {
let mut used_import_all_fields_collector = UsedImportAllCollector::new(ns);
module_item.visit_with(&mut used_import_all_fields_collector);
let used_import_all_fields = used_import_all_fields_collector.used_import_all_fields;

edge_weight
.used_import_all_fields
.entry(ns.clone())
.and_modify(|e| e.extend(used_import_all_fields.clone()))
.or_insert(used_import_all_fields);
}
edge_weight
.used_import_all_fields
.entry(ns.clone())
.and_modify(|e| e.extend(used_import_all_fields.clone()))
.or_insert(used_import_all_fields);
}
}
}
Expand Down Expand Up @@ -90,10 +88,13 @@ impl Visit for UsedImportAllCollector<'_> {
));
}
farmfe_core::swc_ecma_ast::MemberProp::Computed(computed_prop_name) => {
// if it is not a string literal, then the read value is dynamic and degrades to using all fields.
if let Expr::Lit(Lit::Str(str)) = &*computed_prop_name.expr {
self
.used_import_all_fields
.insert(UsedImportAllFields::LiteralComputed(str.value.to_string()));
} else {
self.used_import_all_fields.insert(UsedImportAllFields::All);
}
}
}
Expand Down

0 comments on commit 9e9e94e

Please sign in to comment.