Skip to content

Commit

Permalink
fix: tree shake issues
Browse files Browse the repository at this point in the history
  • Loading branch information
wre232114 committed Jun 3, 2024
1 parent de7017b commit 7fbc408
Show file tree
Hide file tree
Showing 22 changed files with 835 additions and 66 deletions.
459 changes: 459 additions & 0 deletions Cargo.lock

Large diffs are not rendered by default.

29 changes: 14 additions & 15 deletions crates/compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,21 +128,20 @@ impl Compiler {
{
#[cfg(feature = "profile")]
farmfe_core::puffin::profile_scope!("Build Stage");
let write_module_cache = || {
if self.context.config.persistent_cache.enabled() {
self.context.cache_manager.module_cache.write_cache();
}
};
self
.build()
.map(|v| {
write_module_cache();
v
})
.map_err(|err| {
write_module_cache();
err
})?;
// let write_module_cache = || {
// if self.context.config.persistent_cache.enabled() {
// self.context.cache_manager.module_cache.write_cache();
// }
// };
self.build()?;
// .map(|v| {
// write_module_cache();
// v
// })
// .map_err(|err| {
// write_module_cache();
// err
// })?;
}

{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export const routes = import.meta.glob('./modules/*.ts', {
eager: true,
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { routes } from './dep'

console.log(routes)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export default {
path: "/vue-flow",
redirect: "/vue-flow/index",
meta: {
icon: "ep:set-up",
title: "vue-flow",
},
children: [
{
path: "/vue-flow/index",
name: "VueFlow",
meta: {
title: "vue-flow",
extraIcon: "IF-pure-iconfont-new svg"
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//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:{}};t[e]=i;r[e](i,i.exports,o,n);return i.exports}o(e)})({"d2214aaa":function (module, exports, farmRequire, farmDynamicRequire) {
console.log("runtime/index.js")(globalThis || window || global)["__farm_default_namespace__"].__farm_module_system__.setPlugins([]);
}
,},"d2214aaa");(function(_){for(var r in _){_[r].__farm_resource_pot__='index_de21.js';(globalThis || window || global)['__farm_default_namespace__'].__farm_module_system__.register(r,_[r])}})({"05ee5ec7":function (module, exports, farmRequire, farmDynamicRequire) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "routes", {
enumerable: true,
get: function() {
return routes;
}
});
var _interop_require_wildcard = farmRequire("@swc/helpers/_/_interop_require_wildcard");
var _depts = _interop_require_wildcard._(farmRequire("edfa0cee"));
const routes = {
"./modules/dep.ts": _depts
};
}
,
"b5d64806":function (module, exports, farmRequire, farmDynamicRequire) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _dep = farmRequire("05ee5ec7");
console.log(_dep.routes);
}
,
"edfa0cee":function (module, exports, farmRequire, farmDynamicRequire) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "default", {
enumerable: true,
get: function() {
return _default;
}
});
var _default = {
path: "/vue-flow",
redirect: "/vue-flow/index",
meta: {
icon: "ep:set-up",
title: "vue-flow"
},
children: [
{
path: "/vue-flow/index",
name: "VueFlow",
meta: {
title: "vue-flow",
extraIcon: "IF-pure-iconfont-new svg"
}
}
]
};
}
,});(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");
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function $e() {
return {
seed: (e) => {
return e;
}
}
}


var F = class {
constructor(e = {}){
let { randomizer: r = $e() } = e;
this._randomizer = r;
}
get defaultRefDate() {
return this._defaultRefDate;
}
setDefaultRefDate(e = ()=>new Date) {
typeof e == "function" ? this._defaultRefDate = e : this._defaultRefDate = ()=>new Date(e);
}
seed(e = Math.ceil(Math.random() * Number.MAX_SAFE_INTEGER)) {
return this._randomizer.seed(e), e;
}
}, Yt = new F;

export { F as default, Yt as R };
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Dep from "./dep.mjs";
import { useFullscreen, document1 } from "./object-assign.mjs";

console.log(Dep, useFullscreen, document1);
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const defaultDocument = globalThis.isClient ? window.document : undefined;
const defaultWindow = globalThis.isClient ? window : undefined;
const F = {};

function useFullscreen(target, options = {}) {
const {
document = defaultDocument,
autoExit = false
} = options;

return document;
}

const { document: { document1 } = defaultWindow.document } = F;

export {
useFullscreen,
document1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//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:{}};t[e]=i;r[e](i,i.exports,o,n);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_f1d9.js';(globalThis || window || global)['__farm_default_namespace__'].__farm_module_system__.register(r,_[r])}})({"4312d062":function (module, exports, farmRequire, farmDynamicRequire) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "default", {
enumerable: true,
get: function() {
return F;
}
});
function $e() {
return {
seed: (e)=>{
return e;
}
};
}
var F = class {
constructor(e = {}){
let { randomizer: r = $e() } = e;
this._randomizer = r;
}
get defaultRefDate() {
return this._defaultRefDate;
}
setDefaultRefDate(e = ()=>new Date) {
typeof e == "function" ? this._defaultRefDate = e : this._defaultRefDate = ()=>new Date(e);
}
seed(e = Math.ceil(Math.random() * Number.MAX_SAFE_INTEGER)) {
return this._randomizer.seed(e), e;
}
}, Yt = new F;
}
,
"81077a1f":function (module, exports, farmRequire, farmDynamicRequire) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
function _export(target, all) {
for(var name in all)Object.defineProperty(target, name, {
enumerable: true,
get: all[name]
});
}
_export(exports, {
document1: function() {
return document1;
},
useFullscreen: function() {
return useFullscreen;
}
});
const defaultDocument = globalThis.isClient ? window.document : undefined;
const defaultWindow = globalThis.isClient ? window : undefined;
const F = {};
function useFullscreen(target, options = {}) {
const { document = defaultDocument, autoExit = false } = options;
return document;
}
const { document: { document1 } = defaultWindow.document } = F;
}
,
"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("4312d062"));
var _objectassign = farmRequire("81077a1f");
console.log(_dep.default, _objectassign.useFullscreen, _objectassign.document1);
}
,});(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");
2 changes: 1 addition & 1 deletion crates/compiler/tests/tree_shake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ mod common;
#[test]
fn tree_shake_test() {
fixture!(
"tests/fixtures/tree_shake/**/index.ts",
"tests/fixtures/tree_shake/self-executed/new-call/**/index.ts",
|file, crate_path| {
let cwd = file.parent().unwrap();
println!("testing tree shake: {:?}", cwd);
Expand Down
1 change: 1 addition & 0 deletions crates/plugin_script/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ swc_ecma_loader = { version = "0.45.10", features = [
"tsc",
], optional = true }
once_cell = { version = "1.10.0", optional = true }
wasmer-wasix = { version = "0.9.0" }

[features]
swc_plugin = [
Expand Down
4 changes: 3 additions & 1 deletion crates/plugin_script/src/swc_plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub static PLUGIN_MODULE_CACHE: Lazy<Mutex<HashMap<String, Box<CompiledPluginMod
Lazy::new(Default::default);
pub static CACHING_RESOLVER: Lazy<CachingResolver<NodeModulesResolver>> =
Lazy::new(|| CachingResolver::new(40, NodeModulesResolver::default()));
pub static PLUGIN_RUNTIME: Lazy<Option<Arc<dyn wasmer_wasix::Runtime + Send + Sync>>> =
Lazy::new(|| swc_plugin_runner::wasix_runtime::build_wasi_runtime(None));

pub fn init_plugin_module_cache_once(config: &farmfe_core::config::Config) {
for plugin_config in config.script.plugins.iter() {
Expand Down Expand Up @@ -183,7 +185,7 @@ impl RustPlugins {
.expect("plugin module should be cached")
.clone();
let plugin_name = plugin_module_bytes.get_module_name().to_string();
let runtime = swc_plugin_runner::wasix_runtime::build_wasi_runtime(None);
let runtime = PLUGIN_RUNTIME.clone();

let mut plugin_transform_executor = swc_plugin_runner::create_plugin_transform_executor(
&self.source_map,
Expand Down
11 changes: 10 additions & 1 deletion crates/plugin_tree_shake/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ pub enum UsedExportsIdent {
/// Marked as [UsedExportsIdent::ExportAll] when all potential exports of `export * from 'xxx'` are used.
/// Usually works for side effect module(e.g. entry module) that re-export other modules
ExportAll,
/// All idents are used and may be imported from other module
/// Marked as [UsedExportsIdent::ImportAll] when `import * as xx from 'xxx'` are used.
ImportAll,
}

impl UsedExportsIdent {
Expand Down Expand Up @@ -50,6 +53,7 @@ impl ToString for UsedExportsIdent {
UsedExportsIdent::SwcIdent(ident) => ident.to_string(),
UsedExportsIdent::Default => "default".to_string(),
UsedExportsIdent::ExportAll => "*".to_string(),
UsedExportsIdent::ImportAll => "import_*_as".to_string(),
}
}
}
Expand Down Expand Up @@ -287,9 +291,14 @@ impl TreeShakeModule {
}
UsedExports::Partial(idents) => {
let mut used_idents = vec![];
// statement `import * as xxx from './xxx'` is marked as used, we need to mark all exported idents as used the same as UsedExports::All
if idents.contains(&UsedExportsIdent::ImportAll) {
// all export information needs to be collected
return self.all_exports_to_statement_idents(true);
}
// statement `export * from './xxx'` is marked as used, we need to mark all exported idents as used the same as UsedExports::All
// except the default export when idents does not contain default export
if idents.contains(&UsedExportsIdent::ExportAll) {
// for all content introduced, all export information needs to be collected
return self.all_exports_to_statement_idents(idents.contains(&UsedExportsIdent::Default));
}
// find exported ident for every used idents.
Expand Down
2 changes: 1 addition & 1 deletion crates/plugin_tree_shake/src/statement_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ impl Statement {
}
}

#[derive(Debug)]
#[derive(Debug, Default)]
pub struct StatementGraphEdge {
/// used idents of the dependency statement, for example:
/// ```js
Expand Down
Loading

0 comments on commit 7fbc408

Please sign in to comment.