Skip to content

Commit

Permalink
fix: globalThis attributes visit (#1254)
Browse files Browse the repository at this point in the history
* fix: globalThis attributes visit

* chore: add comments and unit test

* chore: extract common fn get_external_target_from_global_obj

* chore: remove duplicated comments
  • Loading branch information
xusd320 authored and sorrycc committed Jun 11, 2024
1 parent 61c0bca commit abdf785
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 19 deletions.
56 changes: 46 additions & 10 deletions crates/mako/src/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ fn get_external_target(
} else if external.starts_with("commonjs ") {
format!("require(\"{}\")", external.replace("commonjs ", ""))
} else {
format!("{}.{}", global_obj, external)
get_external_target_from_global_obj(global_obj, external)
},
None,
)),
Expand All @@ -99,7 +99,7 @@ fn get_external_target(
} else if config.module_type.as_ref().is_some_and(|t| t == "commonjs") {
format!("require(\"{}\")", config.root)
} else {
format!("{}.{}", global_obj, config.root)
get_external_target_from_global_obj(global_obj, &config.root)
},
config.script.clone(),
)),
Expand Down Expand Up @@ -180,7 +180,11 @@ fn get_external_target(
};
}
Some((
format!("{}.{}.{}", global_obj, advanced_config.root, replaced),
format!(
"{}.{}",
get_external_target_from_global_obj(global_obj, &advanced_config.root),
replaced
),
advanced_config.script.clone(),
))
}
Expand All @@ -193,6 +197,14 @@ fn get_external_target(
}
}

/*
* Can't use "globalThis.{xxx}" because "globalThis.@ant-design/icons"
* is syntax invalid
*/
fn get_external_target_from_global_obj(global_obj_name: &str, external: &str) -> String {
format!("{}['{}']", global_obj_name, external)
}

// TODO:
// - 支持物理缓存,让第二次更快
fn do_resolve(
Expand Down Expand Up @@ -498,6 +510,10 @@ mod tests {
"react".to_string(),
ExternalConfig::Basic("react".to_string()),
),
(
"@ant-design/icons".to_string(),
ExternalConfig::Basic("@ant-design/icons".to_string()),
),
("empty".to_string(), ExternalConfig::Basic("".to_string())),
]);
let x = external_resolve(
Expand All @@ -511,7 +527,27 @@ mod tests {
x,
(
"react".to_string(),
Some("(typeof globalThis !== 'undefined' ? globalThis : self).react".to_string()),
Some(
"(typeof globalThis !== 'undefined' ? globalThis : self)['react']".to_string()
),
None,
)
);
let x = external_resolve(
"test/resolve/normal",
None,
Some(&externals),
"index.ts",
"@ant-design/icons",
);
assert_eq!(
x,
(
"@ant-design/icons".to_string(),
Some(
"(typeof globalThis !== 'undefined' ? globalThis : self)['@ant-design/icons']"
.to_string()
),
None,
)
);
Expand Down Expand Up @@ -602,7 +638,7 @@ mod tests {
(
"antd/es/version".to_string(),
Some(
"(typeof globalThis !== 'undefined' ? globalThis : self).antd.version"
"(typeof globalThis !== 'undefined' ? globalThis : self)['antd'].version"
.to_string()
),
None,
Expand All @@ -623,7 +659,7 @@ mod tests {
(
"antd/es/date-picker".to_string(),
Some(
"(typeof globalThis !== 'undefined' ? globalThis : self).antd.DatePicker"
"(typeof globalThis !== 'undefined' ? globalThis : self)['antd'].DatePicker"
.to_string()
),
None,
Expand All @@ -634,7 +670,7 @@ mod tests {
(
"antd/es/input/Group".to_string(),
Some(
"(typeof globalThis !== 'undefined' ? globalThis : self).antd.Input.Group"
"(typeof globalThis !== 'undefined' ? globalThis : self)['antd'].Input.Group"
.to_string()
),
None,
Expand All @@ -647,7 +683,7 @@ mod tests {
(
"/path/to/node_modules/antd/es/button".to_string(),
Some(
"(typeof globalThis !== 'undefined' ? globalThis : self).antd.Button"
"(typeof globalThis !== 'undefined' ? globalThis : self)['antd'].Button"
.to_string()
),
None,
Expand All @@ -662,7 +698,7 @@ mod tests {
(
"/path/to/node_modules/[email protected]@antd/es/button".to_string(),
Some(
"(typeof globalThis !== 'undefined' ? globalThis : self).antd.Button"
"(typeof globalThis !== 'undefined' ? globalThis : self)['antd'].Button"
.to_string()
),
None,
Expand All @@ -674,7 +710,7 @@ mod tests {
(
"script".to_string(),
Some(
"(typeof globalThis !== 'undefined' ? globalThis : self).ScriptType"
"(typeof globalThis !== 'undefined' ? globalThis : self)['ScriptType']"
.to_string()
),
Some("https://example.com/lib/script.js".to_string()),
Expand Down
10 changes: 5 additions & 5 deletions e2e/fixtures/config.externals/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ assert.match(
content,
moduleReg(
"hoo",
"module.exports = \\(typeof globalThis !== 'undefined' \\? globalThis : self\\).hoo;"
"module.exports = \\(typeof globalThis !== 'undefined' \\? globalThis : self\\)\\['hoo'\\];"
),
"should have external module: hoo"
);
Expand Down Expand Up @@ -52,7 +52,7 @@ assert.match(
content,
moduleReg(
"antd/es/version",
"module.exports = \\(typeof globalThis !== 'undefined' \\? globalThis : self\\).antd.version;"
"module.exports = \\(typeof globalThis !== 'undefined' \\? globalThis : self\\)\\['antd'\\].version;"
),
"should external 1-level subpath"
);
Expand All @@ -61,7 +61,7 @@ assert.match(
content,
moduleReg(
"antd/es/date-picker",
"module.exports = \\(typeof globalThis !== 'undefined' \\? globalThis : self\\).antd.DatePicker;"
"module.exports = \\(typeof globalThis !== 'undefined' \\? globalThis : self\\)\\['antd'\\].DatePicker;"
),
"should external 1-level subpath with PascalCase"
);
Expand All @@ -70,7 +70,7 @@ assert.match(
content,
moduleReg(
"antd/es/input/Group",
"module.exports = \\(typeof globalThis !== 'undefined' \\? globalThis : self\\).antd.Input.Group;"
"module.exports = \\(typeof globalThis !== 'undefined' \\? globalThis : self\\)\\['antd'\\].Input.Group;"
),
"should external 2-level subpath with PascalCase"
);
Expand All @@ -79,7 +79,7 @@ assert.match(
content,
moduleReg(
"script",
"__mako_require__.loadScript\\('https://example.com/lib/script.js'[^]+\\(typeof globalThis !== 'undefined' \\? globalThis : self\\).ScriptType"
"__mako_require__.loadScript\\('https://example.com/lib/script.js'[^]+\\(typeof globalThis !== 'undefined' \\? globalThis : self\\)\\['ScriptType'\\]"
),
"should external in script mode"
);
Expand Down
4 changes: 0 additions & 4 deletions packages/mako/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@

/* auto-generated by NAPI-RS */

export interface TransformOutput {
code: string;
map?: string;
}
export interface JsHooks {
name?: string;
load?: (
Expand Down

0 comments on commit abdf785

Please sign in to comment.