Skip to content

Commit

Permalink
fix: win path problem with context module and require context (#1742)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc authored Jan 8, 2025
1 parent 5714d19 commit f2603f0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
3 changes: 2 additions & 1 deletion crates/mako/src/plugins/context_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use swc_core::ecma::ast::{
use swc_core::ecma::utils::{member_expr, quote_ident, quote_str, ExprExt, ExprFactory};
use swc_core::ecma::visit::{VisitMut, VisitMutWith};

use crate::ast::file::{Content, JsContent};
use crate::ast::file::{win_path, Content, JsContent};
use crate::ast::utils::{is_commonjs_require, is_dynamic_import};
use crate::ast::DUMMY_CTXT;
use crate::build::load::JS_EXTENSIONS;
Expand Down Expand Up @@ -120,6 +120,7 @@ module.exports = (id) => {{
"#,
key_values
.into_values()
.map(|v| win_path(v.as_str()))
.collect::<Vec<String>>()
.join(",\n")
);
Expand Down
5 changes: 3 additions & 2 deletions crates/mako/src/plugins/require_context/param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use swc_core::ecma::ast::{Expr, ExprOrSpread, Lit, Regex};
use thiserror::Error;

use super::VIRTUAL_REQUIRE_CONTEXT_MODULE;
use crate::ast::file::win_path;
use crate::compiler::Context;

#[derive(Debug)]
Expand All @@ -34,9 +35,9 @@ impl ContextParam {
Ok(format!(
"{}?root={}&sub={}&reg={}&mode={}&ig={}",
VIRTUAL_REQUIRE_CONTEXT_MODULE,
encode(relative_path.to_string_lossy().as_ref(),),
encode(win_path(relative_path.to_string_lossy().as_ref()).as_str()),
self.use_subdirectories,
encode(self.reg_expr.exp.as_ref()),
encode(win_path(self.reg_expr.exp.as_ref()).as_str()),
self.mode,
ignore_case,
))
Expand Down
38 changes: 26 additions & 12 deletions crates/mako/src/plugins/require_context/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use pathdiff::diff_paths;
use regex::RegexBuilder;

use super::param::ContextLoadMode;
use crate::ast::file::win_path;
use crate::compiler::Context;
use crate::module::ModuleId;

Expand All @@ -24,18 +25,30 @@ impl ContextLoadMode {
let mut map_str = String::from(r#"var _map_lazy = {"#);
map.iter().for_each(|(key, value)| match self {
ContextLoadMode::Sync => {
println!(
"... render_module_import1: {} -> {}",
win_path(key),
win_path(value)
);
map_str.push_str(&format!(
r#"
"{}": ()=> require("{}"),"#,
key, value
win_path(key),
win_path(value)
));
}

ContextLoadMode::Lazy => {
println!(
"... render_module_import2: {} -> {}",
win_path(key),
win_path(value)
);
map_str.push_str(&format!(
r#"
"{}": ()=> import("{}"),"#,
key, value
win_path(key),
win_path(value)
));
}
ContextLoadMode::Eager | ContextLoadMode::Weak | ContextLoadMode::LazyOnce => {
Expand Down Expand Up @@ -71,8 +84,8 @@ impl ContextLoadMode {
var e = new Error("Cannot find module '" + req + "'");
e.code = 'MODULE_NOT_FOUND';
return Promise.reject(e)
}}
}};
}}
}};
"#
.to_string(),
ContextLoadMode::Eager | ContextLoadMode::Weak | ContextLoadMode::LazyOnce => {
Expand Down Expand Up @@ -147,11 +160,12 @@ impl VirtualContextModuleRender {
pub fn module_id_map(&self, map: &BTreeMap<String, String>, context: &Arc<Context>) -> String {
let mut map_str = String::from(r#"var _map = {"#);
for (key, value) in map.iter() {
println!("... replace key: {} -> {}", key, win_path(key));
map_str.push_str(&format!(
r#"
"{}": "{}","#,
key,
ModuleId::from(value.as_str()).generate(context)
win_path(key),
win_path(ModuleId::from(value.as_str()).generate(context).as_str())
));
}
map_str.push_str("\n};\n");
Expand All @@ -175,24 +189,24 @@ impl VirtualContextModuleRender {
r#"
// context Map
{}
// context lazy require function Map
// context lazy require function Map
{}
// context require
{}
module.exports.resolve = function(req) {{
var r = _map[req];
if(r){{
return r
}}else{{
}}else{{
var e = new Error("Cannot find module '" + req + "'");
e.code = 'MODULE_NOT_FOUND';
throw e;
throw e;
}}
}};
module.exports.keys = function() {{ return Object.keys(_map); }}
module.exports.id = "{id}";
module.exports.keys = function() {{ return Object.keys(_map); }}
module.exports.id = "{id}";
"#,
self.module_id_map(&source_to_path, &context),
self.module_import(&source_to_path),
Expand Down

0 comments on commit f2603f0

Please sign in to comment.