Address some of @gadicc's feedback on .babelrc implementation.

Full comments: https://github.com/meteor/meteor/issues/6351#issuecomment-220944251

This commit implements optional "babel-{plugin,preset}-" prefixing and
.__esModule logic (point 2), and gives users more control over load order
by turning their plugins into a preset that loads between
babel-preset-meteor and the default plugins (point 4.i).

As time goes on, Meteor should consider moving some transforms out of the
babel-preset-meteor preset and into the babelOptions.plugins list, so that
they can be preempted by user presets.
This commit is contained in:
Ben Newman
2016-05-24 13:34:29 -04:00
parent 8395c7b832
commit f97f528f33

View File

@@ -190,6 +190,8 @@ BCp._inferHelper = function (inputFile, babelOptions, babelrc) {
return false;
}
var inferredPresets = [];
function infer(listName, prefix) {
var list = babelrc[listName];
if (! Array.isArray(list) || list.length === 0) {
@@ -198,15 +200,31 @@ BCp._inferHelper = function (inputFile, babelOptions, babelrc) {
function req(id) {
var isTopLevel = "./".indexOf(id.charAt(0)) < 0;
var presetOrPlugin;
if (isTopLevel) {
// If the identifier is top-level, it will be prefixed with
// "babel-plugin-" or "babel-preset-". If the identifier is not
// top-level, but relative or absolute, then it will be required
// as-is, so that you can implement your own Babel plugins
// locally, rather than always using plugins installed from npm.
id = prefix + id;
try {
// If the identifier is top-level, try to prefix it with
// "babel-plugin-" or "babel-preset-".
presetOrPlugin = inputFile.require(prefix + id);
} catch (e) {
if (e.code !== "MODULE_NOT_FOUND") {
throw e;
}
// Fall back to requiring the plugin as-is if the prefix failed.
presetOrPlugin = inputFile.require(id);
}
} else {
// If the identifier is not top-level, but relative or absolute,
// then it will be required as-is, so that you can implement your
// own Babel plugins locally, rather than always using plugins
// installed from npm.
presetOrPlugin = inputFile.require(id);
}
return inputFile.require(id);
return presetOrPlugin.__esModule
? presetOrPlugin.default
: presetOrPlugin;
}
list.forEach(function (item, i) {
@@ -220,16 +238,26 @@ BCp._inferHelper = function (inputFile, babelOptions, babelrc) {
list[i] = item;
});
// PREPEND additional plugins to the existing babelOptions[listName]
// list, so that they have a chance to handle syntax differently than
// babel-preset-meteor normally would.
var target = babelOptions[listName] || [];
target.unshift.apply(target, list);
babelOptions[listName] = target;
if (listName === "plugins") {
// Turn any additional plugins into their own preset, so that they
// can come before babel-preset-meteor.
inferredPresets.push({ plugins: list });
} else if (listName === "presets") {
inferredPresets.push.apply(inferredPresets, list);
}
}
infer("presets", "babel-preset-");
infer("plugins", "babel-plugin-");
return true;
if (inferredPresets.length > 0) {
babelOptions.presets.push.apply(
babelOptions.presets,
inferredPresets
);
return true;
}
return false;
};