Skip to content

Commit

Permalink
Pass Babel's .caller option and pass supportsStaticESM:true. (#660)
Browse files Browse the repository at this point in the history
  • Loading branch information
loganfsmyth committed Aug 21, 2018
1 parent 6a70942 commit 5eb1b0a
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion 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, options);
result = await transform(source, injectCaller(options));
} catch (err) {
throw err.message && err.codeFrame ? new LoaderError(err) : err;
}
Expand All @@ -29,3 +29,43 @@ 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 5eb1b0a

Please sign in to comment.