From 7d8500ced72046d4c5c1dcdc7a9c0016d7e6c15b Mon Sep 17 00:00:00 2001 From: Daniel Playfair Cal Date: Thu, 27 Sep 2018 15:38:50 +1000 Subject: [PATCH] Also pass the caller option to loadPartialConfig (#685) * Also pass the caller option to loadPartialConfig * Remove unecessary call to injectCaller before transform --- src/index.js | 3 ++- src/injectCaller.js | 41 +++++++++++++++++++++++++++++++++++++++++ src/transform.js | 42 +----------------------------------------- 3 files changed, 44 insertions(+), 42 deletions(-) create mode 100644 src/injectCaller.js diff --git a/src/index.js b/src/index.js index cd92de5b..5d6e8313 100644 --- a/src/index.js +++ b/src/index.js @@ -22,6 +22,7 @@ if (/^6\./.test(babel.version)) { const pkg = require("../package.json"); const cache = require("./cache"); const transform = require("./transform"); +const injectCaller = require("./injectCaller"); const path = require("path"); const loaderUtils = require("loader-utils"); @@ -151,7 +152,7 @@ async function loader(source, inputSourceMap, overrides) { ); } - const config = babel.loadPartialConfig(programmaticOptions); + const config = babel.loadPartialConfig(injectCaller(programmaticOptions)); if (config) { let options = config.options; if (overrides && overrides.config) { diff --git a/src/injectCaller.js b/src/injectCaller.js new file mode 100644 index 00000000..f35903b9 --- /dev/null +++ b/src/injectCaller.js @@ -0,0 +1,41 @@ +const babel = require("@babel/core"); + +module.exports = function injectCaller(opts) { + if (!supportsCallerOption()) return opts; + + return Object.assign({}, opts, { + caller: Object.assign( + { + name: "babel-loader", + + // Webpack >= 2 supports ESM and dynamic import. + supportsStaticESM: true, + supportsDynamicImport: true, + }, + opts.caller, + ), + }); +}; + +// TODO: We can remove this eventually, I'm just adding it so that people have +// a little time to migrate to the newer RCs of @babel/core without getting +// hard-to-diagnose errors about unknown 'caller' options. +let supportsCallerOptionFlag = undefined; +function supportsCallerOption() { + if (supportsCallerOptionFlag === undefined) { + try { + // Rather than try to match the Babel version, we just see if it throws + // when passed a 'caller' flag, and use that to decide if it is supported. + babel.loadPartialConfig({ + caller: undefined, + babelrc: false, + configFile: false, + }); + supportsCallerOptionFlag = true; + } catch (err) { + supportsCallerOptionFlag = false; + } + } + + return supportsCallerOptionFlag; +} diff --git a/src/transform.js b/src/transform.js index 8e22f0e8..dee2e3e0 100644 --- a/src/transform.js +++ b/src/transform.js @@ -7,7 +7,7 @@ const transform = promisify(babel.transform); module.exports = async function(source, options) { let result; try { - result = await transform(source, injectCaller(options)); + result = await transform(source, options); } catch (err) { throw err.message && err.codeFrame ? new LoaderError(err) : err; } @@ -29,43 +29,3 @@ module.exports = async function(source, options) { }; module.exports.version = babel.version; - -function injectCaller(opts) { - if (!supportsCallerOption()) return opts; - - return Object.assign({}, opts, { - caller: Object.assign( - { - name: "babel-loader", - - // Webpack >= 2 supports ESM and dynamic import. - supportsStaticESM: true, - supportsDynamicImport: true, - }, - opts.caller, - ), - }); -} - -// TODO: We can remove this eventually, I'm just adding it so that people have -// a little time to migrate to the newer RCs of @babel/core without getting -// hard-to-diagnose errors about unknown 'caller' options. -let supportsCallerOptionFlag = undefined; -function supportsCallerOption() { - if (supportsCallerOptionFlag === undefined) { - try { - // Rather than try to match the Babel version, we just see if it throws - // when passed a 'caller' flag, and use that to decide if it is supported. - babel.loadPartialConfig({ - caller: undefined, - babelrc: false, - configFile: false, - }); - supportsCallerOptionFlag = true; - } catch (err) { - supportsCallerOptionFlag = false; - } - } - - return supportsCallerOptionFlag; -}