Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/honest-rabbits-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rspack/binding": patch
---

feat(runtime): support set **webpack_public_path** runtime
32 changes: 30 additions & 2 deletions crates/rspack_plugin_javascript/src/visitors/dependency/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use rspack_regex::RspackRegex;
use sugar_path::SugarPath;
use swc_core::common::{pass::AstNodePath, Mark, SyntaxContext};
use swc_core::ecma::ast::{
BinExpr, BinaryOp, CallExpr, Callee, Expr, ExprOrSpread, Ident, Lit, MemberExpr, MemberProp,
MetaPropExpr, MetaPropKind, ModuleDecl, NewExpr, Tpl,
AssignExpr, AssignOp, BinExpr, BinaryOp, CallExpr, Callee, Expr, ExprOrSpread, Ident, Lit,
MemberExpr, MemberProp, MetaPropExpr, MetaPropKind, ModuleDecl, NewExpr, Pat, PatOrExpr, Tpl,
};
use swc_core::ecma::atoms::js_word;
use swc_core::ecma::utils::{quote_ident, quote_str};
Expand Down Expand Up @@ -300,6 +300,34 @@ impl VisitAstPath for DependencyScanner<'_> {
expr: &'ast Expr,
ast_path: &mut AstNodePath<AstParentNodeRef<'r>>,
) {
if let Expr::Assign(AssignExpr {
op: AssignOp::Assign,
left: PatOrExpr::Pat(box Pat::Ident(ident)),
..
}) = expr
{
// variable can be assigned
if ident.span.ctxt == self.unresolved_ctxt {
#[allow(clippy::single_match)]
match ident.sym.as_ref() as &str {
WEBPACK_PUBLIC_PATH => {
let mut new_expr = expr.clone();
if let Some(e) = new_expr.as_mut_assign() {
e.left = PatOrExpr::Pat(box Pat::Ident(
quote_ident!(RuntimeGlobals::PUBLIC_PATH).into(),
))
};
self.add_presentational_dependency(box ConstDependency::new(
new_expr,
Some(RuntimeGlobals::PUBLIC_PATH),
as_parent_path(ast_path),
));
}
_ => {}
}
}
}

if let Expr::Ident(ident) = expr {
if ident.span.ctxt == self.unresolved_ctxt {
match ident.sym.as_ref() as &str {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
it("__webpack_public_path__", function () {
expect(typeof __webpack_public_path__).toBe("string");
expect(__webpack_public_path__).toBe("/");
__webpack_public_path__ = "/a";
expect(__webpack_public_path__).toBe("/a");
const a = __webpack_public_path__;
expect(a).toBe("/a");
expect(__webpack_require__.p).toBe("/a");
});

it("__webpack_public_path__ use as local varable", function () {
var __webpack_public_path__ = "/test";
// __webpack_require__.p set by prev test
expect(__webpack_require__.p).toBe("/a");
expect(__webpack_public_path__).toBe("/test");
});