babel-plugin-codegen
Advanced tools
Comparing version 2.0.0 to 3.0.0
@@ -7,6 +7,6 @@ 'use strict'; | ||
var fs = require('fs'); | ||
var requireFromString = require('require-from-string'); | ||
var requireFromCodeString = require('require-from-string'); | ||
module.exports = { | ||
transformAndRequire, | ||
requireFromString, | ||
getReplacement, | ||
@@ -20,47 +20,18 @@ replace, | ||
function transformAndRequire(_ref, babel) { | ||
var code = _ref.code, | ||
fileOpts = _ref.fileOpts, | ||
_ref$fileOpts = _ref.fileOpts, | ||
filename = _ref$fileOpts.filename, | ||
plugins = _ref$fileOpts.plugins, | ||
presets = _ref$fileOpts.presets; | ||
/** | ||
* Transform passed in code to generated code | ||
*/ | ||
var _babel$transform = babel.transform(code, { | ||
filename, | ||
plugins, | ||
presets | ||
}), | ||
transformedCode = _babel$transform.code; | ||
/** | ||
* Execute the transformed code, as if it were required | ||
*/ | ||
var module = requireFromString(transformedCode, fileOpts.filename); | ||
/** | ||
* Allow for es modules (default export) | ||
*/ | ||
function requireFromString(code, filename) { | ||
// Execute the transformed code, as if it were required | ||
var module = requireFromCodeString(String(code), filename); | ||
// Allow for es modules (default export) | ||
return module && module.__esModule ? module.default : module; | ||
} | ||
function getReplacement(_ref2, babel) { | ||
var code = _ref2.code, | ||
fileOpts = _ref2.fileOpts, | ||
_ref2$args = _ref2.args, | ||
args = _ref2$args === undefined ? [] : _ref2$args; | ||
function getReplacement(_ref, babel) { | ||
var code = _ref.code, | ||
fileOpts = _ref.fileOpts, | ||
_ref$args = _ref.args, | ||
args = _ref$args === undefined ? [] : _ref$args; | ||
/** | ||
* Execute the code string to get the exported module | ||
*/ | ||
var module = transformAndRequire({ code, fileOpts }, babel); | ||
var module = requireFromString(code, fileOpts.filename); | ||
/** | ||
* If a function is epxorted, call it with args | ||
*/ | ||
// If a function is epxorted, call it with args | ||
if (typeof module === 'function') { | ||
@@ -72,5 +43,3 @@ module = module.apply(undefined, args); | ||
/** | ||
* Convert whatever we got now (hopefully a string) into AST form | ||
*/ | ||
// Convert whatever we got now (hopefully a string) into AST form | ||
if (typeof module !== 'string') { | ||
@@ -97,7 +66,7 @@ throw new Error('codegen: Must module.exports a string.'); | ||
function replace(_ref3, babel) { | ||
var path = _ref3.path, | ||
code = _ref3.code, | ||
fileOpts = _ref3.fileOpts, | ||
args = _ref3.args; | ||
function replace(_ref2, babel) { | ||
var path = _ref2.path, | ||
code = _ref2.code, | ||
fileOpts = _ref2.fileOpts, | ||
args = _ref2.args; | ||
@@ -108,5 +77,5 @@ var replacement = getReplacement({ code, args, fileOpts }, babel); | ||
function resolveModuleContents(_ref4) { | ||
var filename = _ref4.filename, | ||
module = _ref4.module; | ||
function resolveModuleContents(_ref3) { | ||
var filename = _ref3.filename, | ||
module = _ref3.module; | ||
@@ -113,0 +82,0 @@ var resolvedPath = p.resolve(p.dirname(filename), module); |
@@ -11,3 +11,3 @@ 'use strict'; | ||
isPropertyCall = _require.isPropertyCall, | ||
transformAndRequire = _require.transformAndRequire; | ||
requireFromString = _require.requireFromString; | ||
@@ -106,6 +106,3 @@ module.exports = getReplacers; | ||
if (codegenComment !== 'codegen') { | ||
args = transformAndRequire({ | ||
code: `module.exports = [${codegenComment.replace(/codegen\((.*)\)/, '$1').trim()}]`, | ||
fileOpts | ||
}, babel); | ||
args = requireFromString(`module.exports = [${codegenComment.replace(/codegen\((.*)\)/, '$1').trim()}]`, fileOpts.filename); | ||
} | ||
@@ -112,0 +109,0 @@ replace({ |
{ | ||
"name": "babel-plugin-codegen", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"description": "Generate code at build-time", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -9,15 +9,15 @@ <div align="center"> | ||
<!-- prettier-ignore-start --> | ||
[![Build Status][build-badge]][build] | ||
[![Code Coverage][coverage-badge]][coverage] | ||
[![version][version-badge]][package] [![downloads][downloads-badge]][npmcharts] | ||
[![version][version-badge]][package] | ||
[![downloads][downloads-badge]][npmtrends] | ||
[![MIT License][license-badge]][license] | ||
[![All Contributors](https://img.shields.io/badge/all_contributors-3-orange.svg?style=flat-square)](#contributors) | ||
[![PRs Welcome][prs-badge]][prs] [![Code of Conduct][coc-badge]][coc] | ||
[![All Contributors](https://img.shields.io/badge/all_contributors-4-orange.svg?style=flat-square)](#contributors) | ||
[![PRs Welcome][prs-badge]][prs] | ||
[![Code of Conduct][coc-badge]][coc] | ||
[![Babel Macro](https://img.shields.io/badge/babel--macro-%F0%9F%8E%A3-f5da55.svg?style=flat-square)](https://github.com/kentcdodds/babel-plugin-macros) | ||
<!-- prettier-ignore-end --> | ||
[![Watch on GitHub][github-watch-badge]][github-watch] | ||
[![Star on GitHub][github-star-badge]][github-star] | ||
[![Tweet][twitter-badge]][twitter] | ||
## The problem | ||
@@ -50,3 +50,2 @@ | ||
<!-- START doctoc generated TOC please keep comment here to allow auto update --> | ||
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> | ||
@@ -67,2 +66,3 @@ | ||
- [Caveats](#caveats) | ||
- [Examples](#examples) | ||
- [Inspiration](#inspiration) | ||
@@ -95,9 +95,7 @@ - [Other Solutions](#other-solutions) | ||
2. All code _must_ run synchronously. | ||
3. All code will be transpiled via the same instance of babel this plugin is | ||
called with, thus inheriting all presets and plugins. This means you can | ||
rely on any babel plugins/transforms that you're used to using elsewhere in | ||
your codebase. | ||
4. The code that's generated might be transpiled. Please check the output to | ||
make sure. (babel plugin ordering is tricky business 😇) | ||
> You may like to watch | ||
> [this YouTube video](https://www.youtube.com/watch?v=1queadQ0048&list=PLV5CVI1eNcJgCrPH_e6d57KRUTiDZgs0u) | ||
> to get an idea of what codegen is and how it can be used. | ||
### Template Tag | ||
@@ -269,2 +267,8 @@ | ||
## Examples | ||
- [Using babel macros with React Native](https://bit.ly/babel-codegen): A | ||
practical use case for solving an i18n problem using | ||
[`codegen.macro`](https://www.npmjs.com/package/codegen.macro) | ||
## Inspiration | ||
@@ -285,6 +289,5 @@ | ||
<!-- 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-codegen/commits?author=kentcdodds "Code") [📖](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=kentcdodds "Tests") | [<img src="https://avatars1.githubusercontent.com/u/1958812?v=4" width="100px;"/><br /><sub><b>Michael Rawlings</b></sub>](https://github.com/mlrawlings)<br />[💻](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=mlrawlings "Code") [📖](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=mlrawlings "Documentation") [⚠️](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=mlrawlings "Tests") | [<img src="https://avatars3.githubusercontent.com/u/5230863?v=4" width="100px;"/><br /><sub><b>Jan Willem Henckel</b></sub>](https://jan.cologne)<br />[💻](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=djfarly "Code") [📖](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=djfarly "Documentation") [⚠️](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=djfarly "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 />[💻](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=kentcdodds "Code") [📖](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=kentcdodds "Tests") | [<img src="https://avatars1.githubusercontent.com/u/1958812?v=4" width="100px;"/><br /><sub><b>Michael Rawlings</b></sub>](https://github.com/mlrawlings)<br />[💻](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=mlrawlings "Code") [📖](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=mlrawlings "Documentation") [⚠️](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=mlrawlings "Tests") | [<img src="https://avatars3.githubusercontent.com/u/5230863?v=4" width="100px;"/><br /><sub><b>Jan Willem Henckel</b></sub>](https://jan.cologne)<br />[💻](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=djfarly "Code") [📖](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=djfarly "Documentation") [⚠️](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=djfarly "Tests") | [<img src="https://avatars3.githubusercontent.com/u/1824298?v=4" width="100px;"/><br /><sub><b>Karan Thakkar</b></sub>](https://twitter.com/geekykaran)<br />[📖](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=karanjthakkar "Documentation") | | ||
| :---: | :---: | :---: | :---: | | ||
@@ -300,2 +303,4 @@ <!-- ALL-CONTRIBUTORS-LIST:END --> | ||
<!-- prettier-ignore-start --> | ||
[npm]: https://www.npmjs.com/ | ||
@@ -310,3 +315,3 @@ [node]: https://nodejs.org | ||
[downloads-badge]: https://img.shields.io/npm/dm/babel-plugin-codegen.svg?style=flat-square | ||
[npmcharts]: http://npmcharts.com/compare/babel-plugin-codegen | ||
[npmtrends]: http://www.npmtrends.com/babel-plugin-codegen | ||
[license-badge]: https://img.shields.io/npm/l/babel-plugin-codegen.svg?style=flat-square | ||
@@ -319,8 +324,2 @@ [license]: https://github.com/kentcdodds/babel-plugin-codegen/blob/master/LICENSE | ||
[coc]: https://github.com/kentcdodds/babel-plugin-codegen/blob/master/other/CODE_OF_CONDUCT.md | ||
[github-watch-badge]: https://img.shields.io/github/watchers/kentcdodds/babel-plugin-codegen.svg?style=social | ||
[github-watch]: https://github.com/kentcdodds/babel-plugin-codegen/watchers | ||
[github-star-badge]: https://img.shields.io/github/stars/kentcdodds/babel-plugin-codegen.svg?style=social | ||
[github-star]: https://github.com/kentcdodds/babel-plugin-codegen/stargazers | ||
[twitter]: https://twitter.com/intent/tweet?text=Check%20out%20babel-plugin-codegen!%20https://github.com/kentcdodds/babel-plugin-codegen%20%F0%9F%91%8D | ||
[twitter-badge]: https://img.shields.io/twitter/url/https/github.com/kentcdodds/babel-plugin-codegen.svg?style=social | ||
[emojis]: https://github.com/kentcdodds/all-contributors#emoji-key | ||
@@ -332,1 +331,3 @@ [all-contributors]: https://github.com/kentcdodds/all-contributors | ||
[babel-plugin-macros]: https://github.com/kentcdodds/babel-plugin-macros | ||
<!-- prettier-ignore-end --> |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
324
0
24615
319