diff --git a/.all-contributorsrc b/.all-contributorsrc index 18d7fd6..29c8845 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -103,6 +103,15 @@ "doc", "plugin" ] + }, + { + "login": "citycide", + "name": "Bo Lingen", + "avatar_url": "https://avatars2.githubusercontent.com/u/16605186?v=4", + "profile": "https://github.com/citycide", + "contributions": [ + "code" + ] } ] } diff --git a/README.md b/README.md index d819e87..ab23374 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Enables zero-config, importable babel plugins [![downloads][downloads-badge]][npmchart] [![MIT License][license-badge]][license] -[![All Contributors](https://img.shields.io/badge/all_contributors-10-orange.svg?style=flat-square)](#contributors) +[![All Contributors](https://img.shields.io/badge/all_contributors-11-orange.svg?style=flat-square)](#contributors) [![PRs Welcome][prs-badge]][prs] [![Donate][donate-badge]][donate] [![Code of Conduct][coc-badge]][coc] @@ -336,7 +336,7 @@ Thanks goes to these people ([emoji key][emojis]): | [
Kent C. Dodds](https://kentcdodds.com)
[💻](https://github.com/kentcdodds/babel-plugin-macros/commits?author=kentcdodds "Code") [📖](https://github.com/kentcdodds/babel-plugin-macros/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/kentcdodds/babel-plugin-macros/commits?author=kentcdodds "Tests") | [
Sunil Pai](https://github.com/threepointone)
[🤔](#ideas-threepointone "Ideas, Planning, & Feedback") | [
Stephen Scott](http://suchipi.com/)
[💬](#question-suchipi "Answering Questions") [📖](https://github.com/kentcdodds/babel-plugin-macros/commits?author=suchipi "Documentation") | [
Michiel Dral](http://twitter.com/dralletje)
[🤔](#ideas-dralletje "Ideas, Planning, & Feedback") | [
Kye Hohenberger](https://github.com/tkh44)
[🤔](#ideas-tkh44 "Ideas, Planning, & Feedback") | [
Mitchell Hamilton](https://hamil.town)
[💻](https://github.com/kentcdodds/babel-plugin-macros/commits?author=mitchellhamilton "Code") [⚠️](https://github.com/kentcdodds/babel-plugin-macros/commits?author=mitchellhamilton "Tests") | [
Justin Hall](https://github.com/wKovacs64)
[📖](https://github.com/kentcdodds/babel-plugin-macros/commits?author=wKovacs64 "Documentation") | | :---: | :---: | :---: | :---: | :---: | :---: | :---: | -| [
Brian Pedersen](https://github.com/PiereDome)
[💻](https://github.com/kentcdodds/babel-plugin-macros/commits?author=PiereDome "Code") [📖](https://github.com/kentcdodds/babel-plugin-macros/commits?author=PiereDome "Documentation") | [
Andrew Palm](https://github.com/apalm)
[💻](https://github.com/kentcdodds/babel-plugin-macros/commits?author=apalm "Code") | [
Michael Hsu](https://michaelhsu.tw/)
[📖](https://github.com/kentcdodds/babel-plugin-macros/commits?author=evenchange4 "Documentation") [🔌](#plugin-evenchange4 "Plugin/utility libraries") | +| [
Brian Pedersen](https://github.com/PiereDome)
[💻](https://github.com/kentcdodds/babel-plugin-macros/commits?author=PiereDome "Code") [📖](https://github.com/kentcdodds/babel-plugin-macros/commits?author=PiereDome "Documentation") | [
Andrew Palm](https://github.com/apalm)
[💻](https://github.com/kentcdodds/babel-plugin-macros/commits?author=apalm "Code") | [
Michael Hsu](https://michaelhsu.tw/)
[📖](https://github.com/kentcdodds/babel-plugin-macros/commits?author=evenchange4 "Documentation") [🔌](#plugin-evenchange4 "Plugin/utility libraries") | [
Bo Lingen](https://github.com/citycide)
[💻](https://github.com/kentcdodds/babel-plugin-macros/commits?author=citycide "Code") | diff --git a/src/index.js b/src/index.js index 19d174b..3d56ffe 100644 --- a/src/index.js +++ b/src/index.js @@ -71,38 +71,44 @@ function macrosPlugin(babel) { }) path.remove() }, - CallExpression(path, state) { - const isMacros = looksLike(path, { - node: { - callee: { - type: 'Identifier', - name: 'require', + VariableDeclaration(path, state) { + const isMacros = child => + looksLike(child, { + node: { + init: { + callee: { + type: 'Identifier', + name: 'require', + }, + arguments: args => + args.length === 1 && macrosRegex.test(args[0].value), + }, }, - arguments: args => - args.length === 1 && macrosRegex.test(args[0].value), - }, - parent: { - type: 'VariableDeclarator', - }, - }) - if (!isMacros) { - return - } - const imports = path.parent.id.name - ? [{localName: path.parent.id.name, importedName: 'default'}] - : path.parent.id.properties.map(property => ({ - localName: property.value.name, - importedName: property.key.name, - })) - const source = path.node.arguments[0].value - applyMacros({ - path, - imports, - source, - state, - babel, - }) - path.parentPath.remove() + }) + + path + .get('declarations') + .filter(isMacros) + .forEach(child => { + const imports = child.node.id.name + ? [{localName: child.node.id.name, importedName: 'default'}] + : child.node.id.properties.map(property => ({ + localName: property.value.name, + importedName: property.key.name, + })) + + const call = child.get('init') + const source = call.node.arguments[0].value + applyMacros({ + path: call, + imports, + source, + state, + babel, + }) + + child.remove() + }) }, }, }