Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 165b735

Browse files
Fix reinstantiation of inherited plugins (#14241)
* Add test for 14233 * Fix it
1 parent 1deccb0 commit 165b735

File tree

3 files changed

+73
-8
lines changed

3 files changed

+73
-8
lines changed

packages/babel-core/src/config/full.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type LoadedDescriptor = {
3737
options: {};
3838
dirname: string;
3939
alias: string;
40-
externalDependencies: DeepArray<string>;
40+
externalDependencies: ReadonlyDeepArray<string>;
4141
};
4242

4343
export type { InputOptions } from "./validation/options";
@@ -319,7 +319,13 @@ const makeDescriptorLoader = <Context, API>(
319319
throw new Error(error);
320320
}
321321

322-
return { value: item, options, dirname, alias, externalDependencies };
322+
return {
323+
value: item,
324+
options,
325+
dirname,
326+
alias,
327+
externalDependencies: freezeDeepArray(externalDependencies),
328+
};
323329
});
324330

325331
const pluginDescriptorLoader = makeDescriptorLoader<
@@ -397,7 +403,16 @@ const instantiatePlugin = makeWeakCache(function* (
397403
plugin.visitor || {},
398404
]);
399405

400-
externalDependencies.push(inherits.externalDependencies);
406+
if (inherits.externalDependencies.length > 0) {
407+
if (externalDependencies.length === 0) {
408+
externalDependencies = inherits.externalDependencies;
409+
} else {
410+
externalDependencies = freezeDeepArray([
411+
externalDependencies,
412+
inherits.externalDependencies,
413+
]);
414+
}
415+
}
401416
}
402417

403418
return new Plugin(plugin, options, alias, externalDependencies);
@@ -470,7 +485,7 @@ const instantiatePreset = makeWeakCacheSync(
470485
options: validate("preset", value),
471486
alias,
472487
dirname,
473-
externalDependencies: freezeDeepArray(externalDependencies),
488+
externalDependencies,
474489
};
475490
},
476491
);

packages/babel-core/src/config/plugin.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { finalize } from "./helpers/deep-array";
2-
import type { DeepArray, ReadonlyDeepArray } from "./helpers/deep-array";
2+
import type { ReadonlyDeepArray } from "./helpers/deep-array";
33
import type { PluginObject } from "./validation/plugins";
44

55
export default class Plugin {
@@ -20,7 +20,7 @@ export default class Plugin {
2020
plugin: PluginObject,
2121
options: {},
2222
key?: string,
23-
externalDependencies: DeepArray<string> = [],
23+
externalDependencies: ReadonlyDeepArray<string> = finalize([]),
2424
) {
2525
this.key = plugin.name || key;
2626

@@ -32,7 +32,6 @@ export default class Plugin {
3232
this.generatorOverride = plugin.generatorOverride;
3333

3434
this.options = options;
35-
36-
this.externalDependencies = finalize(externalDependencies);
35+
this.externalDependencies = externalDependencies;
3736
}
3837
}

packages/babel-core/test/external-dependencies.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,4 +230,55 @@ describe("externalDependencies", () => {
230230
).not.toThrow();
231231
});
232232
});
233+
234+
describe("regressions", () => {
235+
describe("#14233", () => {
236+
it("no external dependencies", () => {
237+
const code = `let a = 1`;
238+
239+
function pluginA(api) {
240+
api.cache.never();
241+
return { name: "plugin-a" };
242+
}
243+
function pluginB() {
244+
return { name: "plugin-b", inherits: pluginA };
245+
}
246+
247+
expect(() => {
248+
transform(code, { plugins: [pluginB] });
249+
250+
transform(code, { plugins: [pluginB] });
251+
}).not.toThrow();
252+
});
253+
254+
it("with external dependencies", () => {
255+
const code = `let a = 1`;
256+
257+
let pluginAdep = "./a-foo";
258+
259+
function pluginA(api) {
260+
api.cache.never();
261+
api.addExternalDependency(pluginAdep);
262+
return { name: "plugin-a" };
263+
}
264+
const pluginB = jest.fn(function pluginB(api) {
265+
api.cache.using(() => 0);
266+
api.addExternalDependency("./b-foo");
267+
return { name: "plugin-b", inherits: pluginA };
268+
});
269+
270+
const result1 = transform(code, { plugins: [pluginB] });
271+
pluginAdep = "./a-bar";
272+
const result2 = transform(code, { plugins: [pluginB] });
273+
274+
expect(pluginB).toHaveBeenCalledTimes(1);
275+
expect(new Set(result1.externalDependencies)).toEqual(
276+
new Set(["./a-foo", "./b-foo"]),
277+
);
278+
expect(new Set(result2.externalDependencies)).toEqual(
279+
new Set(["./a-bar", "./b-foo"]),
280+
);
281+
});
282+
});
283+
});
233284
});

0 commit comments

Comments
 (0)