Skip to content

Commit

Permalink
fix: transpile imported modules (#51)
Browse files Browse the repository at this point in the history
* add contributor

* (fix) perform transpilation for modules required in preval's template tags
  • Loading branch information
marzelin authored and Kent C. Dodds committed Mar 4, 2018
1 parent 36de84a commit b6eeba0
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 12 deletions.
12 changes: 11 additions & 1 deletion .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@
"contributions": [
"code"
]
},
{
"login": "marzelin",
"name": "Marcin Zielinski",
"avatar_url": "https://avatars3.githubusercontent.com/u/13483453?v=4",
"profile": "https://github.com/marzelin",
"contributions": [
"code"
]
}
]
],
"repoType": "github"
}
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,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-11-orange.svg?style=flat-square)](#contributors)
[![All Contributors](https://img.shields.io/badge/all_contributors-12-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 @@ -116,13 +116,13 @@ npm install --save-dev babel-plugin-preval

Important notes:

1. All code run by `preval` is _not_ run in a sandboxed environment
2. All code _must_ run synchronously.
3. All code will be transpiled via `babel-core` directly or `babel-register`
and should follow all of the normal rules for `.babelrc` resolution (the
closest `.babelrc` to the file being run is the one that's used). This means
you can rely on any babel plugins/transforms that you're used to using
elsewhere in your codebase.
1. All code run by `preval` is _not_ run in a sandboxed environment
2. All code _must_ run synchronously.
3. All code will be transpiled via `babel-core` directly or `babel-register`
and should follow all of the normal rules for `.babelrc` resolution (the
closest `.babelrc` to the file being run is the one that's used). This means
you can rely on any babel plugins/transforms that you're used to using
elsewhere in your codebase.

### Template Tag

Expand Down Expand Up @@ -369,9 +369,9 @@ Thanks goes to these people ([emoji key][emojis]):
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->

<!-- prettier-ignore -->
| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub><b>Kent C. Dodds</b></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><b>Matt Phillips</b></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><b>Philip Oliver</b></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><b>Sorin Davidoi</b></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><b>Luke Herrington</b></sub>](https://github.com/infiniteluke)<br />[💡](#example-infiniteluke "Examples") | [<img src="https://avatars4.githubusercontent.com/u/22868432?v=4" width="100px;"/><br /><sub><b>Lufty Wiranda</b></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><b>Oscar</b></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://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub><b>Kent C. Dodds</b></sub>](https://kentcdodds.com)<br />[💻](/kentcdodds/babel-plugin-preval/commits?author=kentcdodds "Code") [📖](/kentcdodds/babel-plugin-preval/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](/kentcdodds/babel-plugin-preval/commits?author=kentcdodds "Tests") | [<img src="https://avatars3.githubusercontent.com/u/5610087?v=3" width="100px;"/><br /><sub><b>Matt Phillips</b></sub>](http://mattphillips.io)<br />[💻](/kentcdodds/babel-plugin-preval/commits?author=mattphillips "Code") [📖](/kentcdodds/babel-plugin-preval/commits?author=mattphillips "Documentation") [⚠️](/kentcdodds/babel-plugin-preval/commits?author=mattphillips "Tests") | [<img src="https://avatars1.githubusercontent.com/u/28024000?v=3" width="100px;"/><br /><sub><b>Philip Oliver</b></sub>](https://twitter.com/philipodev)<br />[🐛](/kentcdodds/babel-plugin-preval/issues?q=author%3Aphilipodev "Bug reports") | [<img src="https://avatars2.githubusercontent.com/u/2109702?v=3" width="100px;"/><br /><sub><b>Sorin Davidoi</b></sub>](https://toot.cafe/@sorin)<br />[🐛](/kentcdodds/babel-plugin-preval/issues?q=author%3Asorin-davidoi "Bug reports") [💻](/kentcdodds/babel-plugin-preval/commits?author=sorin-davidoi "Code") [⚠️](/kentcdodds/babel-plugin-preval/commits?author=sorin-davidoi "Tests") | [<img src="https://avatars4.githubusercontent.com/u/1127238?v=4" width="100px;"/><br /><sub><b>Luke Herrington</b></sub>](https://github.com/infiniteluke)<br />[💡](#example-infiniteluke "Examples") | [<img src="https://avatars4.githubusercontent.com/u/22868432?v=4" width="100px;"/><br /><sub><b>Lufty Wiranda</b></sub>](http://instagram.com/luftywiranda13)<br />[💻](/kentcdodds/babel-plugin-preval/commits?author=luftywiranda13 "Code") | [<img src="https://avatars0.githubusercontent.com/u/3877773?v=4" width="100px;"/><br /><sub><b>Oscar</b></sub>](http://obartra.github.io)<br />[💻](/kentcdodds/babel-plugin-preval/commits?author=obartra "Code") [⚠️](/kentcdodds/babel-plugin-preval/commits?author=obartra "Tests") |
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
| [<img src="https://avatars1.githubusercontent.com/u/14310216?v=4" width="100px;"/><br /><sub><b>pro-nasa</b></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><b>Sergey Bekrin</b></sub>](http://bekrin.me)<br /> | [<img src="https://avatars0.githubusercontent.com/u/18613301?v=4" width="100px;"/><br /><sub><b>Mauro Bringolf</b></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><b>Joe Lim</b></sub>](https://joelim.me)<br />[💻](https://github.com/kentcdodds/babel-plugin-preval/commits?author=xjlim "Code") |
| [<img src="https://avatars1.githubusercontent.com/u/14310216?v=4" width="100px;"/><br /><sub><b>pro-nasa</b></sub>](https://github.com/pro-nasa)<br />[📖](/kentcdodds/babel-plugin-preval/commits?author=pro-nasa "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/9248479?v=4" width="100px;"/><br /><sub><b>Sergey Bekrin</b></sub>](http://bekrin.me)<br /> | [<img src="https://avatars0.githubusercontent.com/u/18613301?v=4" width="100px;"/><br /><sub><b>Mauro Bringolf</b></sub>](https://maurobringolf.ch)<br />[💻](/kentcdodds/babel-plugin-preval/commits?author=maurobringolf "Code") [⚠️](/kentcdodds/babel-plugin-preval/commits?author=maurobringolf "Tests") | [<img src="https://avatars1.githubusercontent.com/u/10875678?v=4" width="100px;"/><br /><sub><b>Joe Lim</b></sub>](https://joelim.me)<br />[💻](/kentcdodds/babel-plugin-preval/commits?author=xjlim "Code") | [<img src="https://avatars3.githubusercontent.com/u/13483453?v=4" width="100px;"/><br /><sub><b>Marcin Zielinski</b></sub>](https://github.com/marzelin)<br />[💻](/kentcdodds/babel-plugin-preval/commits?author=marzelin "Code") |

<!-- ALL-CONTRIBUTORS-LIST:END -->

Expand Down
10 changes: 10 additions & 0 deletions src/__tests__/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -392,3 +392,13 @@ module.exports = smth.UNDEFINED;
module.exports = undefined;
`;
exports[`38. preval 1`] = `
const x = preval\`module.exports = require('./fixtures/es6.js')\`
↓ ↓ ↓ ↓ ↓ ↓
const x = { "default": "this string used exported default" };
`;
1 change: 1 addition & 0 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,6 @@ pluginTester({
// @preval
/* comment */
`),
`const x = preval\`module.exports = require('./fixtures/es6.js')\``,
],
})
3 changes: 2 additions & 1 deletion src/get-replacement.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ const objectToAST = require('./object-to-ast')
module.exports = getReplacement

function getReplacement({string: stringToPreval, filename, babel}) {
const {code: transpiled} = babel.transform(stringToPreval, {
const {code} = babel.transform(stringToPreval, {
filename,
})
const transpiled = `require('babel-register');\n${code}`
const val = requireFromString(transpiled, filename)
return objectToAST(val)
}

0 comments on commit b6eeba0

Please sign in to comment.