Skip to content
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
4 changes: 2 additions & 2 deletions package-lock.json

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

1 change: 1 addition & 0 deletions test/fixtures/esm-invalid-loader.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default "";
3 changes: 3 additions & 0 deletions test/fixtures/esm-pitching-loader.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function pitch(remainingRequest, previousRequest) {
return [remainingRequest, previousRequest].join(":");
}
8 changes: 8 additions & 0 deletions test/fixtures/esm-raw-loader.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default function (source) {
return Buffer.from(
source.toString("hex") + source.toString("utf-8"),
"utf-8"
);
}

export const raw = true;
148 changes: 148 additions & 0 deletions test/runLoaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,154 @@ describe("runLoaders", () => {
);
});
}

const [nodeMajor, nodeMinor] = process.versions.node.split(".").map(Number);
// `require(esm)` is enabled by default starting from Node.js 22.12.0
if (nodeMajor > 22 || (nodeMajor === 22 && nodeMinor >= 12)) {
it("should load an esm loader using require()", (done) => {
runLoaders(
{
resource: path.resolve(fixtures, "resource.bin"),
loaders: [path.resolve(fixtures, "esm-loader.mjs")],
},
(err, result) => {
if (err) return done(err);
result.result.should.be.eql(["resource-esm"]);
result.cacheable.should.be.eql(true);
result.fileDependencies.should.be.eql([
path.resolve(fixtures, "resource.bin"),
]);
result.contextDependencies.should.be.eql([]);
done();
}
);
});
}

if (Number(process.versions.modules) >= 83) {
it("should load a commonjs loader using import()", (done) => {
runLoaders(
{
resource: path.resolve(fixtures, "resource.bin"),
loaders: [
{
loader: path.resolve(fixtures, "simple-loader.js"),
type: "module",
},
],
},
(err, result) => {
if (err) return done(err);
result.result.should.be.eql(["resource-simple"]);
result.cacheable.should.be.eql(true);
result.fileDependencies.should.be.eql([
path.resolve(fixtures, "resource.bin"),
]);
result.contextDependencies.should.be.eql([]);
done();
}
);
});

it("should process an esm pitching loader using import()", (done) => {
runLoaders(
{
resource: path.resolve(fixtures, "resource.bin"),
loaders: [
path.resolve(fixtures, "simple-loader.js"),
{
loader: path.resolve(fixtures, "esm-pitching-loader.mjs"),
type: "module",
},
path.resolve(fixtures, "simple-async-loader.js"),
],
},
(err, result) => {
if (err) return done(err);
result.result.should.be.eql([
`${path.resolve(fixtures, "simple-async-loader.js")}!${path.resolve(
fixtures,
"resource.bin"
)}:${path.resolve(fixtures, "simple-loader.js")}-simple`,
]);
result.cacheable.should.be.eql(true);
result.fileDependencies.should.be.eql([]);
result.contextDependencies.should.be.eql([]);
done();
}
);
});

it("should process an esm raw loader using import()", (done) => {
runLoaders(
{
resource: path.resolve(fixtures, "bom.bin"),
loaders: [
{
loader: path.resolve(fixtures, "esm-raw-loader.mjs"),
type: "module",
},
],
},
(err, result) => {
if (err) return done(err);
result.result[0].toString("utf8").should.be.eql("efbbbf62c3b66dböm");
done();
}
);
});

it("should not return dependencies when an esm loader is not found", (done) => {
runLoaders(
{
resource: path.resolve(fixtures, "resource.bin"),
loaders: [
{
loader: path.resolve(fixtures, "does-not-exist-loader.mjs"),
type: "module",
},
],
},
(err, result) => {
err.should.be.instanceOf(Error);
result.should.be.eql({
cacheable: false,
fileDependencies: [],
contextDependencies: [],
missingDependencies: [],
});
done();
}
);
});

it("should not return dependencies when an esm loader has an invalid default export", (done) => {
runLoaders(
{
resource: path.resolve(fixtures, "resource.bin"),
loaders: [
{
loader: path.resolve(fixtures, "esm-invalid-loader.mjs"),
type: "module",
},
],
},
(err, result) => {
err.should.be.instanceOf(Error);
err.message.should.match(
/esm-invalid-loader\.mjs' is not a loader \(must have normal or pitch function\)$/
);
result.should.be.eql({
cacheable: false,
fileDependencies: [],
contextDependencies: [],
missingDependencies: [],
});
done();
}
);
});
}
it("should support escaping in resource", (done) => {
runLoaders(
{
Expand Down
Loading