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: win path problem with context module and require context #1742

Merged
merged 1 commit into from
Jan 8, 2025
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
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!(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debug print statements like println!("... render_module_import1: {} -> {}", win_path(key), win_path(value)); should be removed or replaced with proper logging before merging, as they can clutter the output and are not suitable for production code.

"... 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!(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debug print statements like println!("... render_module_import2: {} -> {}", win_path(key), win_path(value)); should be removed or replaced with proper logging before merging, as they can clutter the output and are not suitable for production code.

"... 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));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debug print statements like println!("... replace key: {} -> {}", key, win_path(key)); should be removed or replaced with proper logging before merging, as they can clutter the output and are not suitable for production code.

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
Loading