From 32857820ca1d29797ff9879b0a654b3c2cc34ab6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Wed, 7 Oct 2020 20:19:21 +0200 Subject: [PATCH] Fix presets ordering --- packages/babel-core/src/config/full.js | 24 +++++++++++++++---- packages/babel-core/test/api.js | 2 ++ .../config/complex-plugin-config/.babelrc | 6 +++++ .../config/complex-plugin-config/twentyone.js | 1 + .../config/complex-plugin-config/twentytwo.js | 1 + 5 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 packages/babel-core/test/fixtures/config/complex-plugin-config/twentyone.js create mode 100644 packages/babel-core/test/fixtures/config/complex-plugin-config/twentytwo.js diff --git a/packages/babel-core/src/config/full.js b/packages/babel-core/src/config/full.js index 5a3f1d9b6fe5..73745caa759f 100644 --- a/packages/babel-core/src/config/full.js +++ b/packages/babel-core/src/config/full.js @@ -103,10 +103,20 @@ export default gensync<[any], ResolvedConfig | null>(function* loadFullConfig( const descriptor = rawPresets[i]; if (descriptor.options !== false) { try { - presets.push({ - preset: yield* loadPresetDescriptor(descriptor, context), - pass: descriptor.ownPass ? [] : pluginDescriptorsPass, - }); + // Presets normally run in reverse order, but if they + // have their own pass they run after the presets + // in the previous pass. + if (descriptor.ownPass) { + presets.push({ + preset: yield* loadPresetDescriptor(descriptor, context), + pass: [], + }); + } else { + presets.unshift({ + preset: yield* loadPresetDescriptor(descriptor, context), + pass: pluginDescriptorsPass, + }); + } } catch (e) { if (e.code === "BABEL_UNKNOWN_OPTION") { checkNoUnwrappedItemOptionPairs(rawPresets, i, "preset", e); @@ -129,7 +139,7 @@ export default gensync<[any], ResolvedConfig | null>(function* loadFullConfig( for (const { preset, pass } of presets) { if (!preset) return true; - pass.unshift(...preset.plugins); + pass.push(...preset.plugins); const ignored = yield* recursePresetDescriptors(preset.presets, pass); if (ignored) return true; @@ -178,6 +188,10 @@ export default gensync<[any], ResolvedConfig | null>(function* loadFullConfig( .map(plugins => ({ plugins })); opts.passPerPreset = opts.presets.length > 0; + if (context.filename?.includes("RuntimeErrorContaine")) { + console.log(initialPluginsDescriptors.map(p => p.key)); + } + return { options: opts, passes: passes, diff --git a/packages/babel-core/test/api.js b/packages/babel-core/test/api.js index 384ed7421872..bf714bc6e9f5 100644 --- a/packages/babel-core/test/api.js +++ b/packages/babel-core/test/api.js @@ -423,6 +423,8 @@ describe("api", function () { "argone;", "five;", "six;", + "twentyone;", + "twentytwo;", "three;", "four;", "nineteen;", diff --git a/packages/babel-core/test/fixtures/config/complex-plugin-config/.babelrc b/packages/babel-core/test/fixtures/config/complex-plugin-config/.babelrc index a9c3ab04b842..ede759ab7c6b 100644 --- a/packages/babel-core/test/fixtures/config/complex-plugin-config/.babelrc +++ b/packages/babel-core/test/fixtures/config/complex-plugin-config/.babelrc @@ -14,6 +14,12 @@ "./five", "./six", ], + presets: [{ + plugins: [ + "./twentyone", + "./twentytwo", + ] + }] }, { passPerPreset: true, presets: [{ diff --git a/packages/babel-core/test/fixtures/config/complex-plugin-config/twentyone.js b/packages/babel-core/test/fixtures/config/complex-plugin-config/twentyone.js new file mode 100644 index 000000000000..1bcd01492698 --- /dev/null +++ b/packages/babel-core/test/fixtures/config/complex-plugin-config/twentyone.js @@ -0,0 +1 @@ +module.exports = require("./plugin")("twentyone"); diff --git a/packages/babel-core/test/fixtures/config/complex-plugin-config/twentytwo.js b/packages/babel-core/test/fixtures/config/complex-plugin-config/twentytwo.js new file mode 100644 index 000000000000..3e7ddde2ece5 --- /dev/null +++ b/packages/babel-core/test/fixtures/config/complex-plugin-config/twentytwo.js @@ -0,0 +1 @@ +module.exports = require("./plugin")("twentytwo");