Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Also pass the caller option to loadPartialConfig #685

Merged
merged 2 commits into from Sep 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/index.js
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 loaderUtils = require("loader-utils");

Expand Down Expand Up @@ -118,7 +119,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
@@ -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
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;
}