Skip to content

Commit

Permalink
chore: πŸ€– pass more tree shaking tests (#4787)
Browse files Browse the repository at this point in the history
* chore: πŸ€– enable more tests

* chore: πŸ€– fix extends class

* chore: πŸ€– enable more test

* test: πŸ’ chore

enable more test

* chore: πŸ€– align with webpack

* chore: πŸ€– update test.config.js

* chore: πŸ€– update check.js

* chore: πŸ€– fmt

* chore: πŸ€– clean

* chore: πŸ€– update snapshot
  • Loading branch information
IWANABETHATGUY authored Nov 28, 2023
1 parent 07e0356 commit 8d6af5e
Show file tree
Hide file tree
Showing 84 changed files with 757 additions and 249 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ justfile
/webpack-test/ChangesAndRemovals
/webpack-test/**/dev-defaults.webpack.lock
!/webpack-test/**/target
!/webpack-test/cases/**/node_modules

/webpack-examples/**/dist

Expand Down
1 change: 1 addition & 0 deletions crates/rspack/tests/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const falsePositiveMap = {
"react-redux-like": "update dep",
"webpack-side-effects-all-used": "update dep",
"webpack-side-effects-simple-unused": "update dep",
"reexport-all-as-multi-level-nested": "update dep"
};

const normalizedList = filteredList.map((item) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ source: crates/rspack_testing/src/run_fixture.rs
"use strict";
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
a: function() { return a; },
aa: function() { return _aa__WEBPACK_IMPORTED_MODULE_0__; }
a: function() { return a; }
});
/* harmony import */var _aa__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./aa */"./package/autogen/aa.js");
function a() {}
function dddd() {}
Expand All @@ -29,9 +27,10 @@ __webpack_require__.d(__webpack_exports__, {
"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony import */var _package_src_index_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../package/src/index.js */"./package/autogen/a.js");
/* harmony import */var _package_src_index_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../package/src/index.js */"./package/autogen/aa.js");
_package_src_index_js__WEBPACK_IMPORTED_MODULE_0__.a;
_package_src_index_js__WEBPACK_IMPORTED_MODULE_0__.aa.aa;
_package_src_index_js__WEBPACK_IMPORTED_MODULE_1__.aa;
}),
},function(__webpack_require__) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
--- expected
+++ actual
@@ -25,31 +25,13 @@
@@ -7,10 +7,8 @@
"use strict";
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
- a: function() { return a; },
- aa: function() { return _aa__WEBPACK_IMPORTED_MODULE_0__; }
+ a: function() { return a; }
});
-/* harmony import */var _aa__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./aa */"./package/autogen/aa.js");

function a() {}
function dddd() {}
@@ -24,32 +22,15 @@
});
const aa = 3;
const cc = 3;
}),
-}),
-"./package/autogen/index.js": (function (__unused_webpack_module, __webpack_exports__, __webpack_require__) {
-"use strict";
-__webpack_require__.r(__webpack_exports__);
Expand All @@ -14,7 +27,7 @@
-
-
-
-}),
}),
-"./package/src/index.js": (function (__unused_webpack_module, __webpack_exports__, __webpack_require__) {
-"use strict";
-__webpack_require__.r(__webpack_exports__);
Expand All @@ -27,11 +40,12 @@
__webpack_require__.r(__webpack_exports__);
-/* harmony import */var _package_src_index_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../package/src/index.js */"./package/src/index.js");
+/* harmony import */var _package_src_index_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../package/src/index.js */"./package/autogen/a.js");
+/* harmony import */var _package_src_index_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../package/src/index.js */"./package/autogen/aa.js");

-_package_src_index_js__WEBPACK_IMPORTED_MODULE_0__.a.a;
-_package_src_index_js__WEBPACK_IMPORTED_MODULE_0__.a.aa.aa;
+_package_src_index_js__WEBPACK_IMPORTED_MODULE_0__.a;
+_package_src_index_js__WEBPACK_IMPORTED_MODULE_0__.aa.aa;
+_package_src_index_js__WEBPACK_IMPORTED_MODULE_1__.aa;
}),

},function(__webpack_require__) {
3 changes: 1 addition & 2 deletions crates/rspack_core/src/module_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,6 @@ impl ModuleGraph {
mut connection: ModuleGraphConnection,
module_identifier: ModuleIdentifier,
) -> ModuleGraphConnection {
let old_connection_module_id = connection.module_identifier;
let old_connection_original_module_id = connection.original_module_identifier;
let old_connection_dependency_id = connection.dependency_id;
let new_connection_id = ConnectionId::from(mg.connections.len());
Expand All @@ -655,7 +654,7 @@ impl ModuleGraph {
.insert(new_connection_id, old_connection_dependency_id);

let mgm = mg
.module_graph_module_by_identifier_mut(&old_connection_module_id)
.module_graph_module_by_identifier_mut(&module_identifier)
.expect("should have mgm");

mgm.add_incoming_connection(new_connection_id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ impl<'a> Visit for InnerGraphPlugin<'a> {
if is_pure {
self.set_symbol_if_is_top_level(symbol);
}
class.class.visit_with(self);
class.visit_with(self);
self.clear_symbol_if_is_top_level();
}
_ => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use std::collections::VecDeque;
use std::fmt::Debug;
use std::sync::Arc;

use async_trait::async_trait;
use once_cell::sync::Lazy;
use rspack_core::{Compilation, ConnectionState, ModuleGraph, Plugin, ResolvedExportInfoTarget};
use rspack_core::{
Compilation, ConnectionState, ModuleGraph, ModuleIdentifier, Plugin, ResolvedExportInfoTarget,
};
use rspack_error::Result;
use rspack_identifier::IdentifierSet;
use rustc_hash::FxHashSet as HashSet;
// use rspack_core::Plugin;
// use rspack_error::Result;
Expand Down Expand Up @@ -413,16 +417,12 @@ pub struct SideEffectsFlagPlugin;
#[async_trait]
impl Plugin for SideEffectsFlagPlugin {
async fn optimize_dependencies(&self, compilation: &mut Compilation) -> Result<Option<()>> {
// SAFETY: this method will not modify the map, and we can guarantee there is no other
// thread access the map at the same time.
let entries = compilation.entry_modules().collect::<Vec<_>>();
let mg = &mut compilation.module_graph;
let module_id_list = mg
.module_identifier_to_module
.keys()
.cloned()
.collect::<Vec<_>>();
for module_identifier in module_id_list {
let level_order_module_identifier = get_level_order_module_ids(mg, entries);
for module_identifier in level_order_module_identifier {
let mut module_chain = HashSet::default();
// dbg!(&module_identifier);
let Some(module) = mg.module_by_identifier(&module_identifier) else {
continue;
};
Expand Down Expand Up @@ -489,6 +489,7 @@ impl Plugin for SideEffectsFlagPlugin {
}

let ids = dep_id.get_ids(mg);
// dbg!(&ids);

if !ids.is_empty() {
let export_info_id = cur_exports_info_id.get_export_info(&ids[0], mg);
Expand Down Expand Up @@ -527,3 +528,30 @@ impl Plugin for SideEffectsFlagPlugin {
Ok(None)
}
}

fn get_level_order_module_ids(
mg: &ModuleGraph,
entries: Vec<ModuleIdentifier>,
) -> Vec<ModuleIdentifier> {
let mut res = vec![];
let mut visited = IdentifierSet::default();
for entry in entries {
let mut q = VecDeque::from_iter([entry]);
while let Some(mi) = q.pop_front() {
if visited.contains(&mi) {
continue;
} else {
visited.insert(mi);
res.push(mi);
}
let Some(m) = mg.module_by_identifier(&mi) else {
continue;
};
for con in mg.get_outgoing_connections(m) {
let mi = con.module_identifier;
q.push_back(mi);
}
}
}
res
}
4 changes: 4 additions & 0 deletions webpack-test/TestCases.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ const describeCases = config => {
mangleExports: true,
emitOnErrors: true,
concatenateModules: false,
innerGraph: true,
// TODO: size is not supported yet
// moduleIds: "size",
// chunkIds: "size",
Expand Down Expand Up @@ -219,6 +220,8 @@ const describeCases = config => {
backCompat: false,
// RSPACK exclusive: Rspack enables `css` by default.
// Turning off here to fallback to webpack's default css processing logic.

rspackFuture: testConfig?.experiments?.rspackFuture ?? {},
css: false,
...(config.module ? { outputModule: true } : {}),
},
Expand Down Expand Up @@ -285,6 +288,7 @@ const describeCases = config => {
);
infraStructureLog.length = 0;
const deprecationTracker = deprecationTracking.start();
console.log(options.experiments)
const webpack = require("@rspack/core").rspack;
webpack(options, err => {
deprecationTracker();
Expand Down
12 changes: 12 additions & 0 deletions webpack-test/cases/cjs-tree-shaking/cjs-to-esm/test.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**@type {import("@rspack/cli").Configuration} */
const config = {
experiments: {
rspackFuture: {
newTreeshaking: true,
},
},
builtins: {
treeShaking: false,
},
};
module.exports = config;
12 changes: 12 additions & 0 deletions webpack-test/cases/cjs-tree-shaking/esModule-getter/test.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**@type {import("@rspack/cli").Configuration} */
const config = {
experiments: {
rspackFuture: {
newTreeshaking: true,
},
},
builtins: {
treeShaking: false,
},
};
module.exports = config;
12 changes: 12 additions & 0 deletions webpack-test/cases/cjs-tree-shaking/esm-to-cjs/test.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**@type {import("@rspack/cli").Configuration} */
const config = {
experiments: {
rspackFuture: {
newTreeshaking: true,
},
},
builtins: {
treeShaking: false,
},
};
module.exports = config;
12 changes: 12 additions & 0 deletions webpack-test/cases/cjs-tree-shaking/exports/test.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**@type {import("@rspack/cli").Configuration} */
const config = {
experiments: {
rspackFuture: {
newTreeshaking: true,
},
},
builtins: {
treeShaking: false,
},
};
module.exports = config;
12 changes: 12 additions & 0 deletions webpack-test/cases/cjs-tree-shaking/importing/test.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**@type {import("@rspack/cli").Configuration} */
const config = {
experiments: {
rspackFuture: {
newTreeshaking: true,
},
},
builtins: {
treeShaking: false,
},
};
module.exports = config;
12 changes: 12 additions & 0 deletions webpack-test/cases/cjs-tree-shaking/mjs/test.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**@type {import("@rspack/cli").Configuration} */
const config = {
experiments: {
rspackFuture: {
newTreeshaking: true,
},
},
builtins: {
treeShaking: false,
},
};
module.exports = config;
12 changes: 12 additions & 0 deletions webpack-test/cases/cjs-tree-shaking/namespace/test.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**@type {import("@rspack/cli").Configuration} */
const config = {
experiments: {
rspackFuture: {
newTreeshaking: true,
},
},
builtins: {
treeShaking: false,
},
};
module.exports = config;
12 changes: 12 additions & 0 deletions webpack-test/cases/cjs-tree-shaking/non-root-this/test.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**@type {import("@rspack/cli").Configuration} */
const config = {
experiments: {
rspackFuture: {
newTreeshaking: true,
},
},
builtins: {
treeShaking: false,
},
};
module.exports = config;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**@type {import("@rspack/cli").Configuration} */
const config = {
experiments: {
rspackFuture: {
newTreeshaking: true,
},
},
builtins: {
treeShaking: false,
},
};
module.exports = config;
12 changes: 12 additions & 0 deletions webpack-test/cases/cjs-tree-shaking/objects/test.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**@type {import("@rspack/cli").Configuration} */
const config = {
experiments: {
rspackFuture: {
newTreeshaking: true,
},
},
builtins: {
treeShaking: false,
},
};
module.exports = config;
12 changes: 12 additions & 0 deletions webpack-test/cases/cjs-tree-shaking/parsing/test.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**@type {import("@rspack/cli").Configuration} */
const config = {
experiments: {
rspackFuture: {
newTreeshaking: true,
},
},
builtins: {
treeShaking: false,
},
};
module.exports = config;
12 changes: 12 additions & 0 deletions webpack-test/cases/cjs-tree-shaking/reexports/test.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**@type {import("@rspack/cli").Configuration} */
const config = {
experiments: {
rspackFuture: {
newTreeshaking: true,
},
},
builtins: {
treeShaking: false,
},
};
module.exports = config;
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

module.exports = () => {return "https://github.com/web-infra-dev/rspack/issues/4325"}


12 changes: 12 additions & 0 deletions webpack-test/cases/cjs-tree-shaking/transpiled/test.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**@type {import("@rspack/cli").Configuration} */
const config = {
experiments: {
rspackFuture: {
newTreeshaking: true,
},
},
builtins: {
treeShaking: false,
},
};
module.exports = config;
Loading

0 comments on commit 8d6af5e

Please sign in to comment.