Skip to content

Commit

Permalink
fix: render filename with dollar sign
Browse files Browse the repository at this point in the history
  • Loading branch information
JSerFeng committed Aug 1, 2024
1 parent 6e86a14 commit 6fd5edd
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 24 deletions.
52 changes: 28 additions & 24 deletions crates/rspack_core/src/options/filename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::sync::Arc;
use std::{borrow::Cow, convert::Infallible, ptr};

use once_cell::sync::Lazy;
use regex::{Captures, Regex};
use regex::{Captures, NoExpand, Regex};
use rspack_error::error;
use rspack_macros::MergeFrom;
use rspack_util::atom::Atom;
Expand Down Expand Up @@ -247,38 +247,42 @@ fn render_template(
}) = parse_resource(filename)
{
t = t
.map(|t| FILE_PLACEHOLDER.replace_all(t, file.to_string_lossy()))
.map(|t| FILE_PLACEHOLDER.replace_all(t, NoExpand(&file.to_string_lossy())))
.map(|t| {
EXT_PLACEHOLDER.replace_all(
t,
file
.extension()
.map(|p| format!(".{}", p.to_string_lossy()))
.unwrap_or_default(),
NoExpand(
&file
.extension()
.map(|p| format!(".{}", p.to_string_lossy()))
.unwrap_or_default(),
),
)
});

if let Some(base) = file.file_name().map(|p| p.to_string_lossy()) {
t = t.map(|t| BASE_PLACEHOLDER.replace_all(t, &base));
t = t.map(|t| BASE_PLACEHOLDER.replace_all(t, NoExpand(&base)));
}
if let Some(name) = file.file_stem().map(|p| p.to_string_lossy()) {
t = t.map(|t| NAME_PLACEHOLDER.replace_all(t, &name));
t = t.map(|t| NAME_PLACEHOLDER.replace_all(t, NoExpand(&name)));
}
t = t
.map(|t| {
PATH_PLACEHOLDER.replace_all(
t,
&file
.parent()
.map(|p| p.to_string_lossy())
// "" -> "", "folder" -> "folder/"
.filter(|p| !p.is_empty())
.map(|p| p + "/")
.unwrap_or_default(),
NoExpand(
&file
.parent()
.map(|p| p.to_string_lossy())
// "" -> "", "folder" -> "folder/"
.filter(|p| !p.is_empty())
.map(|p| p + "/")
.unwrap_or_default(),
),
)
})
.map(|t| QUERY_PLACEHOLDER.replace_all(t, &query.unwrap_or_default()))
.map(|t| FRAGMENT_PLACEHOLDER.replace_all(t, &fragment.unwrap_or_default()));
.map(|t| QUERY_PLACEHOLDER.replace_all(t, NoExpand(&query.unwrap_or_default())))
.map(|t| FRAGMENT_PLACEHOLDER.replace_all(t, NoExpand(&fragment.unwrap_or_default())));
}
}
if let Some(content_hash) = options.content_hash {
Expand Down Expand Up @@ -313,12 +317,12 @@ fn render_template(
}
if let Some(chunk) = options.chunk {
if let Some(id) = &options.id {
t = t.map(|t| ID_PLACEHOLDER.replace_all(t, *id));
t = t.map(|t| ID_PLACEHOLDER.replace_all(t, NoExpand(*id)));

Check failure on line 320 in crates/rspack_core/src/options/filename.rs

View workflow job for this annotation

GitHub Actions / Rust check

deref which would be done by auto-deref
} else if let Some(id) = &chunk.id {
t = t.map(|t| ID_PLACEHOLDER.replace_all(t, id));
t = t.map(|t| ID_PLACEHOLDER.replace_all(t, NoExpand(id)));
}
if let Some(name) = chunk.name_for_filename_template() {
t = t.map(|t| NAME_PLACEHOLDER.replace_all(t, name));
t = t.map(|t| NAME_PLACEHOLDER.replace_all(t, NoExpand(name)));
}
if let Some(d) = chunk.rendered_hash.as_ref() {
t = t.map(|t| {
Expand All @@ -336,17 +340,17 @@ fn render_template(
}

if let Some(id) = &options.id {
t = t.map(|t| ID_PLACEHOLDER.replace_all(t, *id));
t = t.map(|t| ID_PLACEHOLDER.replace_all(t, NoExpand(*id)));

Check failure on line 343 in crates/rspack_core/src/options/filename.rs

View workflow job for this annotation

GitHub Actions / Rust check

deref which would be done by auto-deref
} else if let Some(module) = options.module {
if let Some(chunk_graph) = options.chunk_graph {
if let Some(id) = chunk_graph.get_module_id(module.identifier()) {
t = t.map(|t| ID_PLACEHOLDER.replace_all(t, id));
t = t.map(|t| ID_PLACEHOLDER.replace_all(t, NoExpand(id)));
}
}
}
t = t.map(|t| RUNTIME_PLACEHOLDER.replace_all(t, options.runtime.unwrap_or("_")));
t = t.map(|t| RUNTIME_PLACEHOLDER.replace_all(t, NoExpand(options.runtime.unwrap_or("_"))));
if let Some(url) = options.url {
t = t.map(|t| URL_PLACEHOLDER.replace_all(t, url));
t = t.map(|t| URL_PLACEHOLDER.replace_all(t, NoExpand(url)));
}
t.into_owned()
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import "./img$1.png";
import "./img$2.png";
import fs from "fs";
import path from "path";

it("should compile", () => {
expect(fs.existsSync(path.resolve(__dirname, 'img$1.png'))).toBeTruthy();
expect(fs.existsSync(path.resolve(__dirname, 'img$2.png'))).toBeTruthy();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/** @type {import("@rspack/core").Configuration} */
module.exports = {
context: __dirname,
module: {
rules: [
{
test: /\.png$/,
type: "asset/resource",
generator: {
filename: '[name][ext]'
}
}
],
}
};

0 comments on commit 6fd5edd

Please sign in to comment.