Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass assumptions set in presets to plugins #13321

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/babel-core/package.json
Expand Up @@ -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",
Expand Down
12 changes: 8 additions & 4 deletions packages/babel-core/src/config/full.ts
Expand Up @@ -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 = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will presetContext.assumptions be undefined?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but it doesn't matter because the preset API doesn't have a method to read assumptions anyway.

...context,
targets: options.targets,
assumptions: options.assumptions ?? {},
};

const toDescriptor = (item: PluginItem) => {
Expand Down Expand Up @@ -110,15 +109,15 @@ export default gensync<(inputOpts: unknown) => ResolvedConfig | null>(
presets.push({
preset: yield* loadPresetDescriptor(
descriptor,
pluginContext,
presetContext,
),
pass: [],
});
} else {
presets.unshift({
preset: yield* loadPresetDescriptor(
descriptor,
pluginContext,
presetContext,
),
pass: pluginDescriptorsPass,
});
Expand Down Expand Up @@ -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);

Expand Down
58 changes: 58 additions & 0 deletions packages/babel-core/test/assumptions.js
@@ -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));

Expand Down Expand Up @@ -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", () => {
nicolo-ribaudo marked this conversation as resolved.
Show resolved Hide resolved
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 => {
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Expand Up @@ -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"
Expand Down