Skip to content

Commit

Permalink
Fix/context module source with first empty quasis (#1719)
Browse files Browse the repository at this point in the history
* test: ✅ add first empty quasis template string module source case

* fix: 🐛 first emtpy quasis case
  • Loading branch information
stormslowly authored Dec 12, 2024
1 parent 75c356d commit ae5e6e3
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 45 deletions.
49 changes: 32 additions & 17 deletions crates/mako/src/plugins/context_module.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::{BTreeMap, HashSet};
use std::fs;
use std::path::Path;
use std::sync::Arc;
Expand Down Expand Up @@ -38,25 +39,34 @@ impl Plugin for ContextModulePlugin {
let glob_pattern = param.file.pathname.clone().join(glob_pattern);
let paths = glob(glob_pattern.to_str().unwrap())?;

let mut key_values = vec![];
let mut key_values = BTreeMap::new();
let load_by = if param.file.has_param("async") {
"import"
} else {
"require"
};

for path in paths {
let path = path?;
let rlt_path = path.strip_prefix(&param.file.pathname)?;
let is_file = path.is_file();

// full path `./i18n/jzh_CN.json`
let mut keys = HashSet::new();

// full path `./i18n/zh_CN.json`
let mut keys: Vec<String> = vec![];
let metadata = fs::metadata(&path);
if let Ok(md) = metadata {
if md.is_dir() && !has_index_file_in_directory(&path) {
continue;
}
}
keys.push(format!("./{}", rlt_path.to_string_lossy()));
keys.insert(format!("./{}", rlt_path.to_string_lossy()));
// omit ext `./i18n/zh_CN`
if let Some(ext) = rlt_path.extension() {
if get_module_extensions().contains(&format!(".{}", ext.to_string_lossy())) {
keys.push(format!(
if is_file
&& get_module_extensions().contains(&format!(".{}", ext.to_string_lossy()))
{
keys.insert(format!(
"./{}",
rlt_path.with_extension("").to_string_lossy()
));
Expand Down Expand Up @@ -85,16 +95,11 @@ impl Plugin for ContextModulePlugin {
}
}

let is_async = param.file.has_param("async");

for key in keys {
let load_by = if is_async { "import" } else { "require" };
key_values.push(format!(
"'{}': () => {}('{}')",
key,
load_by,
path.to_string_lossy()
));
let map_entry =
format!("'{}': () => {}('{}')", key, load_by, path.to_string_lossy());

key_values.insert(key, map_entry);
}
}

Expand All @@ -113,7 +118,10 @@ module.exports = (id) => {{
}}
}};
"#,
key_values.join(",\n")
key_values
.into_values()
.collect::<Vec<String>>()
.join(",\n")
);
Ok(Some(Content::Js(JsContent {
content,
Expand Down Expand Up @@ -245,9 +253,16 @@ fn try_replace_context_arg(
}

// handle `./foo/${bar}.ext`
// `${bar}` will be handle as `./${bar}`
Expr::Tpl(tpl) => {
if !tpl.exprs.is_empty() {
let pre_quasis = tpl.quasis.first().unwrap().raw.to_string();
let first_quasis_str = tpl.quasis.first().unwrap().raw.to_string();
let pre_quasis = if first_quasis_str.is_empty() {
"./".to_string()
} else {
first_quasis_str
};

let (prefix, remainder) = if let Some(pos) = pre_quasis.rfind('/') {
(
pre_quasis[..=pos].to_string(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const assert = require("assert");
const { parseBuildResult, moduleReg } = require("../../../scripts/test-utils");
const { files } = parseBuildResult(__dirname);
const {parseBuildResult, moduleReg} = require("../../../scripts/test-utils");
const {files} = parseBuildResult(__dirname);

const names = Object.keys(files);
const content = files["index.js"];
Expand All @@ -11,26 +11,30 @@ assert.match(
moduleReg(
"src\\?context&glob=\\*\\*/\\*",
[
"'./ext/json.ts': ()=>__mako_require__(\"src/ext/json.ts\")",
"'./ext/json': ()=>__mako_require__(\"src/ext/json.ts\")",
"'./fake.js': ()=>__mako_require__(\"src/fake.js/index.js\")",
"'./fake': ()=>__mako_require__(\"src/fake.js/index.js\")",
"'./fake.js/a.js': ()=>__mako_require__(\"src/fake.js/a.js\")",
"'./fake.js/a': ()=>__mako_require__(\"src/fake.js/a.js\")",
"'./fake.js/aa.js': ()=>__mako_require__(\"src/fake.js/aa.js\")",
"'./fake.js/aa': ()=>__mako_require__(\"src/fake.js/aa.js\")",
"'./fake.js/index.js': ()=>__mako_require__(\"src/fake.js/index.js\")",
"'./fake.js/index': ()=>__mako_require__(\"src/fake.js/index.js\")",
"'./fake.js': ()=>__mako_require__(\"src/fake.js/index.js\")",
"'./fake.js/': ()=>__mako_require__(\"src/fake.js/index.js\")",
"'./i18n/en-US.json': ()=>__mako_require__(\"src/i18n/en-US.json\")",
"'./i18n/en-US': ()=>__mako_require__(\"src/i18n/en-US.json\")",
"'./i18n/zh-CN.json': ()=>__mako_require__(\"src/i18n/zh-CN.json\")",
"'./i18n/zh-CN': ()=>__mako_require__(\"src/i18n/zh-CN.json\")",
"'./index.ts': ()=>__mako_require__(\"src/index.ts\")",
"'./index': ()=>__mako_require__(\"src/index.ts\")",
"'.': ()=>__mako_require__(\"src/index.ts\")",
"'./': ()=>__mako_require__(\"src/index.ts\")"
`'.': ()=>__mako_require__("src/index.ts")`,
`'./': ()=>__mako_require__("src/index.ts")`,
`'./ext/json': ()=>__mako_require__("src/ext/json.ts")`,
`'./ext/json.ts': ()=>__mako_require__("src/ext/json.ts")`,
`'./fake.js': ()=>__mako_require__("src/fake.js/index.js")`,
`'./fake.js/': ()=>__mako_require__("src/fake.js/index.js")`,
`'./fake.js/a': ()=>__mako_require__("src/fake.js/a.js")`,
`'./fake.js/a.js': ()=>__mako_require__("src/fake.js/a.js")`,
`'./fake.js/aa': ()=>__mako_require__("src/fake.js/aa.js")`,
`'./fake.js/aa.js': ()=>__mako_require__("src/fake.js/aa.js")`,
`'./fake.js/index': ()=>__mako_require__("src/fake.js/index.js")`,
`'./fake.js/index.js': ()=>__mako_require__("src/fake.js/index.js")`,
`'./first_empty_quasis': ()=>__mako_require__("src/first_empty_quasis/index.js")`,
`'./first_empty_quasis/': ()=>__mako_require__("src/first_empty_quasis/index.js")`,
`'./first_empty_quasis/index': ()=>__mako_require__("src/first_empty_quasis/index.js")`,
`'./first_empty_quasis/index.js': ()=>__mako_require__("src/first_empty_quasis/index.js")`,
`'./first_empty_quasis/other': ()=>__mako_require__("src/first_empty_quasis/other.js")`,
`'./first_empty_quasis/other.js': ()=>__mako_require__("src/first_empty_quasis/other.js")`,
`'./i18n/en-US': ()=>__mako_require__("src/i18n/en-US.json")`,
`'./i18n/en-US.json': ()=>__mako_require__("src/i18n/en-US.json")`,
`'./i18n/zh-CN': ()=>__mako_require__("src/i18n/zh-CN.json")`,
`'./i18n/zh-CN.json': ()=>__mako_require__("src/i18n/zh-CN.json")`,
`'./index': ()=>__mako_require__("src/index.ts")`,
`'./index.ts': ()=>__mako_require__("src/index.ts")`
].join(',\n\\s+'),
true,
),
Expand All @@ -42,14 +46,14 @@ assert.match(
moduleReg(
"src/fake.js\\?context&glob=\\*\\*/\\*",
[
"'./a.js': ()=>__mako_require__(\"src/fake.js/a.js\")",
"'.': ()=>__mako_require__(\"src/fake.js/index.js\")",
"'./': ()=>__mako_require__(\"src/fake.js/index.js\")",
"'./a': ()=>__mako_require__(\"src/fake.js/a.js\")",
"'./aa.js': ()=>__mako_require__(\"src/fake.js/aa.js\")",
"'./a.js': ()=>__mako_require__(\"src/fake.js/a.js\")",
"'./aa': ()=>__mako_require__(\"src/fake.js/aa.js\")",
"'./index.js': ()=>__mako_require__(\"src/fake.js/index.js\")",
"'./aa.js': ()=>__mako_require__(\"src/fake.js/aa.js\")",
"'./index': ()=>__mako_require__(\"src/fake.js/index.js\")",
"'.': ()=>__mako_require__(\"src/fake.js/index.js\")",
"'./': ()=>__mako_require__(\"src/fake.js/index.js\")",
"'./index.js': ()=>__mako_require__(\"src/fake.js/index.js\")",
].join(',\n\\s+'),
true,
),
Expand Down Expand Up @@ -125,3 +129,5 @@ assert.doesNotMatch(
/glob=\*\*\/\*\s*\*\//,
"should escape glob pattern in module id debug comment"
);

assert.match(content, /src\/first_empty_quasis\?context&glob=\*\*\/\*&async/g, "should handle template string with first empty quasis");
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"alias": [
["@", "./src"]
]
}
},
"progress":false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function loadUrl(url) {
return import(`${url}`)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const other = "other"
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ console.log(loadFile('/zh-CN.json'));
console.log(loadFile2('/a.js'));
console.log(loadFile3('a.js'));

require("./first_empty_quasis")

0 comments on commit ae5e6e3

Please sign in to comment.