|
1 | 1 | import path from "path"; |
2 | 2 | import { fileURLToPath } from "url"; |
3 | 3 | import { loadOptions as loadOptionsOrig, transformSync } from "../lib"; |
| 4 | +import pluginCommonJS from "@babel/plugin-transform-modules-commonjs"; |
4 | 5 |
|
5 | 6 | const cwd = path.dirname(fileURLToPath(import.meta.url)); |
6 | 7 |
|
@@ -101,6 +102,63 @@ describe("assumptions", () => { |
101 | 102 | expect(assumptionFn).toBeUndefined(); |
102 | 103 | }); |
103 | 104 |
|
| 105 | + // https://github.com/babel/babel/issues/13316 |
| 106 | + describe("assumptions set in presets are visible from plugins - #13316", () => { |
| 107 | + function presetEnumerableModuleMeta() { |
| 108 | + return { assumptions: { enumerableModuleMeta: true } }; |
| 109 | + } |
| 110 | + |
| 111 | + function pluginExtractEnumerableModuleMeta(api, options) { |
| 112 | + options.enumerableModuleMeta = api.assumption("enumerableModuleMeta"); |
| 113 | + return { visitor: {} }; |
| 114 | + } |
| 115 | + |
| 116 | + it("plugin defined outside preset", () => { |
| 117 | + const ref = {}; |
| 118 | + |
| 119 | + loadOptions({ |
| 120 | + configFile: false, |
| 121 | + presets: [presetEnumerableModuleMeta], |
| 122 | + plugins: [[pluginExtractEnumerableModuleMeta, ref]], |
| 123 | + }); |
| 124 | + |
| 125 | + expect(ref.enumerableModuleMeta).toBe(true); |
| 126 | + }); |
| 127 | + |
| 128 | + it("plugin defined inside preset", () => { |
| 129 | + const ref = {}; |
| 130 | + |
| 131 | + loadOptions({ |
| 132 | + configFile: false, |
| 133 | + presets: [ |
| 134 | + () => ({ |
| 135 | + assumptions: { enumerableModuleMeta: true }, |
| 136 | + plugins: [[pluginExtractEnumerableModuleMeta, ref]], |
| 137 | + }), |
| 138 | + ], |
| 139 | + }); |
| 140 | + |
| 141 | + expect(ref.enumerableModuleMeta).toBe(true); |
| 142 | + }); |
| 143 | + |
| 144 | + it("integration", () => { |
| 145 | + const { code } = transformSync(`export const foo = 1;`, { |
| 146 | + configFile: false, |
| 147 | + presets: [presetEnumerableModuleMeta], |
| 148 | + plugins: [pluginCommonJS], |
| 149 | + }); |
| 150 | + |
| 151 | + expect(code).toMatchInlineSnapshot(` |
| 152 | + "\\"use strict\\"; |
| 153 | +
|
| 154 | + exports.__esModule = true; |
| 155 | + exports.foo = void 0; |
| 156 | + const foo = 1; |
| 157 | + exports.foo = foo;" |
| 158 | + `); |
| 159 | + }); |
| 160 | + }); |
| 161 | + |
104 | 162 | describe("plugin cache", () => { |
105 | 163 | const makePlugin = () => |
106 | 164 | jest.fn(api => { |
|
0 commit comments