Pass dirname as extra metadata to preset constructor. (#4834)

* Pass `dirname` as extra metadata to preset constructor.

Sometimes a preset would like to know where it should resolve relative paths from (e.g. https://github.com/tleunen/babel-plugin-module-resolver) and this extra information makes that possible.

* Test for `dirname` passed into preset constructor

This adds a check for `dirname`’s existence and correctness to the
`resolve-addons-relative-to-file` test, and serves as a minimal example
of a path-aware preset.
This commit is contained in:
Izaak Schroeder
2017-02-21 19:13:03 -08:00
committed by Henry Zhu
parent 02f51fb7a5
commit 8d9195f862
2 changed files with 8 additions and 3 deletions

View File

@@ -299,7 +299,7 @@ export default class OptionManager {
(presetLoc || "a preset") + " which does not accept options.");
}
if (typeof val === "function") val = val(context, options);
if (typeof val === "function") val = val(context, options, { dirname });
if (typeof val !== "object") {
throw new Error(`Unsupported preset format: ${val}.`);

View File

@@ -1,5 +1,10 @@
module.exports = {
plugins: [plugin],
module.exports = function preset (context, options, fileContext) {
if (/resolve-addons-relative-to-file$/.test(fileContext.dirname)) {
return {
plugins: [plugin],
};
}
return {};
};
function plugin () {