Skip to content

Commit

Permalink
Also pass the caller option to loadPartialConfig (#685)
Browse files Browse the repository at this point in the history
* Also pass the caller option to loadPartialConfig

* Remove unecessary call to injectCaller before transform
  • Loading branch information
hedgepigdaniel authored and loganfsmyth committed Sep 27, 2018
1 parent a507914 commit 7d8500c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 42 deletions.
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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) {
Expand Down
41 changes: 41 additions & 0 deletions src/injectCaller.js
Original file line number Diff line number Diff line change
@@ -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;
}
42 changes: 1 addition & 41 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}

0 comments on commit 7d8500c

Please sign in to comment.