Skip to content

Commit

Permalink
fix: sourceMapFilename is relative (#8269)
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind authored Oct 30, 2024
1 parent 6ecd777 commit f96227c
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 18 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/rspack_plugin_devtool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ dashmap = { workspace = true }
derivative = { workspace = true }
futures = { workspace = true }
itertools = { workspace = true }
pathdiff = { workspace = true }
rayon = { workspace = true }
regex = { workspace = true }
rspack_base64 = { version = "0.1.0", path = "../rspack_base64" }
Expand Down
24 changes: 17 additions & 7 deletions crates/rspack_plugin_devtool/src/source_map_dev_tool_plugin.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::path::{Component, PathBuf};
use std::sync::LazyLock;
use std::{borrow::Cow, path::Path};

use cow_utils::CowUtils;
use derivative::Derivative;
use futures::future::{join_all, BoxFuture};
use itertools::Itertools;
use pathdiff::diff_paths;
use rayon::prelude::*;
use regex::Regex;
use rspack_core::{
Expand Down Expand Up @@ -416,13 +416,23 @@ impl SourceMapDevToolPlugin {

if let Some(current_source_mapping_url_comment) = current_source_mapping_url_comment {
let source_map_url = if let Some(public_path) = &self.public_path {
Cow::Owned(format!("{public_path}{source_map_filename}"))
} else if let Some(dirname) = Path::new(filename.as_ref()).parent()
&& let Some(relative) = diff_paths(&source_map_filename, dirname)
{
Cow::Owned(relative.to_string_lossy().to_string())
format!("{public_path}{source_map_filename}")
} else {
Cow::Borrowed(source_map_filename.as_str())
let mut file_path = PathBuf::new();
file_path.push(Component::RootDir);
file_path.extend(Path::new(filename.as_ref()).components());

let mut source_map_path = PathBuf::new();
source_map_path.push(Component::RootDir);
source_map_path.extend(Path::new(&source_map_filename).components());

relative(
#[allow(clippy::unwrap_used)]
file_path.parent().unwrap(),
&source_map_path,
)
.to_string_lossy()
.to_string()
};
let data = data.url(&source_map_url);
let current_source_mapping_url_comment = match &current_source_mapping_url_comment {
Expand Down
41 changes: 37 additions & 4 deletions crates/rspack_util/src/path.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,43 @@
use std::path::{Path, PathBuf};
use std::path::{Component, Components, Path, PathBuf};

fn normalize(parts: Components, allow_above_root: bool) -> Vec<Component> {
let mut res = vec![];
for p in parts {
match &p {
Component::CurDir => (),
Component::ParentDir => {
if !matches!(
res.last(),
Some(Component::ParentDir) | Some(Component::RootDir)
) {
res.pop();
} else if allow_above_root {
res.push(p);
}
}
_ => res.push(p),
}
}
res
}

pub fn relative(from: &Path, to: &Path) -> PathBuf {
if from == to {
return PathBuf::new();
}

let mut from_iter = from.components();
let mut to_iter = to.components();
let is_from_absolute = matches!(from.components().next(), Some(Component::RootDir));
let is_to_absolute = matches!(to.components().next(), Some(Component::RootDir));

// At this point the path should be resolved to a full absolute path, but
// handle relative paths to be safe

// Normalize the path
let from = normalize(from.components(), !is_from_absolute);
let to = normalize(to.components(), !is_to_absolute);

let mut from_iter = from.iter();
let mut to_iter = to.iter();
let mut common_parts = 0;

let from_remain = loop {
Expand All @@ -24,7 +55,7 @@ pub fn relative(from: &Path, to: &Path) -> PathBuf {
result.push("..");
}

let to_iter = to.components().skip(common_parts);
let to_iter = to.into_iter().skip(common_parts);
for part in to_iter {
result.push(part.as_os_str());
}
Expand Down Expand Up @@ -59,6 +90,8 @@ mod test {
("/baz-quux", "/baz", "../baz"),
("/baz", "/baz-quux", "../baz-quux"),
("/page1/page2/foo", "/", "../../.."),
// Fix https://github.com/web-infra-dev/rspack/issues/8219
("/", "/../maps/main.js.map", "maps/main.js.map"),
];

for (from, to, expected) in test_cases {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import fs from "fs";
import fs from "fs/promises";
import path from "path";

it("source-map-filename/name should same", async function () {
import("./two");
expect(
fs.readdirSync(path.resolve(__dirname, "")).includes("main.js.map")
).toBe(true);

expect(async () => await fs.stat(path.resolve(__dirname, "../maps/main.js.map"))).not.toThrow();

const outputCode = await fs.readFile(__filename, 'utf-8');
const sourceMapPath = outputCode.match(/\/\/# sourceMappingURL=(.*)/)?.[1];
expect(sourceMapPath).toBe(path.normalize("maps/main.js.map"));
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ module.exports = {
target: "node",
output: {
filename: "[name].js",
sourceMapFilename: "[name].js.map"
sourceMapFilename: "../maps/[name].js.map"
}
};

1 comment on commit f96227c

@rspack-bot
Copy link

Choose a reason for hiding this comment

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

📝 Benchmark detail: Open

Name Base (2024-10-30 85bb238) Current Change
10000_big_production-mode + exec 48.8 s ± 720 ms 49.2 s ± 598 ms +0.86 %
10000_development-mode + exec 2.11 s ± 43 ms 2.12 s ± 33 ms +0.48 %
10000_development-mode_hmr + exec 647 ms ± 5.9 ms 651 ms ± 8.8 ms +0.59 %
10000_production-mode + exec 2.67 s ± 33 ms 2.68 s ± 55 ms +0.35 %
arco-pro_development-mode + exec 1.77 s ± 81 ms 1.79 s ± 74 ms +1.12 %
arco-pro_development-mode_hmr + exec 428 ms ± 1.8 ms 429 ms ± 2 ms +0.12 %
arco-pro_production-mode + exec 3.18 s ± 73 ms 3.25 s ± 92 ms +2.04 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.28 s ± 79 ms 3.27 s ± 68 ms -0.21 %
threejs_development-mode_10x + exec 1.62 s ± 14 ms 1.63 s ± 17 ms +0.25 %
threejs_development-mode_10x_hmr + exec 774 ms ± 5.9 ms 775 ms ± 9.3 ms +0.11 %
threejs_production-mode_10x + exec 4.99 s ± 19 ms 5.02 s ± 34 ms +0.52 %

Please sign in to comment.