diff --git a/packages/babel-plugin-transform-async-to-module-method/package.json b/packages/babel-plugin-transform-async-to-module-method/package.json index b05dbf0498..2821e0c377 100644 --- a/packages/babel-plugin-transform-async-to-module-method/package.json +++ b/packages/babel-plugin-transform-async-to-module-method/package.json @@ -10,6 +10,7 @@ ], "dependencies": { "babel-helper-remap-async-to-generator": "7.0.0-beta.2", + "babel-helper-module-imports": "7.0.0-beta.2", "babel-plugin-syntax-async-functions": "7.0.0-beta.0", "babel-types": "7.0.0-beta.2" }, diff --git a/packages/babel-plugin-transform-async-to-module-method/src/index.js b/packages/babel-plugin-transform-async-to-module-method/src/index.js index 1eda016ef5..d32f9b0a89 100644 --- a/packages/babel-plugin-transform-async-to-module-method/src/index.js +++ b/packages/babel-plugin-transform-async-to-module-method/src/index.js @@ -1,7 +1,9 @@ import remapAsyncToGenerator from "babel-helper-remap-async-to-generator"; import syntaxAsyncFunctions from "babel-plugin-syntax-async-functions"; -export default function() { +import { addNamed } from "babel-helper-module-imports"; + +export default function({ types: t }) { return { inherits: syntaxAsyncFunctions, @@ -9,8 +11,17 @@ export default function() { Function(path, state) { if (!path.node.async || path.node.generator) return; + const { module, method } = state.opts; + + let wrapAsync = state.methodWrapper; + if (wrapAsync) { + wrapAsync = t.cloneDeep(wrapAsync); + } else { + wrapAsync = state.methodWrapper = addNamed(path, method, module); + } + remapAsyncToGenerator(path, state.file, { - wrapAsync: state.addImport(state.opts.module, state.opts.method), + wrapAsync, }); }, }, diff --git a/packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/arrow-function/expected.js b/packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/arrow-function/expected.js index fd2485114e..a5e45c929d 100644 --- a/packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/arrow-function/expected.js +++ b/packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/arrow-function/expected.js @@ -1,4 +1,4 @@ -import { coroutine as _coroutine } from "bluebird"; +var _coroutine = require("bluebird").coroutine; _coroutine(function* () { yield foo(); diff --git a/packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/class/expected.js b/packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/class/expected.js index 2bcbd54c95..c169c87cce 100644 --- a/packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/class/expected.js +++ b/packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/class/expected.js @@ -1,4 +1,4 @@ -import { coroutine as _coroutine } from "bluebird"; +var _coroutine = require("bluebird").coroutine; class Foo { foo() { diff --git a/packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/expression/expected.js b/packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/expression/expected.js index 168d702dc6..e347008cbb 100644 --- a/packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/expression/expected.js +++ b/packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/expression/expected.js @@ -1,4 +1,4 @@ -import { coroutine as _coroutine } from "bluebird"; +var _coroutine = require("bluebird").coroutine; var foo = (() => { var _ref = _coroutine(function* () { diff --git a/packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/named-expression/expected.js b/packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/named-expression/expected.js index 640f0f576b..d3bfbda8a3 100644 --- a/packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/named-expression/expected.js +++ b/packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/named-expression/expected.js @@ -1,4 +1,4 @@ -import { coroutine as _coroutine } from "bluebird"; +var _coroutine = require("bluebird").coroutine; var foo = (() => { var _ref = _coroutine(function* () { diff --git a/packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/statement/expected.js b/packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/statement/expected.js index ecbbba1715..c5dc8a29ef 100644 --- a/packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/statement/expected.js +++ b/packages/babel-plugin-transform-async-to-module-method/test/fixtures/bluebird-coroutines/statement/expected.js @@ -1,4 +1,4 @@ -import { coroutine as _coroutine } from "bluebird"; +var _coroutine = require("bluebird").coroutine; let foo = (() => { var _ref = _coroutine(function* () {