/
option-manager.js
276 lines (246 loc) 路 7.62 KB
/
option-manager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import { loadOptions as loadOptionsOrig } from "../lib";
import path from "path";
function loadOptions(opts) {
return loadOptionsOrig({
cwd: __dirname,
...opts,
});
}
describe("option-manager", () => {
it("throws for babel 5 plugin", () => {
return expect(() => {
loadOptions({
plugins: [({ Plugin }) => new Plugin("object-assign", {})],
});
}).toThrow(/Babel 5 plugin is being run with an unsupported Babel/);
});
describe("config plugin/preset flattening and overriding", () => {
function makePlugin() {
const calls = [];
const plugin = (api, opts) => {
calls.push(opts);
return {};
};
return { plugin, calls };
}
it("should throw when an option is provided as a preset", () => {
expect(() => {
loadOptions({
presets: [
"./fixtures/option-manager/babel-preset-bar",
{ useBuiltIns: "entry" },
],
});
}).toThrowErrorMatchingSnapshot();
});
it("should throw when an option is provided as a plugin", () => {
expect(() => {
loadOptions({
plugins: [
"./fixtures/option-manager/babel-plugin-foo",
{ useSpread: true },
],
});
}).toThrowErrorMatchingSnapshot();
});
it("should throw when an option is following a preset", () => {
expect(() => {
loadOptions({
presets: [
"./fixtures/option-manager/babel-plugin-foo",
"./fixtures/option-manager/babel-preset-bar",
{ useSpread: true },
],
});
}).toThrowErrorMatchingSnapshot();
});
it("should not throw when a preset string followed by valid preset object", () => {
const { plugin } = makePlugin("my-plugin");
expect(
loadOptions({
presets: [
"@babel/env",
{ plugins: [[plugin, undefined, "my-plugin"]] },
],
}),
).toBeTruthy();
});
it("should throw if a plugin is repeated, with information about the repeated plugin", () => {
const { calls, plugin } = makePlugin("my-plugin");
expect(() => {
loadOptions({
plugins: [
[plugin, undefined, "my-plugin"],
[plugin, undefined, "my-plugin"],
],
});
}).toThrow(
/Duplicate plugin\/preset detected.*Duplicates detected are.*my-plugin.*my-plugin/ms,
);
expect(calls).toEqual([]);
});
it("throws for null options", () => {
const { calls, plugin } = makePlugin();
expect(() => {
loadOptions({
plugins: [[plugin, null]],
});
}).toThrow(".plugins[0][1] must be an object, false, or undefined");
expect(calls).toEqual([]);
});
it("should not throw if a repeated plugin has a different name", () => {
const { calls: calls1, plugin: plugin1 } = makePlugin();
const { calls: calls2, plugin: plugin2 } = makePlugin();
loadOptions({
plugins: [
[plugin1, { arg: 1 }],
[plugin2, { arg: 2 }, "some-name"],
],
});
expect(calls1).toEqual([{ arg: 1 }]);
expect(calls2).toEqual([{ arg: 2 }]);
});
it("should merge .env[] plugins with parent presets", () => {
const { calls: calls1, plugin: plugin1 } = makePlugin();
const { calls: calls2, plugin: plugin2 } = makePlugin();
loadOptions({
envName: "test",
plugins: [[plugin1, { arg: 1 }]],
env: {
test: {
plugins: [
[plugin1, { arg: 3 }],
[plugin2, { arg: 2 }],
],
},
},
});
expect(calls1).toEqual([{ arg: 3 }]);
expect(calls2).toEqual([{ arg: 2 }]);
});
it("should throw if a preset is repeated", () => {
const { calls, plugin: preset } = makePlugin();
expect(() => {
loadOptions({
presets: [preset, preset],
});
}).toThrow(/Duplicate plugin\/preset detected/);
expect(calls).toEqual([]);
});
it("should not throw if a repeated preset has a different name", () => {
const { calls: calls1, plugin: preset1 } = makePlugin();
const { calls: calls2, plugin: preset2 } = makePlugin();
loadOptions({
presets: [
[preset1, { arg: 1 }],
[preset2, { arg: 2 }, "some-name"],
],
});
expect(calls1).toEqual([{ arg: 1 }]);
expect(calls2).toEqual([{ arg: 2 }]);
});
it("should merge .env[] presets with parent presets", () => {
const { calls: calls1, plugin: preset1 } = makePlugin();
const { calls: calls2, plugin: preset2 } = makePlugin();
loadOptions({
envName: "test",
presets: [[preset1, { arg: 1 }]],
env: {
test: {
presets: [
[preset1, { arg: 3 }],
[preset2, { arg: 2 }],
],
},
},
});
expect(calls1).toEqual([{ arg: 3 }]);
expect(calls2).toEqual([{ arg: 2 }]);
});
it("should not merge .env[] presets with parent presets when passPerPreset", () => {
const { calls: calls1, plugin: preset1 } = makePlugin();
const { calls: calls2, plugin: preset2 } = makePlugin();
loadOptions({
envName: "test",
passPerPreset: true,
presets: [[preset1, { arg: 1 }]],
env: {
test: {
presets: [
[preset1, { arg: 3 }],
[preset2, { arg: 2 }],
],
},
},
});
expect(calls1).toEqual([{ arg: 1 }, { arg: 3 }]);
expect(calls2).toEqual([{ arg: 2 }]);
});
});
describe("mergeOptions", () => {
it("throws for removed babel 5 options", () => {
return expect(() => {
loadOptions({
randomOption: true,
});
}).toThrow(/Unknown option: .randomOption/);
});
it("throws for removed babel 5 options", () => {
return expect(() => {
loadOptions({
auxiliaryComment: true,
blacklist: true,
});
}).toThrow(
// eslint-disable-next-line max-len
/Using removed Babel 5 option: .auxiliaryComment - Use `auxiliaryCommentBefore` or `auxiliaryCommentAfter`/,
);
});
it("throws for resolved but erroring preset", () => {
return expect(() => {
loadOptions({
presets: [
path.join(__dirname, "fixtures/option-manager/not-a-preset"),
],
});
}).toThrow(
/While processing: .*option-manager(?:\/|\\\\)not-a-preset\.js/,
);
});
});
describe("presets", function() {
function presetTest(name) {
it(name, function() {
const options = loadOptions({
presets: [
path.join(__dirname, "fixtures/option-manager/presets", name),
],
});
expect(Array.isArray(options.plugins)).toBe(true);
expect(options.plugins).toHaveLength(1);
expect(options.presets).toHaveLength(0);
});
}
function presetThrowsTest(name, msg) {
it(name, function() {
expect(() =>
loadOptions({
presets: [
path.join(__dirname, "fixtures/option-manager/presets", name),
],
}),
).toThrow(msg);
});
}
presetTest("es5_function");
presetTest("es5_object");
presetTest("es2015_default_function");
presetTest("es2015_default_object");
presetThrowsTest(
"es2015_named",
/Must export a default export when using ES6 modules/,
);
presetThrowsTest("es2015_invalid", /Unsupported format: string/);
presetThrowsTest("es5_invalid", /Unsupported format: string/);
});
});