Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Path pitfall for loader type #7666

Merged
merged 4 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
h-a-n-a marked this conversation as resolved.
Show resolved Hide resolved
});
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.

Loading