Skip to content

Commit

Permalink
fix: assets with query (#975)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc authored Mar 21, 2024
1 parent ecbb162 commit 14b6ed9
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 8 deletions.
8 changes: 4 additions & 4 deletions crates/mako/src/ast_2/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,15 @@ impl File {
}

pub fn get_file_size(&self) -> Result<u64> {
let metadata = std::fs::metadata(&self.path)?;
let metadata = std::fs::metadata(&self.pathname)?;
Ok(metadata.len())
}

pub fn get_base64(&self) -> Result<String> {
let content = std::fs::read(&self.path)?;
let content = std::fs::read(&self.pathname)?;
let engine = engine::GeneralPurpose::new(&STANDARD, engine::general_purpose::PAD);
let content = engine.encode(content);
let guess = mime_guess::from_path(&self.path);
let guess = mime_guess::from_path(&self.pathname);
if let Some(mime) = guess.first() {
Ok(format!(
"data:{};base64,{}",
Expand All @@ -213,7 +213,7 @@ impl File {
}

pub fn get_content_hash(&self) -> Result<String> {
let file = std::fs::File::open(&self.path)?;
let file = std::fs::File::open(&self.pathname)?;
let len = file.metadata()?.len();
// Decide on a reasonable buffer size (1MB in this case, fastest will depend on hardware)
let buf_len = len.min(1_000_000) as usize;
Expand Down
9 changes: 5 additions & 4 deletions crates/mako/src/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export function moduleToDom(css) {
file.extname
);
context.emit_assets(
file.path.to_string_lossy().to_string(),
file.pathname.to_string_lossy().to_string(),
final_file_name.clone(),
);
return Ok(Content::Js(format!(
Expand Down Expand Up @@ -238,10 +238,11 @@ export function moduleToDom(css) {
inject_public_path: bool,
context: Arc<Context>,
) -> Result<String> {
let path = file.path.to_string_lossy().to_string();
let file_size = file
.get_file_size()
.map_err(|_| LoadError::ReadFileSizeError { path })?;
.map_err(|_| LoadError::ReadFileSizeError {
path: file.path.to_string_lossy().to_string(),
})?;
let emit_assets = || -> Result<String> {
let final_file_name = Self::emit_asset(file, context.clone());
if inject_public_path {
Expand Down Expand Up @@ -270,7 +271,7 @@ export function moduleToDom(css) {
}

pub fn emit_asset(file: &File, context: Arc<Context>) -> String {
let path = file.path.to_string_lossy().to_string();
let path = file.pathname.to_string_lossy().to_string();
let final_file_name = format!(
"{}.{}.{}",
file.get_file_stem(),
Expand Down
14 changes: 14 additions & 0 deletions e2e/fixtures/assets.with-query/expect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const assert = require("assert");
const { parseBuildResult, moduleReg } = require("../../../scripts/test-utils");
const { files } = parseBuildResult(__dirname);

assert(
Object.keys(files).find((file) => /xx.[0-9a-zA-z]*.schema/.test(file)),
"add file of unsupported mime to assets"
);

const content = files["index.js"];
assert(
content.includes(`"src/xx.schema?a=1":`),
"assets with query should be handled correctly"
);
1 change: 1 addition & 0 deletions e2e/fixtures/assets.with-query/mako.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "minify": false }
3 changes: 3 additions & 0 deletions e2e/fixtures/assets.with-query/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import xx from "./xx.schema?a=1";

console.log(xx);
1 change: 1 addition & 0 deletions e2e/fixtures/assets.with-query/src/xx.schema
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Object() {}

0 comments on commit 14b6ed9

Please sign in to comment.