Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replace the "typeof __webpack_require__" content #1688

Merged
merged 6 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/mako/src/build/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ use crate::visitors::try_resolve::TryResolve;
use crate::visitors::ts_strip::ts_strip;
use crate::visitors::tsx_strip::tsx_strip;
use crate::visitors::virtual_css_modules::VirtualCSSModules;
use crate::visitors::webpack_require::WebpackRequire;
use crate::visitors::worker_module::WorkerModule;

pub struct Transform {}
Expand Down Expand Up @@ -93,6 +94,7 @@ impl Transform {
unresolved_mark,
}),
Box::new(WorkerModule::new(unresolved_mark)),
Box::new(WebpackRequire::new(unresolved_mark)),
];
if is_tsx {
visitors.push(Box::new(tsx_strip(
Expand Down
1 change: 1 addition & 0 deletions crates/mako/src/visitors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ pub(crate) mod try_resolve;
pub(crate) mod ts_strip;
pub(crate) mod tsx_strip;
pub(crate) mod virtual_css_modules;
pub(crate) mod webpack_require;
pub(crate) mod worker_module;
99 changes: 99 additions & 0 deletions crates/mako/src/visitors/webpack_require.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
use swc_core::common::{Mark, DUMMY_SP};
use swc_core::ecma::ast::{Expr, Ident, UnaryExpr};
use swc_core::ecma::visit::VisitMut;

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 struct WebpackRequire {
pub unresolved_mark: Mark,
}

impl WebpackRequire {
pub fn new(unresolved_mark: Mark) -> Self {
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
}
}

impl VisitMut for WebpackRequire {
// find the "typeof __webpack_require__" in the ast tree
fn visit_mut_unary_expr(&mut self, unary_expr: &mut UnaryExpr) {
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)
}
}
}

#[cfg(test)]
mod tests {
use swc_core::common::GLOBALS;
use swc_core::ecma::visit::VisitMutWith;

use super::WebpackRequire;
use crate::ast::tests::TestUtils;

#[test]
fn test_webpack_require_ident() {
assert_eq!(
run(r#"typeof __webpack_require__ === 'function';"#),
r#"typeof void 0 === 'function';"#
);
}

#[test]
fn test_webpack_module_ident() {
assert_eq!(run(r#"typeof __webpack_modules__;"#), r#"typeof void 0;"#);
}

fn run(js_code: &str) -> String {
let mut test_utils = TestUtils::gen_js_ast(js_code);
let ast = test_utils.ast.js_mut();
GLOBALS.set(&test_utils.context.meta.script.globals, || {
let mut visitor = WebpackRequire {
unresolved_mark: ast.unresolved_mark,
};
ast.ast.visit_mut_with(&mut visitor);
});
test_utils.js_ast_to_code()
}
}
Loading