Pass assumptions set in presets to plugins (#13321)
This commit is contained in:
parent
0d0950f341
commit
f85c6437c5
@ -66,6 +66,7 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/helper-transform-fixture-test-runner": "workspace:*",
|
||||
"@babel/plugin-transform-modules-commonjs": "workspace:*",
|
||||
"@types/convert-source-map": "^1.5.1",
|
||||
"@types/debug": "^4.1.0",
|
||||
"@types/resolve": "^1.3.2",
|
||||
|
||||
@ -68,10 +68,9 @@ export default gensync<(inputOpts: unknown) => ResolvedConfig | null>(
|
||||
throw new Error("Assertion failure - plugins and presets exist");
|
||||
}
|
||||
|
||||
const pluginContext: Context.FullPlugin = {
|
||||
const presetContext: Context.FullPreset = {
|
||||
...context,
|
||||
targets: options.targets,
|
||||
assumptions: options.assumptions ?? {},
|
||||
};
|
||||
|
||||
const toDescriptor = (item: PluginItem) => {
|
||||
@ -110,7 +109,7 @@ export default gensync<(inputOpts: unknown) => ResolvedConfig | null>(
|
||||
presets.push({
|
||||
preset: yield* loadPresetDescriptor(
|
||||
descriptor,
|
||||
pluginContext,
|
||||
presetContext,
|
||||
),
|
||||
pass: [],
|
||||
});
|
||||
@ -118,7 +117,7 @@ export default gensync<(inputOpts: unknown) => ResolvedConfig | null>(
|
||||
presets.unshift({
|
||||
preset: yield* loadPresetDescriptor(
|
||||
descriptor,
|
||||
pluginContext,
|
||||
presetContext,
|
||||
),
|
||||
pass: pluginDescriptorsPass,
|
||||
});
|
||||
@ -168,6 +167,11 @@ export default gensync<(inputOpts: unknown) => ResolvedConfig | null>(
|
||||
const opts: any = optionDefaults;
|
||||
mergeOptions(opts, options);
|
||||
|
||||
const pluginContext: Context.FullPlugin = {
|
||||
...presetContext,
|
||||
assumptions: opts.assumptions ?? {},
|
||||
};
|
||||
|
||||
yield* enhanceError(context, function* loadPluginDescriptors() {
|
||||
pluginDescriptorsByPass[0].unshift(...initialPluginsDescriptors);
|
||||
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
import path from "path";
|
||||
import { fileURLToPath } from "url";
|
||||
import { loadOptions as loadOptionsOrig, transformSync } from "../lib";
|
||||
import pluginCommonJS from "@babel/plugin-transform-modules-commonjs";
|
||||
|
||||
const cwd = path.dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
@ -101,6 +102,63 @@ describe("assumptions", () => {
|
||||
expect(assumptionFn).toBeUndefined();
|
||||
});
|
||||
|
||||
// https://github.com/babel/babel/issues/13316
|
||||
describe("assumptions set in presets are visible from plugins - #13316", () => {
|
||||
function presetEnumerableModuleMeta() {
|
||||
return { assumptions: { enumerableModuleMeta: true } };
|
||||
}
|
||||
|
||||
function pluginExtractEnumerableModuleMeta(api, options) {
|
||||
options.enumerableModuleMeta = api.assumption("enumerableModuleMeta");
|
||||
return { visitor: {} };
|
||||
}
|
||||
|
||||
it("plugin defined outside preset", () => {
|
||||
const ref = {};
|
||||
|
||||
loadOptions({
|
||||
configFile: false,
|
||||
presets: [presetEnumerableModuleMeta],
|
||||
plugins: [[pluginExtractEnumerableModuleMeta, ref]],
|
||||
});
|
||||
|
||||
expect(ref.enumerableModuleMeta).toBe(true);
|
||||
});
|
||||
|
||||
it("plugin defined inside preset", () => {
|
||||
const ref = {};
|
||||
|
||||
loadOptions({
|
||||
configFile: false,
|
||||
presets: [
|
||||
() => ({
|
||||
assumptions: { enumerableModuleMeta: true },
|
||||
plugins: [[pluginExtractEnumerableModuleMeta, ref]],
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
expect(ref.enumerableModuleMeta).toBe(true);
|
||||
});
|
||||
|
||||
it("integration", () => {
|
||||
const { code } = transformSync(`export const foo = 1;`, {
|
||||
configFile: false,
|
||||
presets: [presetEnumerableModuleMeta],
|
||||
plugins: [pluginCommonJS],
|
||||
});
|
||||
|
||||
expect(code).toMatchInlineSnapshot(`
|
||||
"\\"use strict\\";
|
||||
|
||||
exports.__esModule = true;
|
||||
exports.foo = void 0;
|
||||
const foo = 1;
|
||||
exports.foo = foo;"
|
||||
`);
|
||||
});
|
||||
});
|
||||
|
||||
describe("plugin cache", () => {
|
||||
const makePlugin = () =>
|
||||
jest.fn(api => {
|
||||
|
||||
@ -214,6 +214,7 @@ __metadata:
|
||||
"@babel/helper-transform-fixture-test-runner": "workspace:*"
|
||||
"@babel/helpers": "workspace:^7.14.0"
|
||||
"@babel/parser": "workspace:^7.14.2"
|
||||
"@babel/plugin-transform-modules-commonjs": "workspace:*"
|
||||
"@babel/template": "workspace:^7.12.13"
|
||||
"@babel/traverse": "workspace:^7.14.2"
|
||||
"@babel/types": "workspace:^7.14.2"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user