diff --git a/packages/babel-plugin-transform-typescript/src/index.ts b/packages/babel-plugin-transform-typescript/src/index.ts index a14c66029c..066759f1d6 100644 --- a/packages/babel-plugin-transform-typescript/src/index.ts +++ b/packages/babel-plugin-transform-typescript/src/index.ts @@ -465,12 +465,26 @@ export default declare((api, opts) => { transpileEnum(path, t); }, - TSImportEqualsDeclaration(path) { - throw path.buildCodeFrameError( - "`import =` is not supported by @babel/plugin-transform-typescript\n" + - "Please consider using " + - "`import from '';` alongside " + - "Typescript's --allowSyntheticDefaultImports option.", + TSImportEqualsDeclaration(path: NodePath) { + if (t.isTSExternalModuleReference(path.node.moduleReference)) { + // import alias = require('foo'); + throw path.buildCodeFrameError( + `\`import ${path.node.id.name} = require('${path.node.moduleReference.expression.value}')\` ` + + "is not supported by @babel/plugin-transform-typescript\n" + + "Please consider using " + + `\`import ${path.node.id.name} from '${path.node.moduleReference.expression.value}';\` alongside ` + + "Typescript's --allowSyntheticDefaultImports option.", + ); + } + + // import alias = Namespace; + path.replaceWith( + t.variableDeclaration("var", [ + t.variableDeclarator( + path.node.id, + entityNameToExpr(path.node.moduleReference), + ), + ]), ); }, @@ -519,6 +533,14 @@ export default declare((api, opts) => { }, }; + function entityNameToExpr(node: t.TSEntityName): t.Expression { + if (t.isTSQualifiedName(node)) { + return t.memberExpression(entityNameToExpr(node.left), node.right); + } + + return node; + } + function visitPattern({ node }) { if (node.typeAnnotation) node.typeAnnotation = null; if (t.isIdentifier(node) && node.optional) node.optional = null; diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/import=/options.json b/packages/babel-plugin-transform-typescript/test/fixtures/imports/import=/options.json index 596239c2e1..fc8cd41e40 100644 --- a/packages/babel-plugin-transform-typescript/test/fixtures/imports/import=/options.json +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/import=/options.json @@ -1,3 +1,3 @@ { - "throws": "`import =` is not supported by @babel/plugin-transform-typescript\nPlease consider using `import from '';` alongside Typescript's --allowSyntheticDefaultImports option." + "throws": "`import lib = require('lib')` is not supported by @babel/plugin-transform-typescript\nPlease consider using `import lib from 'lib';` alongside Typescript's --allowSyntheticDefaultImports option." } diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/namespace/alias/input.ts b/packages/babel-plugin-transform-typescript/test/fixtures/namespace/alias/input.ts new file mode 100644 index 0000000000..94eae80eb8 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/namespace/alias/input.ts @@ -0,0 +1,22 @@ +declare module LongNameModule { + export type SomeType = number; + export const foo: number; + module Inner { + export type T = string; + export const bar: boolean; + } +} + +import * as babel from '@babel/core'; + +/** type only */ +import b = babel; +import AliasModule = LongNameModule; + +const some: AliasModule.SomeType = 3; +const bar = AliasModule.foo; +const baz = AliasModule.Inner.bar; +let str: LongNameModule.Inner.T; +let node: b.OptionManager; + +console.log(some); diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/namespace/alias/output.mjs b/packages/babel-plugin-transform-typescript/test/fixtures/namespace/alias/output.mjs new file mode 100644 index 0000000000..4b824fe832 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/namespace/alias/output.mjs @@ -0,0 +1,11 @@ +import * as babel from '@babel/core'; +/** type only */ + +var b = babel; +var AliasModule = LongNameModule; +const some = 3; +const bar = AliasModule.foo; +const baz = AliasModule.Inner.bar; +let str; +let node; +console.log(some);