From 2e194026fe28974071d7e85c882739a51f73d118 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Fri, 27 May 2022 21:33:04 +0200 Subject: [PATCH] Fix `@babel/transform-typescript` compatibility with Next.js --- .../babel-plugin-transform-typescript/src/index.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/babel-plugin-transform-typescript/src/index.ts b/packages/babel-plugin-transform-typescript/src/index.ts index ef57f17e9e34..0468c6e04d6b 100644 --- a/packages/babel-plugin-transform-typescript/src/index.ts +++ b/packages/babel-plugin-transform-typescript/src/index.ts @@ -575,8 +575,16 @@ export default declare((api, opts: Options) => { [process.env.BABEL_8_BREAKING ? "TSNonNullExpression|TSInstantiationExpression" - : // This has been introduced in Babel 7.18.0 - t.tsInstantiationExpression + : /* This has been introduced in Babel 7.18.0 + We use api.types.* and not t.* for feature detection, + because the Babel version that is running this plugin + (where we check if the visitor is valid) might be different + from the Babel version that we resolve with `import "@babel/core"`. + This happens, for example, with Next.js that bundled `@babel/core` + but allows loading unbundled plugin (which cannot obviously import + the bundled `@babel/core` version). + */ + api.types.tsInstantiationExpression ? "TSNonNullExpression|TSInstantiationExpression" : "TSNonNullExpression"]( path: NodePath,