Skip to content

Commit

Permalink
fix: ensure duplicated are excluded from child bundles
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane committed Nov 7, 2018
1 parent a208bb5 commit ed1a944
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
10 changes: 8 additions & 2 deletions rjs-parse/src/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub fn generate_modules(
&mut modules,
req_log,
&config.bundles,
&mut vec![],
vec!["requirejs/require".into()],
);
modules.to_vec()
Expand All @@ -64,13 +65,17 @@ pub fn collect<'a>(
modules: &'a mut Vec<BuildModule>,
req_log: &Vec<ModuleData>,
children: &Vec<ConfigItem>,
prev: &mut Vec<String>,
exclude: Vec<String>,
) {
children.iter().for_each(|conf_item| {
let mut include: Vec<String> = vec![];
req_log.iter().for_each(|item| {
if let Some(..) = conf_item.urls.iter().find(|x| **x == item.referrer) {
include.push(create_entry_point(&item));
let next_id = create_entry_point(&item);
if let None = prev.iter().find(|x| **x == next_id) {
include.push(next_id);
}
}
});
include.sort();
Expand All @@ -84,7 +89,8 @@ pub fn collect<'a>(
modules.push(this_item);
let mut exclude = exclude.clone();
exclude.push(conf_item.name.to_string());
collect(modules, req_log, &conf_item.children, exclude);
prev.extend(include);
collect(modules, req_log, &conf_item.children, prev, exclude);
});
}

Expand Down
58 changes: 56 additions & 2 deletions rjs-parse/tests/e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extern crate rjs;
extern crate from_file;
extern crate serde_json;

use rjs::{RequireJsBuildConfig, RequireJsClientConfig, bundle_config};
use rjs::{RequireJsBuildConfig, bundle_config};

#[test]
fn test_all_strings() {
Expand All @@ -16,14 +16,48 @@ fn test_all_strings() {
})();
"#;
let bundle_config = r#"
{"bundles": [{"name": "main", "children": [], "urls": ["/"]}]}
{
"bundles": [
{
"name": "main",
"urls": ["/"],
"children": [
{
"name": "other",
"urls": ["/about"],
"children": []
},
{
"name": "product",
"urls": ["/product"],
"children": []
}
]
}
]
}
"#;
let req_log = r#"
[
{
"url": "https://example.com/jquery",
"id": "jquery",
"referrer": "/"
},
{
"url": "https://example.com/jquery",
"id": "jquery",
"referrer": "/about"
},
{
"url": "https://example.com/jquery-ui",
"id": "jquery-ui",
"referrer": "/about"
},
{
"url": "https://example.com/gallery.js",
"id": "gallery",
"referrer": "/product"
}
]
"#;
Expand Down Expand Up @@ -57,6 +91,24 @@ fn test_all_strings() {
"requirejs/require"
],
"create": true
},
{
"name": "other",
"include": ["jquery-ui"],
"exclude": [
"requirejs/require",
"main"
],
"create": true
},
{
"name": "product",
"include": ["gallery"],
"exclude": [
"requirejs/require",
"main"
],
"create": true
}
]
}
Expand All @@ -72,5 +124,7 @@ fn test_all_strings() {
let actual_as_value: serde_json::Value = serde_json::from_str(&as_string).expect("serde actual");
let expected_as_value: serde_json::Value = serde_json::from_str(&expected).expect("serde expected");

// println!("{}", as_string);

assert_eq!(actual_as_value, expected_as_value);
}

0 comments on commit ed1a944

Please sign in to comment.