Skip to content

Commit

Permalink
fix: remove babel-core dependency (#44)
Browse files Browse the repository at this point in the history
* Remove babel-core dependency

* Update .all-contributorsrc

* Update .all-contributorsrc

* Update README.md
  • Loading branch information
xjlim authored and Kent C. Dodds committed Oct 30, 2017
1 parent ec3df93 commit 99ea71a
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 14 deletions.
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@
"code",
"test"
]
},
{
"login": "xjlim",
"name": "Joe Lim",
"avatar_url": "https://avatars1.githubusercontent.com/u/10875678?v=4",
"profile": "https://joelim.me",
"contributions": [
"code"
]
}
]
}
25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Pre-evaluate code at build-time
[![downloads][downloads-badge]][npm-stat]
[![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]
Expand Down Expand Up @@ -74,6 +74,27 @@ or add `// @preval` comment at the top of a file.

See more below.

## Table of Contents

<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->

- [Installation](#installation)
- [Usage](#usage)
- [Configure with Babel](#configure-with-babel)
- [Use with [`babel-macros`][babel-macros]](#use-with-babel-macrosbabel-macros)
- [Examples](#examples)
- [Notes](#notes)
- [FAQ](#faq)
- [Inspiration](#inspiration)
- [Related Projects](#related-projects)
- [Other Solutions](#other-solutions)
- [Contributors](#contributors)
- [LICENSE](#license)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->


## Installation

This module is distributed via [npm][npm] which is bundled with [node][node] and
Expand Down Expand Up @@ -341,7 +362,7 @@ Thanks goes to these people ([emoji key][emojis]):
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub>Kent C. Dodds</sub>](https://kentcdodds.com)<br />[💻](https://github.com/kentcdodds/babel-plugin-preval/commits?author=kentcdodds "Code") [📖](https://github.com/kentcdodds/babel-plugin-preval/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/kentcdodds/babel-plugin-preval/commits?author=kentcdodds "Tests") | [<img src="https://avatars3.githubusercontent.com/u/5610087?v=3" width="100px;"/><br /><sub>Matt Phillips</sub>](http://mattphillips.io)<br />[💻](https://github.com/kentcdodds/babel-plugin-preval/commits?author=mattphillips "Code") [📖](https://github.com/kentcdodds/babel-plugin-preval/commits?author=mattphillips "Documentation") [⚠️](https://github.com/kentcdodds/babel-plugin-preval/commits?author=mattphillips "Tests") | [<img src="https://avatars1.githubusercontent.com/u/28024000?v=3" width="100px;"/><br /><sub>Philip Oliver</sub>](https://twitter.com/philipodev)<br />[🐛](https://github.com/kentcdodds/babel-plugin-preval/issues?q=author%3Aphilipodev "Bug reports") | [<img src="https://avatars2.githubusercontent.com/u/2109702?v=3" width="100px;"/><br /><sub>Sorin Davidoi</sub>](https://toot.cafe/@sorin)<br />[🐛](https://github.com/kentcdodds/babel-plugin-preval/issues?q=author%3Asorin-davidoi "Bug reports") [💻](https://github.com/kentcdodds/babel-plugin-preval/commits?author=sorin-davidoi "Code") [⚠️](https://github.com/kentcdodds/babel-plugin-preval/commits?author=sorin-davidoi "Tests") | [<img src="https://avatars4.githubusercontent.com/u/1127238?v=4" width="100px;"/><br /><sub>Luke Herrington</sub>](https://github.com/infiniteluke)<br />[💡](#example-infiniteluke "Examples") | [<img src="https://avatars4.githubusercontent.com/u/22868432?v=4" width="100px;"/><br /><sub>Lufty Wiranda</sub>](http://instagram.com/luftywiranda13)<br />[💻](https://github.com/kentcdodds/babel-plugin-preval/commits?author=luftywiranda13 "Code") | [<img src="https://avatars0.githubusercontent.com/u/3877773?v=4" width="100px;"/><br /><sub>Oscar</sub>](http://obartra.github.io)<br />[💻](https://github.com/kentcdodds/babel-plugin-preval/commits?author=obartra "Code") [⚠️](https://github.com/kentcdodds/babel-plugin-preval/commits?author=obartra "Tests") |
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
| [<img src="https://avatars1.githubusercontent.com/u/14310216?v=4" width="100px;"/><br /><sub>pro-nasa</sub>](https://github.com/pro-nasa)<br />[📖](https://github.com/kentcdodds/babel-plugin-preval/commits?author=pro-nasa "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/9248479?v=4" width="100px;"/><br /><sub>Sergey Bekrin</sub>](http://bekrin.me)<br /> | [<img src="https://avatars0.githubusercontent.com/u/18613301?v=4" width="100px;"/><br /><sub>Mauro Bringolf</sub>](https://maurobringolf.ch)<br /> |
| [<img src="https://avatars1.githubusercontent.com/u/14310216?v=4" width="100px;"/><br /><sub>pro-nasa</sub>](https://github.com/pro-nasa)<br />[📖](https://github.com/kentcdodds/babel-plugin-preval/commits?author=pro-nasa "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/9248479?v=4" width="100px;"/><br /><sub>Sergey Bekrin</sub>](http://bekrin.me)<br /> | [<img src="https://avatars0.githubusercontent.com/u/18613301?v=4" width="100px;"/><br /><sub>Mauro Bringolf</sub>](https://maurobringolf.ch)<br />[💻](https://github.com/kentcdodds/babel-plugin-preval/commits?author=maurobringolf "Code") [⚠️](https://github.com/kentcdodds/babel-plugin-preval/commits?author=maurobringolf "Tests") | [<img src="https://avatars1.githubusercontent.com/u/10875678?v=4" width="100px;"/><br /><sub>Joe Lim</sub>](https://joelim.me)<br /> |
<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors][all-contributors] specification.
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"author": "Kent C. Dodds <[email protected]> (http://kentcdodds.com/)",
"license": "MIT",
"dependencies": {
"babel-core": "^6.26.0",
"babel-macros": "^1.1.1",
"babel-register": "^6.26.0",
"babylon": "^6.18.0",
Expand Down
3 changes: 1 addition & 2 deletions src/get-replacement.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const babel = require('babel-core')
const requireFromString = require('require-from-string')
const objectToAST = require('./object-to-ast')

module.exports = getReplacement

function getReplacement({string: stringToPreval, filename}) {
function getReplacement({string: stringToPreval, filename, babel}) {
const {code: transpiled} = babel.transform(stringToPreval, {
filename,
})
Expand Down
8 changes: 5 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const objectToAST = require('./object-to-ast')

module.exports = prevalPlugin

function prevalPlugin({types: t, template, transformFromAst}) {
function prevalPlugin(babel) {
const {types: t, template, transformFromAst} = babel
const assignmentBuilder = template('const NAME = VALUE')
return {
name: 'preval',
Expand All @@ -22,7 +23,7 @@ function prevalPlugin({types: t, template, transformFromAst}) {
comments.find(isPrevalComment).value = ' this file was prevaled'

const {code: string} = transformFromAst(path.node)
const replacement = getReplacement({string, filename})
const replacement = getReplacement({string, filename, babel})

const moduleExports = Object.assign(
{},
Expand Down Expand Up @@ -50,7 +51,7 @@ function prevalPlugin({types: t, template, transformFromAst}) {
if (!string) {
throw new Error('Unable to determine the value of your preval string')
}
const replacement = getReplacement({string, filename})
const replacement = getReplacement({string, filename, babel})
path.replaceWith(replacement)
},
ImportDeclaration(path, {file: {opts: {filename}}}) {
Expand Down Expand Up @@ -88,6 +89,7 @@ function prevalPlugin({types: t, template, transformFromAst}) {
module.exports = mod
`,
filename,
babel,
})
path.replaceWith(
assignmentBuilder({
Expand Down
16 changes: 10 additions & 6 deletions src/macro.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@ const getReplacement = require('./get-replacement')

module.exports = createMacro(prevalMacros)

function prevalMacros({references, state}) {
function prevalMacros({references, state, babel}) {
references.default.forEach(referencePath => {
if (referencePath.parentPath.type === 'TaggedTemplateExpression') {
asTag(referencePath.parentPath.get('quasi'), state)
asTag(referencePath.parentPath.get('quasi'), state, babel)
} else if (referencePath.parentPath.type === 'CallExpression') {
asFunction(referencePath.parentPath.get('arguments'), state)
asFunction(referencePath.parentPath.get('arguments'), state, babel)
} else if (referencePath.parentPath.type === 'JSXOpeningElement') {
asJSX(
{
attributes: referencePath.parentPath.get('attributes'),
children: referencePath.parentPath.parentPath.get('children'),
},
state,
babel,
)
} else if (referencePath.parentPath.type === 'JSXClosingElement') {
// That's okay, we already prevaled this one on its opening element.
Expand All @@ -29,34 +30,37 @@ function prevalMacros({references, state}) {
})
}

function asTag(quasiPath, {file: {opts: {filename}}}) {
function asTag(quasiPath, {file: {opts: {filename}}}, babel) {
const string = quasiPath.parentPath.get('quasi').evaluate().value
quasiPath.parentPath.replaceWith(
getReplacement({
string,
filename,
babel,
}),
)
}

function asFunction(argumentsPaths, {file: {opts: {filename}}}) {
function asFunction(argumentsPaths, {file: {opts: {filename}}}, babel) {
const string = argumentsPaths[0].evaluate().value
argumentsPaths[0].parentPath.replaceWith(
getReplacement({
string,
filename,
babel,
}),
)
}

// eslint-disable-next-line no-unused-vars
function asJSX({attributes, children}, {file: {opts: {filename}}}) {
function asJSX({attributes, children}, {file: {opts: {filename}}}, babel) {
// It's a shame you cannot use evaluate() with JSX
const string = children[0].node.value
children[0].replaceWith(
getReplacement({
string,
filename,
babel,
}),
)
const {parentPath: {node: {openingElement, closingElement}}} = children[0]
Expand Down

0 comments on commit 99ea71a

Please sign in to comment.