[7.0] Allow presets to be objects (#5385)
* Allow presets to be objects * Improve logic to detect preset format
This commit is contained in:
parent
9acae54a29
commit
6d6cdf6baf
@ -268,29 +268,30 @@ export default class OptionManager {
|
||||
JSON.stringify(dirname));
|
||||
}
|
||||
}
|
||||
const presetFactory = this.getPresetFactoryForPreset(presetLoc || preset);
|
||||
const resolvedPreset = this.loadPreset(presetLoc || preset, options, { dirname });
|
||||
|
||||
preset = presetFactory(context, options, { dirname });
|
||||
if (onResolve) onResolve(resolvedPreset, presetLoc);
|
||||
|
||||
if (onResolve) onResolve(preset, presetLoc);
|
||||
return resolvedPreset;
|
||||
} catch (e) {
|
||||
if (presetLoc) {
|
||||
e.message += ` (While processing preset: ${JSON.stringify(presetLoc)})`;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
|
||||
return preset;
|
||||
});
|
||||
}
|
||||
|
||||
getPresetFactoryForPreset(preset) {
|
||||
/**
|
||||
* Tries to load one preset. The input is either the module name of the preset,
|
||||
* a function, or an object
|
||||
*/
|
||||
loadPreset(preset, options, meta) {
|
||||
let presetFactory = preset;
|
||||
if (typeof presetFactory === "string") {
|
||||
presetFactory = require(presetFactory);
|
||||
}
|
||||
|
||||
// If the imported preset is a transpiled ES2015 module
|
||||
if (typeof presetFactory === "object" && presetFactory.__esModule) {
|
||||
if (presetFactory.default) {
|
||||
presetFactory = presetFactory.default;
|
||||
@ -299,12 +300,17 @@ export default class OptionManager {
|
||||
}
|
||||
}
|
||||
|
||||
// Allow simple object exports
|
||||
if (typeof presetFactory === "object") {
|
||||
return presetFactory;
|
||||
}
|
||||
|
||||
if (typeof presetFactory !== "function") {
|
||||
// eslint-disable-next-line max-len
|
||||
throw new Error(`Unsupported preset format: ${typeof presetFactory}. Expected preset to return a function.`);
|
||||
}
|
||||
|
||||
return presetFactory;
|
||||
return presetFactory(context, options, meta);
|
||||
}
|
||||
|
||||
normaliseOptions() {
|
||||
|
||||
6
packages/babel-core/test/fixtures/option-manager/presets/es2015_invalid.js
vendored
Normal file
6
packages/babel-core/test/fixtures/option-manager/presets/es2015_invalid.js
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
// from code:
|
||||
// export default "string";
|
||||
'use strict';
|
||||
|
||||
exports.__esModule = true;
|
||||
exports.default = "string";
|
||||
@ -76,11 +76,12 @@ describe("option-manager", () => {
|
||||
}
|
||||
|
||||
presetTest("es5_function");
|
||||
presetTest("es5_object");
|
||||
presetTest("es2015_default_function");
|
||||
presetTest("es2015_default_object");
|
||||
|
||||
presetThrowsTest("es5", /Expected preset to return a function./);
|
||||
presetThrowsTest("es2015_default", /Expected preset to return a function./);
|
||||
presetThrowsTest("es2015_named", /Preset must export a default export when using ES6 modules/);
|
||||
presetThrowsTest("es2015_invalid", /Unsupported preset format: string/);
|
||||
presetThrowsTest("es5_invalid", /Unsupported preset format: string/);
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user