diff --git a/packages/babel-core/src/transformation/file/index.js b/packages/babel-core/src/transformation/file/index.js index 6c2ab6044f..a4bd43672b 100644 --- a/packages/babel-core/src/transformation/file/index.js +++ b/packages/babel-core/src/transformation/file/index.js @@ -178,49 +178,13 @@ export default class File extends Store { return source; } - addImport( - source: string, - imported?: string = "", - name?: string = imported, - ): Object | null { - const prependDeclaration = ( - specifiers: Array, - ): void => { - const declar = t.importDeclaration(specifiers, t.stringLiteral(source)); - declar._blockHoist = 3; - - this.path.unshiftContainer("body", declar); - }; - - // import "module-name"; - if (!imported) { - prependDeclaration([]); - return null; - } - - const alias = `${source}:${imported}`; - let id = this.dynamicImportIds[alias]; - - if (!id) { - source = this.resolveModuleSource(source); - id = this.dynamicImportIds[alias] = this.scope.generateUidIdentifier( - name, - ); - - const specifiers = []; - - if (imported === "*") { - specifiers.push(t.importNamespaceSpecifier(id)); - } else if (imported === "default") { - specifiers.push(t.importDefaultSpecifier(id)); - } else { - specifiers.push(t.importSpecifier(id, t.identifier(imported))); - } - - prependDeclaration(specifiers); - } - - return t.identifier(id.name); + addImport() { + throw new Error( + "This API has been removed. If you looking for this " + + "functionality in Babel 7, you should import the " + + "'babel-helper-module-imports' module and use the functions exposed " + + " from that module, such as 'addNamed' or 'addDefault'.", + ); } addHelper(name: string): Object { diff --git a/packages/babel-core/test/fixtures/plugins/file-add-import/exec.js b/packages/babel-core/test/fixtures/plugins/file-add-import/exec.js deleted file mode 100644 index 72108171cb..0000000000 --- a/packages/babel-core/test/fixtures/plugins/file-add-import/exec.js +++ /dev/null @@ -1,30 +0,0 @@ -var res = transform("", { - plugins: [ - function (b) { - return { - visitor: { - Program: function(path, state) { - var file = state.file; - file.addImport("import-star", "*", "lib"); - file.addImport("import-default", "default", "foo"); - file.addImport("import-alias", "bar", "baz"); - file.addImport("import-default-alias", "quux"); - file.addImport("import-empty", ""); - file.addImport("import-none"); - } - } - }; - } - ] -}); - -var expected = multiline([ - 'import "import-none";', - 'import "import-empty";', - 'import { quux as _quux } from "import-default-alias";', - 'import { bar as _baz } from "import-alias";', - 'import _foo from "import-default";', - 'import * as _lib from "import-star";', -]); - -assert.equal(res.code, expected);