Skip to content

Commit

Permalink
fix: 增加对typeof 表达式的处理
Browse files Browse the repository at this point in the history
  • Loading branch information
shikuan.sk committed Dec 5, 2024
1 parent 9e57431 commit 02e9ce5
Showing 1 changed file with 28 additions and 38 deletions.
66 changes: 28 additions & 38 deletions crates/mako/src/visitors/webpack_require.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
use swc_core::common::{Mark, DUMMY_SP};
use swc_core::ecma::ast::{Expr, Ident, UnaryExpr};
use swc_core::ecma::visit::VisitMut;
use swc_core::ecma::visit::{VisitMut, VisitMutWith};

const WEBPACK_REQUIRE: &str = "__webpack_require__";
const WEBPACK_HASH: &str = "__webpack_hash__";
const WEBPACK_LAYER: &str = "__webpack_layer__";
const WEBPACK_PUBLIC_PATH: &str = "__webpack_public_path__";
const WEBPACK_MODULES: &str = "__webpack_modules__";
const WEBPACK_MODULE: &str = "__webpack_module__";
const WEBPACK_CHUNK_LOAD: &str = "__webpack_chunk_load__";
const WEBPACK_BASE_URI: &str = "__webpack_base_uri__";
const NON_WEBPACK_REQUIRE: &str = "__non_webpack_require__";
const SYSTEM_CONTEXT: &str = "__system_context__";
const WEBPACK_SHARE_SCOPES: &str = "__webpack_share_scopes__";
const WEBPACK_INIT_SHARING: &str = "__webpack_init_sharing__";
const WEBPACK_NONCE: &str = "__webpack_nonce__";
const WEBPACK_CHUNK_NAME: &str = "__webpack_chunkname__";
const WEBPACK_RUNTIME_ID: &str = "__webpack_runtime_id__";
const WEBPACK_GET_SCRIPT_FILENAME: &str = "__webpack_get_script_filename__";
pub const WEBPACK_VALUES: [&str; 14] = [
"__webpack_get_script_filename__",
"__webpack_runtime_id__",
"__webpack_chunkname__",
"__webpack_nonce__",
"__webpack_init_sharing__",
"__webpack_share_scopes__",
"__system_context__",
"__non_webpack_require__",
"__webpack_require__",
"__webpack_hash__",
"__webpack_modules__",
"__webpack_module__",
"__webpack_chunk_load__",
"__webpack_base_uri__",
];

pub struct WebpackRequire {
pub unresolved_mark: Mark,
Expand All @@ -28,38 +28,21 @@ impl WebpackRequire {
Self { unresolved_mark }
}
fn is_ident_webpack(&self, ident: &Ident, unresolved_mark: &Mark) -> bool {
[
WEBPACK_REQUIRE,
WEBPACK_HASH,
WEBPACK_LAYER,
WEBPACK_PUBLIC_PATH,
WEBPACK_MODULES,
WEBPACK_MODULE,
WEBPACK_CHUNK_LOAD,
WEBPACK_BASE_URI,
NON_WEBPACK_REQUIRE,
SYSTEM_CONTEXT,
WEBPACK_SHARE_SCOPES,
WEBPACK_INIT_SHARING,
WEBPACK_NONCE,
WEBPACK_CHUNK_NAME,
WEBPACK_RUNTIME_ID,
WEBPACK_GET_SCRIPT_FILENAME,
]
.iter()
.any(|&i| i == &ident.sym)
&& ident.ctxt.outer() == *unresolved_mark
WEBPACK_VALUES.iter().any(|&i| i == &ident.sym) && ident.ctxt.outer() == *unresolved_mark
}
}

impl VisitMut for WebpackRequire {
// find the "typeof __webpack_require__" in the ast tree
fn visit_mut_unary_expr(&mut self, unary_expr: &mut UnaryExpr) {
println!("aaaa{:?}", unary_expr.op.as_str());
if unary_expr.op.as_str() == "typeof"
&& let Some(arg_ident) = unary_expr.arg.as_ident()
&& self.is_ident_webpack(arg_ident, &self.unresolved_mark)
{
unary_expr.arg = Expr::undefined(DUMMY_SP)
} else {
unary_expr.visit_mut_children_with(self);
}
}
}
Expand All @@ -84,6 +67,13 @@ mod tests {
fn test_webpack_module_ident() {
assert_eq!(run(r#"typeof __webpack_modules__;"#), r#"typeof void 0;"#);
}
#[test]
fn test_dbcheck_webpack_module_ident() {
assert_eq!(
run(r#"typeof typeof __webpack_modules__;"#),
r#"typeof typeof void 0;"#
);
}

fn run(js_code: &str) -> String {
let mut test_utils = TestUtils::gen_js_ast(js_code);
Expand Down

0 comments on commit 02e9ce5

Please sign in to comment.