-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support __webpack_public_path and __mako_public_path assignment (…
…#1441) * feat: support __webpack_public_path and __mako_public_path assignment * docs: update doc
- Loading branch information
Showing
9 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`); |
6 changes: 6 additions & 0 deletions
6
e2e/fixtures/config.public_path.with-assignment/mako.config.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
3
e2e/fixtures/config.public_path.with-assignment/src/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/'; |