From 8d9195f8627557f53a8be06324f553f50acd047b Mon Sep 17 00:00:00 2001 From: Izaak Schroeder Date: Tue, 21 Feb 2017 19:13:03 -0800 Subject: [PATCH] Pass `dirname` as extra metadata to preset constructor. (#4834) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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. --- .../src/transformation/file/options/option-manager.js | 2 +- .../node_modules/addons/preset.js | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/babel-core/src/transformation/file/options/option-manager.js b/packages/babel-core/src/transformation/file/options/option-manager.js index f4b25206bc..4746260a36 100644 --- a/packages/babel-core/src/transformation/file/options/option-manager.js +++ b/packages/babel-core/src/transformation/file/options/option-manager.js @@ -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}.`); diff --git a/packages/babel-core/test/fixtures/resolution/resolve-addons-relative-to-file/node_modules/addons/preset.js b/packages/babel-core/test/fixtures/resolution/resolve-addons-relative-to-file/node_modules/addons/preset.js index a1af16e41f..e08f4b1c61 100644 --- a/packages/babel-core/test/fixtures/resolution/resolve-addons-relative-to-file/node_modules/addons/preset.js +++ b/packages/babel-core/test/fixtures/resolution/resolve-addons-relative-to-file/node_modules/addons/preset.js @@ -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 () {