Skip to content

Commit

Permalink
fix: Path pitfall for loader type (#7666)
Browse files Browse the repository at this point in the history
* fix: fix UTF-8 pitfall for loader type

* fix: `as_str` instead of `extension`

* fix

* fix
  • Loading branch information
h-a-n-a authored Aug 23, 2024
1 parent 881f15f commit b96bf7d
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ pub(crate) async fn resolve_loader(
description_data,
..
} = resource;
// Pitfall: `Path::ends_with` is different from `str::ends_with`
// So we need to convert `PathBuf` to `&str`
// Use `str::ends_with` instead of `Path::extension` to avoid unnecessary allocation
let path = path.as_str();

let r#type = if path.ends_with(".mjs") {
Some("module")
Expand Down
4 changes: 3 additions & 1 deletion packages/rspack/src/loader-runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ function createLoaderObject(
obj.ident = ident;
}

obj.type = value.type;
// CHANGE: `rspack_core` returns empty string for `undefined` type.
// Comply to webpack test case: tests/webpack-test/cases/loaders/cjs-loader-type/index.js
obj.type = value.type === "" ? undefined : value.type;
if (obj.options === null) obj.query = "";
else if (obj.options === undefined) obj.query = "";
else if (typeof obj.options === "string") obj.query = `?${obj.options}`;
Expand Down
15 changes: 15 additions & 0 deletions tests/webpack-test/cases/loaders/cjs-loader-type/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
it("should pass package.json type to loader", function () {
expect(require("cjs/loader.js!")).toBe("commonjs");
expect(require("./loader.js!")).toBe("undefined");
});

it("should pass 'commonjs' type to loader for .cjs", function () {
expect(require("cjs/loader.cjs!")).toBe("commonjs");
expect(require("./loader.cjs!")).toBe("commonjs");
// ORIGINAL WEBPACK COMMENT: TODO need fix in v8 https://github.com/nodejs/node/issues/35889
// ORIGINAL WEBPACK COMMENT: TODO otherwise this test case cause segment fault
// Turned on this as rspack checks extensions for loader type.
// So this will not fall into dynamic import which causes segment fault.
// See: crates/rspack_binding_options/src/plugins/js_loader/resolver.rs
expect(require("esm/loader.cjs!")).toBe("commonjs");
});
4 changes: 4 additions & 0 deletions tests/webpack-test/cases/loaders/cjs-loader-type/loader.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function loader() {
return `module.exports = "${this.loaders[this.loaderIndex].type}";`;
};
4 changes: 4 additions & 0 deletions tests/webpack-test/cases/loaders/cjs-loader-type/loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function loader() {
return `module.exports = "${this.loaders[this.loaderIndex].type}";`;
};

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

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

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

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

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

2 comments on commit b96bf7d

@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-08-23 2d18cc2) Current Change
10000_development-mode + exec 2.37 s ± 25 ms 2.36 s ± 16 ms -0.08 %
10000_development-mode_hmr + exec 705 ms ± 15 ms 708 ms ± 5.5 ms +0.41 %
10000_production-mode + exec 3.03 s ± 26 ms 3.08 s ± 60 ms +1.77 %
arco-pro_development-mode + exec 1.9 s ± 88 ms 1.92 s ± 76 ms +0.87 %
arco-pro_development-mode_hmr + exec 437 ms ± 2.6 ms 438 ms ± 0.95 ms +0.26 %
arco-pro_production-mode + exec 3.47 s ± 54 ms 3.45 s ± 58 ms -0.68 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.53 s ± 69 ms 3.55 s ± 71 ms +0.63 %
threejs_development-mode_10x + exec 1.69 s ± 15 ms 1.69 s ± 11 ms -0.09 %
threejs_development-mode_10x_hmr + exec 800 ms ± 10 ms 800 ms ± 6.4 ms +0.01 %
threejs_production-mode_10x + exec 5.49 s ± 29 ms 5.57 s ± 15 ms +1.42 %

@rspack-bot
Copy link

Choose a reason for hiding this comment

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

📝 Ran ecosystem CI: Open

suite result
modernjs ✅ success
_selftest ✅ success
nx ❌ failure
rspress ✅ success
rslib ✅ success
rsbuild ✅ success
examples ✅ success

Please sign in to comment.