Skip to content

Commit b6eeba0

Browse files
marzelinKent C. Dodds
authored andcommitted
fix: transpile imported modules (#51)
* add contributor * (fix) perform transpilation for modules required in preval's template tags
1 parent 36de84a commit b6eeba0

5 files changed

Lines changed: 34 additions & 12 deletions

File tree

β€Ž.all-contributorsrcβ€Ž

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,16 @@
112112
"contributions": [
113113
"code"
114114
]
115+
},
116+
{
117+
"login": "marzelin",
118+
"name": "Marcin Zielinski",
119+
"avatar_url": "https://avatars3.githubusercontent.com/u/13483453?v=4",
120+
"profile": "https://github.com/marzelin",
121+
"contributions": [
122+
"code"
123+
]
115124
}
116-
]
125+
],
126+
"repoType": "github"
117127
}

β€ŽREADME.mdβ€Ž

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Pre-evaluate code at build-time
1313
[![downloads][downloads-badge]][npm-stat]
1414
[![MIT License][license-badge]][license]
1515

16-
[![All Contributors](https://img.shields.io/badge/all_contributors-11-orange.svg?style=flat-square)](#contributors)
16+
[![All Contributors](https://img.shields.io/badge/all_contributors-12-orange.svg?style=flat-square)](#contributors)
1717
[![PRs Welcome][prs-badge]][prs]
1818
[![Donate][donate-badge]][donate]
1919
[![Code of Conduct][coc-badge]][coc]
@@ -116,13 +116,13 @@ npm install --save-dev babel-plugin-preval
116116

117117
Important notes:
118118

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

127127
### Template Tag
128128

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

371371
<!-- prettier-ignore -->
372-
| [<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") |
372+
| [<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") |
373373
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
374-
| [<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") |
374+
| [<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") |
375375

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

β€Žsrc/__tests__/__snapshots__/index.js.snapβ€Ž

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,3 +392,13 @@ module.exports = smth.UNDEFINED;
392392
module.exports = undefined;
393393
394394
`;
395+
396+
exports[`38. preval 1`] = `
397+
398+
const x = preval\`module.exports = require('./fixtures/es6.js')\`
399+
400+
↓ ↓ ↓ ↓ ↓ ↓
401+
402+
const x = { "default": "this string used exported default" };
403+
404+
`;

β€Žsrc/__tests__/index.jsβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,5 +135,6 @@ pluginTester({
135135
// @preval
136136
/* comment */
137137
`),
138+
`const x = preval\`module.exports = require('./fixtures/es6.js')\``,
138139
],
139140
})

β€Žsrc/get-replacement.jsβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ const objectToAST = require('./object-to-ast')
44
module.exports = getReplacement
55

66
function getReplacement({string: stringToPreval, filename, babel}) {
7-
const {code: transpiled} = babel.transform(stringToPreval, {
7+
const {code} = babel.transform(stringToPreval, {
88
filename,
99
})
10+
const transpiled = `require('babel-register');\n${code}`
1011
const val = requireFromString(transpiled, filename)
1112
return objectToAST(val)
1213
}

0 commit comments

Comments
Β (0)