Skip to content

Commit

Permalink
Merge pull request #389 from webpack/feat-accept-nullish-plugins
Browse files Browse the repository at this point in the history
feat: ignore `false`/`null`/`undefined` plugins
  • Loading branch information
TheLarkInn authored Jun 13, 2023
2 parents 3c3d31e + 3a765d9 commit d08a64d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/ResolverFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ const UseFilePlugin = require("./UseFilePlugin");
/** @typedef {string|string[]|false} AliasOptionNewRequest */
/** @typedef {{[k: string]: AliasOptionNewRequest}} AliasOptions */
/** @typedef {{[k: string]: string|string[] }} ExtensionAliasOptions */
/** @typedef {{apply: function(Resolver): void} | function(this: Resolver, Resolver): void} Plugin */
/** @typedef {false | 0 | "" | null | undefined} Falsy */
/** @typedef {{apply: function(Resolver): void} | (function(this: Resolver, Resolver): void) | Falsy} Plugin */

/**
* @typedef {Object} UserResolveOptions
Expand Down Expand Up @@ -657,7 +658,7 @@ exports.createResolver = function (options) {
if (typeof plugin === "function") {
/** @type {function(this: Resolver, Resolver): void} */
(plugin).call(resolver, resolver);
} else {
} else if (plugin) {
plugin.apply(resolver);
}
}
Expand Down
42 changes: 42 additions & 0 deletions test/plugins.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,46 @@ describe("plugins", function () {
}
);
});

it("should ignore 'false'/'null'/'undefined' plugins", done => {
const FailedPlugin = class {
apply() {
throw new Error("FailedPlugin");
}
};
const falsy = false;
const resolver = ResolverFactory.createResolver({
fileSystem: require("fs"),
plugins: [
0,
"",
false,
null,
undefined,
falsy && new FailedPlugin(),
new CloneBasenamePlugin(
"after-existing-directory",
"undescribed-raw-file"
)
]
});

resolver.resolve(
{},
__dirname,
"./fixtures/directory-default",
{},
function (err, result) {
if (err) return done(err);
if (!result) return done(new Error("No result"));
expect(result).toEqual(
path.resolve(
__dirname,
"fixtures/directory-default/directory-default.js"
)
);
done();
}
);
});
});
5 changes: 5 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,11 @@ declare interface ParsedIdentifier {
internal: boolean;
}
type Plugin =
| undefined
| null
| false
| ""
| 0
| { apply: (arg0: Resolver) => void }
| ((this: Resolver, arg1: Resolver) => void);
declare interface PnpApiImpl {
Expand Down

0 comments on commit d08a64d

Please sign in to comment.