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

feat: support __webpack_public_path and __mako_public_path assignment #1441

Merged
merged 2 commits into from
Jul 23, 2024
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
2 changes: 2 additions & 0 deletions crates/mako/src/build/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use crate::visitors::fix_symbol_conflict::FixSymbolConflict;
use crate::visitors::import_template_to_string_literal::ImportTemplateToStringLiteral;
use crate::visitors::new_url_assets::NewUrlAssets;
use crate::visitors::provide::Provide;
use crate::visitors::public_path_assignment::PublicPathAssignment;
use crate::visitors::react::react;
use crate::visitors::try_resolve::TryResolve;
use crate::visitors::ts_strip::ts_strip;
Expand Down Expand Up @@ -131,6 +132,7 @@ impl Transform {
context: context.clone(),
unresolved_mark,
}));
visitors.push(Box::new(PublicPathAssignment {}));
// TODO: refact provide
visitors.push(Box::new(Provide::new(
context.config.providers.clone(),
Expand Down
4 changes: 3 additions & 1 deletion crates/mako/src/plugins/invalid_webpack_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ impl<'a> Visit for InvalidSyntaxVisitor<'a> {
}
fn visit_ident(&mut self, n: &Ident) {
// why keep __webpack_nonce__? since styled-components is using it
let is_webpack_prefix = n.sym.starts_with("__webpack_") && &n.sym != "__webpack_nonce__";
let is_webpack_prefix = n.sym.starts_with("__webpack_")
&& &n.sym != "__webpack_nonce__"
&& &n.sym != "__webpack_public_path__";
let has_binding = n.span.ctxt.outer() != self.unresolved_mark;
if is_webpack_prefix && !has_binding {
self.handler
Expand Down
1 change: 1 addition & 0 deletions crates/mako/src/visitors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub(crate) mod meta_url_replacer;
pub(crate) mod new_url_assets;
pub(crate) mod optimize_define_utils;
pub(crate) mod provide;
pub(crate) mod public_path_assignment;
pub(crate) mod react;
pub(crate) mod try_resolve;
pub(crate) mod ts_strip;
Expand Down
54 changes: 54 additions & 0 deletions crates/mako/src/visitors/public_path_assignment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use swc_core::common::DUMMY_SP;
use swc_core::ecma::ast::{AssignExpr, AssignOp, Pat, PatOrExpr};
use swc_core::ecma::utils::member_expr;
use swc_core::ecma::visit::{VisitMut, VisitMutWith};

pub struct PublicPathAssignment {}

impl VisitMut for PublicPathAssignment {
fn visit_mut_assign_expr(&mut self, n: &mut AssignExpr) {
if n.op == AssignOp::Assign {
if let PatOrExpr::Pat(box Pat::Ident(ident)) = &n.left {
let sym = ident.sym.as_ref();
if sym == "__webpack_public_path__" || sym == "__mako_public_path__" {
*n = AssignExpr {
left: PatOrExpr::Expr(member_expr!(DUMMY_SP, __mako_require__.publicPath)),
..n.clone()
};
}
}
}
n.visit_mut_children_with(self);
}
}

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

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

#[test]
fn test_normal() {
assert_eq!(
run(r#"__webpack_public_path__ = '/foo/';"#),
r#"__mako_require__.publicPath = '/foo/';"#.trim()
);
assert_eq!(
run(r#"__mako_public_path__ = '/foo/';"#),
r#"__mako_require__.publicPath = '/foo/';"#.trim()
);
}

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 = PublicPathAssignment {};
ast.ast.visit_mut_with(&mut visitor);
});
test_utils.js_ast_to_code()
}
}
6 changes: 6 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,12 @@ Buffer;

publicPath configuration. Note: There is a special value `"runtime"`, which means that it will switch to runtime mode and use the runtime `window.publicPath` as publicPath.

If you want to set the `publicPath` in the runtime, use `__mako_public_path__`. (Notice: `__webpack_public_path__` is also supported)

```ts
__mako_public_path__ = '/foo/';
```

### px2rem

- Type: `false | { root?: number, propBlackList?: string[], propWhiteList?: string[], selectorBlackList?: string[], selectorWhiteList?: string[], selectorDoubleList?: string[], minPixelValue?: number }`
Expand Down
6 changes: 6 additions & 0 deletions docs/config.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,12 @@ Buffer;

publicPath 配置。注意:有一个特殊值 `"runtime"`,这意味着它将切换到运行时模式并使用运行时的 `window.publicPath` 作为 publicPath。

如果你想在运行时设置 `publicPath`,请使用 `__mako_public_path__`。(注:`__webpack_public_path__` 也是支持的)

```ts
__mako_public_path__ = '/foo/';
```

### px2rem

- 类型:`false | { root?: number, propBlackList?: string[], propWhiteList?: string[], selectorBlackList?: string[], selectorWhiteList?: string[], selectorDoubleList?: string[], minPixelValue?: number }`
Expand Down
8 changes: 8 additions & 0 deletions e2e/fixtures/config.public_path.with-assignment/expect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const assert = require("assert");
const { parseBuildResult, trim, moduleReg } = require("../../../scripts/test-utils");
const { files } = parseBuildResult(__dirname);

const content = files["index.js"];

assert(content.includes(`__mako_require__.publicPath = '/foo/'`), `__webpack_public_path__ works`);
assert(content.includes(`__mako_require__.publicPath = '/bar/'`), `__mako_public_path__ works`);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"publicPath": "/foooooo/",
"mode": "production",
"minify": false,
"hmr": false
}
3 changes: 3 additions & 0 deletions e2e/fixtures/config.public_path.with-assignment/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
console.log(1);
__webpack_public_path__ = '/foo/';
__mako_public_path__ = '/bar/';
Loading