Initial commit
This commit is contained in:
4
themes/keepit/node_modules/babel-plugin-preval/CHANGELOG.md
generated
vendored
Normal file
4
themes/keepit/node_modules/babel-plugin-preval/CHANGELOG.md
generated
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
# CHANGELOG
|
||||
|
||||
The changelog is automatically updated using [semantic-release](https://github.com/semantic-release/semantic-release).
|
||||
You can see it on the [releases page](../../releases).
|
||||
20
themes/keepit/node_modules/babel-plugin-preval/LICENSE
generated
vendored
Normal file
20
themes/keepit/node_modules/babel-plugin-preval/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
The MIT License (MIT)
|
||||
Copyright (c) 2017 Kent C. Dodds
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
464
themes/keepit/node_modules/babel-plugin-preval/README.md
generated
vendored
Normal file
464
themes/keepit/node_modules/babel-plugin-preval/README.md
generated
vendored
Normal file
@@ -0,0 +1,464 @@
|
||||
<p align="center">
|
||||
<a href="https://codefund.io/properties/492/visit-sponsor">
|
||||
<img src="https://codefund.io/properties/492/sponsor" />
|
||||
</a>
|
||||
</p>
|
||||
|
||||
<div align="center">
|
||||
<h1>babel-plugin-preval</h1>
|
||||
|
||||
Pre-evaluate code at build-time
|
||||
|
||||
</div>
|
||||
|
||||
<hr />
|
||||
|
||||
<!-- prettier-ignore-start -->
|
||||
|
||||
[![Build Status][build-badge]][build]
|
||||
[![Code Coverage][coverage-badge]][coverage]
|
||||
[![version][version-badge]][package]
|
||||
[![downloads][downloads-badge]][npmtrends]
|
||||
[![MIT License][license-badge]][license]
|
||||
|
||||
[](#contributors)
|
||||
[![PRs Welcome][prs-badge]][prs]
|
||||
[![Code of Conduct][coc-badge]][coc]
|
||||
[![Babel Macro][macros-badge]][babel-plugin-macros]
|
||||
[![Examples][examples-badge]][examples]
|
||||
|
||||
[![Watch on GitHub][github-watch-badge]][github-watch]
|
||||
[![Star on GitHub][github-star-badge]][github-star]
|
||||
[![Tweet][twitter-badge]][twitter]
|
||||
|
||||
<!-- prettier-ignore-end -->
|
||||
|
||||
## The problem
|
||||
|
||||
You need to do some dynamic stuff, but don't want to do it at runtime. Or maybe
|
||||
you want to do stuff like read the filesystem to get a list of files and you
|
||||
can't do that in the browser.
|
||||
|
||||
## This solution
|
||||
|
||||
This allows you to specify some code that runs in Node and whatever you
|
||||
`module.exports` in there will be swapped. For example:
|
||||
|
||||
```js
|
||||
const x = preval`module.exports = 1`
|
||||
|
||||
// ↓ ↓ ↓ ↓ ↓ ↓
|
||||
|
||||
const x = 1
|
||||
```
|
||||
|
||||
Or, more interestingly:
|
||||
|
||||
```javascript
|
||||
const x = preval`
|
||||
const fs = require('fs')
|
||||
const val = fs.readFileSync(__dirname + '/fixture1.md', 'utf8')
|
||||
module.exports = {
|
||||
val,
|
||||
getSplit: function(splitDelimiter) {
|
||||
return x.val.split(splitDelimiter)
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
// ↓ ↓ ↓ ↓ ↓ ↓
|
||||
|
||||
const x = {
|
||||
val: '# fixture\n\nThis is some file thing...\n',
|
||||
getSplit: function getSplit(splitDelimiter) {
|
||||
return x.val.split(splitDelimiter)
|
||||
},
|
||||
}
|
||||
```
|
||||
|
||||
There's also `preval.require('./something')` and
|
||||
`import x from /* preval */ './something'` (which can both take some arguments)
|
||||
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)
|
||||
- [Template Tag](#template-tag)
|
||||
- [import comment](#import-comment)
|
||||
- [preval.require](#prevalrequire)
|
||||
- [preval file comment (`// @preval`)](#preval-file-comment--preval)
|
||||
- [Configure with Babel](#configure-with-babel)
|
||||
- [Via `.babelrc` (Recommended)](#via-babelrc-recommended)
|
||||
- [Via CLI](#via-cli)
|
||||
- [Via Node API](#via-node-api)
|
||||
- [Use with `babel-plugin-macros`](#use-with-babel-plugin-macros)
|
||||
- [Examples](#examples)
|
||||
- [Notes](#notes)
|
||||
- [FAQ](#faq)
|
||||
- [How is this different from prepack?](#how-is-this-different-from-prepack)
|
||||
- [How is this different from webpack loaders?](#how-is-this-different-from-webpack-loaders)
|
||||
- [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
|
||||
should be installed as one of your project's `devDependencies`:
|
||||
|
||||
```
|
||||
npm install --save-dev babel-plugin-preval
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Important notes:
|
||||
|
||||
1. All code run by `preval` is _not_ run in a sandboxed environment
|
||||
2. All code _must_ run synchronously.
|
||||
3. Code that is run by preval is not transpiled so it must run natively in the
|
||||
version of node you're running. (cannot use es modules).
|
||||
|
||||
> You may like to watch
|
||||
> [this YouTube video](https://www.youtube.com/watch?v=1queadQ0048&list=PLV5CVI1eNcJgCrPH_e6d57KRUTiDZgs0u)
|
||||
> to get an idea of what preval is and how it can be used.
|
||||
|
||||
### Template Tag
|
||||
|
||||
**Before**:
|
||||
|
||||
```javascript
|
||||
const greeting = preval`
|
||||
const fs = require('fs')
|
||||
module.exports = fs.readFileSync(require.resolve('./greeting.txt'), 'utf8')
|
||||
`
|
||||
```
|
||||
|
||||
**After** (assuming `greeting.txt` contains the text: `"Hello world!"`):
|
||||
|
||||
```javascript
|
||||
const greeting = 'Hello world!'
|
||||
```
|
||||
|
||||
`preval` can also handle _some_ simple dynamic values as well:
|
||||
|
||||
**Before**:
|
||||
|
||||
```javascript
|
||||
const name = 'Bob Hope'
|
||||
const person = preval`
|
||||
const [first, last] = require('./name-splitter')(${name})
|
||||
module.exports = {first, last}
|
||||
`
|
||||
```
|
||||
|
||||
**After** (assuming `./name-splitter` is a function that splits a name into
|
||||
first/last):
|
||||
|
||||
```javascript
|
||||
const name = 'Bob Hope'
|
||||
const person = {first: 'Bob', last: 'Hope'}
|
||||
```
|
||||
|
||||
### import comment
|
||||
|
||||
**Before**:
|
||||
|
||||
```javascript
|
||||
import fileList from /* preval */ './get-list-of-files'
|
||||
```
|
||||
|
||||
**After** (depending on what `./get-list-of-files does`, it might be something
|
||||
like):
|
||||
|
||||
```javascript
|
||||
const fileList = ['file1.md', 'file2.md', 'file3.md', 'file4.md']
|
||||
```
|
||||
|
||||
You can also provide arguments which themselves are prevaled!
|
||||
|
||||
**Before**:
|
||||
|
||||
```javascript
|
||||
import fileList from /* preval(3) */ './get-list-of-files'
|
||||
```
|
||||
|
||||
**After** (assuming `./get-list-of-files` accepts an argument limiting how many
|
||||
files are retrieved:
|
||||
|
||||
```javascript
|
||||
const fileList = ['file1.md', 'file2.md', 'file3.md']
|
||||
```
|
||||
|
||||
### preval.require
|
||||
|
||||
**Before**:
|
||||
|
||||
```javascript
|
||||
const fileLastModifiedDate = preval.require('./get-last-modified-date')
|
||||
```
|
||||
|
||||
**After**:
|
||||
|
||||
```javascript
|
||||
const fileLastModifiedDate = '2017-07-05'
|
||||
```
|
||||
|
||||
And you can provide _some_ simple dynamic arguments as well:
|
||||
|
||||
**Before**:
|
||||
|
||||
```javascript
|
||||
const fileLastModifiedDate = preval.require(
|
||||
'./get-last-modified-date',
|
||||
'../../some-other-file.js',
|
||||
)
|
||||
```
|
||||
|
||||
**After**:
|
||||
|
||||
```javascript
|
||||
const fileLastModifiedDate = '2017-07-04'
|
||||
```
|
||||
|
||||
### preval file comment (`// @preval`)
|
||||
|
||||
Using the preval file comment will update a whole file to be evaluated down to
|
||||
an export.
|
||||
|
||||
Whereas the above usages (assignment/import/require) will only preval the scope
|
||||
of the assignment or file being imported.
|
||||
|
||||
**Before**:
|
||||
|
||||
```javascript
|
||||
// @preval
|
||||
|
||||
const id = require('./path/identity')
|
||||
const one = require('./path/one')
|
||||
|
||||
const compose = (...fns) => fns.reduce((f, g) => a => f(g(a)))
|
||||
const double = a => a * 2
|
||||
const square = a => a * a
|
||||
|
||||
module.exports = compose(
|
||||
square,
|
||||
id,
|
||||
double,
|
||||
)(one)
|
||||
```
|
||||
|
||||
**After**:
|
||||
|
||||
```javascript
|
||||
module.exports = 4
|
||||
```
|
||||
|
||||
## Configure with Babel
|
||||
|
||||
### Via `.babelrc` (Recommended)
|
||||
|
||||
**.babelrc**
|
||||
|
||||
```json
|
||||
{
|
||||
"plugins": ["preval"]
|
||||
}
|
||||
```
|
||||
|
||||
### Via CLI
|
||||
|
||||
```sh
|
||||
babel --plugins preval script.js
|
||||
```
|
||||
|
||||
### Via Node API
|
||||
|
||||
```javascript
|
||||
require('babel-core').transform('code', {
|
||||
plugins: ['preval'],
|
||||
})
|
||||
```
|
||||
|
||||
## Use with `babel-plugin-macros`
|
||||
|
||||
Once you've
|
||||
[configured `babel-plugin-macros`](https://github.com/kentcdodds/babel-plugin-macros/blob/master/other/docs/user.md)
|
||||
you can import/require the preval macro at `babel-plugin-preval/macro`. For
|
||||
example:
|
||||
|
||||
```javascript
|
||||
import preval from 'babel-plugin-preval/macro'
|
||||
|
||||
const one = preval`module.exports = 1 + 2 - 1 - 1`
|
||||
```
|
||||
|
||||
> You could also use [`preval.macro`][preval.macro] if you'd prefer to type less
|
||||
> 😀
|
||||
|
||||
## Examples
|
||||
|
||||
- [Mastodon](https://github.com/tootsuite/mastodon/pull/4202) saved 40kb
|
||||
(gzipped) using `babel-plugin-preval`
|
||||
- [glamorous-website](https://github.com/kentcdodds/glamorous-website/pull/235)
|
||||
uses [`preval.macro`][preval.macro] to determine Algolia options based on
|
||||
`process.env.LOCALE`. It also uses [`preval.macro`][preval.macro] to load an
|
||||
`svg` file as a string, `base64` encode it, and use it as a `background-url`
|
||||
for an input element.
|
||||
- [Generate documentation for React components](https://gist.github.com/souporserious/575609dc5a5d52e167dd2236079eccc0)
|
||||
- [Serverless with webpack](https://github.com/geovanisouza92/serverless-preval)
|
||||
build serverless functions using webpack and Babel for development and
|
||||
production with preval to replace (possible sensible) content in code.
|
||||
- [Read files at build time (video)](https://www.youtube.com/watch?v=NhmrbpVKgdQ&feature=youtu.be)
|
||||
|
||||
## Notes
|
||||
|
||||
If you use `babel-plugin-transform-decorators-legacy`, there is a conflict
|
||||
because both plugins must be placed at the top
|
||||
|
||||
Wrong:
|
||||
|
||||
```json
|
||||
{
|
||||
"plugins": ["preval", "transform-decorators-legacy"]
|
||||
}
|
||||
```
|
||||
|
||||
Ok:
|
||||
|
||||
```json
|
||||
{
|
||||
"plugins": ["preval", ["transform-decorators-legacy"]]
|
||||
}
|
||||
```
|
||||
|
||||
## FAQ
|
||||
|
||||
### How is this different from prepack?
|
||||
|
||||
[`prepack`][prepack] is intended to be run on your final bundle after you've run
|
||||
your webpack/etc magic on it. It does a TON of stuff, but the idea is that your
|
||||
code should work with or without prepack.
|
||||
|
||||
`babel-plugin-preval` is intended to let you write code that would _not_ work
|
||||
otherwise. Doing things like reading something from the file system are not
|
||||
possible in the browser (or with prepack), but `preval` enables you to do this.
|
||||
|
||||
### How is this different from webpack loaders?
|
||||
|
||||
This plugin was inspired by webpack's [val-loader][val-loader]. The benefit of
|
||||
using this over that loader (or any other loader) is that it integrates with
|
||||
your existing babel pipeline. This is especially useful for the server where
|
||||
you're probably not bundling your code with [`webpack`][webpack], but you may be
|
||||
using babel. (If you're not using either, configuring babel for this would be
|
||||
easier than configuring webpack for `val-loader`).
|
||||
|
||||
In addition, you can implement pretty much any webpack loader using
|
||||
`babel-plugin-preval`.
|
||||
|
||||
If you want to learn more, check `webpack` documentations about
|
||||
[`loaders`][webpack-loaders].
|
||||
|
||||
## Inspiration
|
||||
|
||||
I needed something like this for the
|
||||
[glamorous website](https://github.com/kentcdodds/glamorous-website). I
|
||||
live-streamed developing the whole thing. If you're interested you can find
|
||||
[the recording on my youtube channel](https://www.youtube.com/watch?v=3vxov5xUai8&index=19&list=PLV5CVI1eNcJh5CTgArGVwANebCrAh2OUE)
|
||||
(note, screen only recording, no audio).
|
||||
|
||||
I was inspired by the [val-loader][val-loader] from webpack.
|
||||
|
||||
## Related Projects
|
||||
|
||||
- [`preval.macro`][preval.macro] - nicer integration with `babel-plugin-macros`
|
||||
|
||||
## Other Solutions
|
||||
|
||||
I'm not aware of any, if you are please [make a pull request][prs] and add it
|
||||
here!
|
||||
|
||||
## Contributors
|
||||
|
||||
Thanks goes to these people ([emoji key][emojis]):
|
||||
|
||||
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
|
||||
<!-- prettier-ignore -->
|
||||
<table>
|
||||
<tr>
|
||||
<td align="center"><a href="https://kentcdodds.com"><img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;" alt="Kent C. Dodds"/><br /><sub><b>Kent C. Dodds</b></sub></a><br /><a href="https://github.com/kentcdodds/babel-plugin-preval/commits?author=kentcdodds" title="Code">💻</a> <a href="https://github.com/kentcdodds/babel-plugin-preval/commits?author=kentcdodds" title="Documentation">📖</a> <a href="#infra-kentcdodds" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a> <a href="https://github.com/kentcdodds/babel-plugin-preval/commits?author=kentcdodds" title="Tests">⚠️</a></td>
|
||||
<td align="center"><a href="http://mattphillips.io"><img src="https://avatars3.githubusercontent.com/u/5610087?v=3" width="100px;" alt="Matt Phillips"/><br /><sub><b>Matt Phillips</b></sub></a><br /><a href="https://github.com/kentcdodds/babel-plugin-preval/commits?author=mattphillips" title="Code">💻</a> <a href="https://github.com/kentcdodds/babel-plugin-preval/commits?author=mattphillips" title="Documentation">📖</a> <a href="https://github.com/kentcdodds/babel-plugin-preval/commits?author=mattphillips" title="Tests">⚠️</a></td>
|
||||
<td align="center"><a href="https://twitter.com/philipodev"><img src="https://avatars1.githubusercontent.com/u/28024000?v=3" width="100px;" alt="Philip Oliver"/><br /><sub><b>Philip Oliver</b></sub></a><br /><a href="https://github.com/kentcdodds/babel-plugin-preval/issues?q=author%3Aphilipodev" title="Bug reports">🐛</a></td>
|
||||
<td align="center"><a href="https://toot.cafe/@sorin"><img src="https://avatars2.githubusercontent.com/u/2109702?v=3" width="100px;" alt="Sorin Davidoi"/><br /><sub><b>Sorin Davidoi</b></sub></a><br /><a href="https://github.com/kentcdodds/babel-plugin-preval/issues?q=author%3Asorin-davidoi" title="Bug reports">🐛</a> <a href="https://github.com/kentcdodds/babel-plugin-preval/commits?author=sorin-davidoi" title="Code">💻</a> <a href="https://github.com/kentcdodds/babel-plugin-preval/commits?author=sorin-davidoi" title="Tests">⚠️</a></td>
|
||||
<td align="center"><a href="https://github.com/infiniteluke"><img src="https://avatars4.githubusercontent.com/u/1127238?v=4" width="100px;" alt="Luke Herrington"/><br /><sub><b>Luke Herrington</b></sub></a><br /><a href="#example-infiniteluke" title="Examples">💡</a></td>
|
||||
<td align="center"><a href="http://instagram.com/luftywiranda13"><img src="https://avatars4.githubusercontent.com/u/22868432?v=4" width="100px;" alt="Lufty Wiranda"/><br /><sub><b>Lufty Wiranda</b></sub></a><br /><a href="https://github.com/kentcdodds/babel-plugin-preval/commits?author=luftywiranda13" title="Code">💻</a></td>
|
||||
<td align="center"><a href="http://obartra.github.io"><img src="https://avatars0.githubusercontent.com/u/3877773?v=4" width="100px;" alt="Oscar"/><br /><sub><b>Oscar</b></sub></a><br /><a href="https://github.com/kentcdodds/babel-plugin-preval/commits?author=obartra" title="Code">💻</a> <a href="https://github.com/kentcdodds/babel-plugin-preval/commits?author=obartra" title="Tests">⚠️</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center"><a href="https://github.com/pro-nasa"><img src="https://avatars1.githubusercontent.com/u/14310216?v=4" width="100px;" alt="pro-nasa"/><br /><sub><b>pro-nasa</b></sub></a><br /><a href="https://github.com/kentcdodds/babel-plugin-preval/commits?author=pro-nasa" title="Documentation">📖</a></td>
|
||||
<td align="center"><a href="http://bekrin.me"><img src="https://avatars0.githubusercontent.com/u/9248479?v=4" width="100px;" alt="Sergey Bekrin"/><br /><sub><b>Sergey Bekrin</b></sub></a><br /></td>
|
||||
<td align="center"><a href="https://maurobringolf.ch"><img src="https://avatars0.githubusercontent.com/u/18613301?v=4" width="100px;" alt="Mauro Bringolf"/><br /><sub><b>Mauro Bringolf</b></sub></a><br /><a href="https://github.com/kentcdodds/babel-plugin-preval/commits?author=maurobringolf" title="Code">💻</a> <a href="https://github.com/kentcdodds/babel-plugin-preval/commits?author=maurobringolf" title="Tests">⚠️</a></td>
|
||||
<td align="center"><a href="https://joelim.me"><img src="https://avatars1.githubusercontent.com/u/10875678?v=4" width="100px;" alt="Joe Lim"/><br /><sub><b>Joe Lim</b></sub></a><br /><a href="https://github.com/kentcdodds/babel-plugin-preval/commits?author=xjlim" title="Code">💻</a></td>
|
||||
<td align="center"><a href="https://github.com/marzelin"><img src="https://avatars3.githubusercontent.com/u/13483453?v=4" width="100px;" alt="Marcin Zielinski"/><br /><sub><b>Marcin Zielinski</b></sub></a><br /><a href="https://github.com/kentcdodds/babel-plugin-preval/commits?author=marzelin" title="Code">💻</a></td>
|
||||
<td align="center"><a href="http://www.tommyleunen.com"><img src="https://avatars3.githubusercontent.com/u/1972567?v=4" width="100px;" alt="Tommy"/><br /><sub><b>Tommy</b></sub></a><br /><a href="https://github.com/kentcdodds/babel-plugin-preval/commits?author=tleunen" title="Code">💻</a></td>
|
||||
<td align="center"><a href="https://github.com/PlayMa256"><img src="https://avatars0.githubusercontent.com/u/831308?v=4" width="100px;" alt="Matheus Gonçalves da Silva"/><br /><sub><b>Matheus Gonçalves da Silva</b></sub></a><br /><a href="https://github.com/kentcdodds/babel-plugin-preval/commits?author=PlayMa256" title="Documentation">📖</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center"><a href="https://stackshare.io/jdorfman/decisions"><img src="https://avatars1.githubusercontent.com/u/398230?v=4" width="100px;" alt="Justin Dorfman"/><br /><sub><b>Justin Dorfman</b></sub></a><br /><a href="#fundingFinding-jdorfman" title="Funding Finding">🔍</a></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<!-- ALL-CONTRIBUTORS-LIST:END -->
|
||||
|
||||
This project follows the [all-contributors][all-contributors] specification.
|
||||
Contributions of any kind welcome!
|
||||
|
||||
## LICENSE
|
||||
|
||||
MIT
|
||||
|
||||
[npm]: https://www.npmjs.com/
|
||||
[node]: https://nodejs.org
|
||||
[build-badge]: https://img.shields.io/travis/kentcdodds/babel-plugin-preval.svg?style=flat-square
|
||||
[build]: https://travis-ci.org/kentcdodds/babel-plugin-preval
|
||||
[coverage-badge]: https://img.shields.io/codecov/c/github/kentcdodds/babel-plugin-preval.svg?style=flat-square
|
||||
[coverage]: https://codecov.io/github/kentcdodds/babel-plugin-preval
|
||||
[version-badge]: https://img.shields.io/npm/v/babel-plugin-preval.svg?style=flat-square
|
||||
[package]: https://www.npmjs.com/package/babel-plugin-preval
|
||||
[downloads-badge]: https://img.shields.io/npm/dm/babel-plugin-preval.svg?style=flat-square
|
||||
[npmtrends]: http://www.npmtrends.com/babel-plugin-preval
|
||||
[license-badge]: https://img.shields.io/npm/l/babel-plugin-preval.svg?style=flat-square
|
||||
[license]: https://github.com/kentcdodds/babel-plugin-preval/blob/master/LICENSE
|
||||
[prs-badge]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square
|
||||
[prs]: http://makeapullrequest.com
|
||||
[donate-badge]: https://img.shields.io/badge/$-support-green.svg?style=flat-square
|
||||
[donate]: http://kcd.im/donate
|
||||
[coc-badge]: https://img.shields.io/badge/code%20of-conduct-ff69b4.svg?style=flat-square
|
||||
[coc]: https://github.com/kentcdodds/babel-plugin-preval/blob/master/other/CODE_OF_CONDUCT.md
|
||||
[macros-badge]: https://img.shields.io/badge/babel--macro-%F0%9F%8E%A3-f5da55.svg?style=flat-square
|
||||
[babel-plugin-macros]: https://github.com/kentcdodds/babel-plugin-macros
|
||||
[examples-badge]: https://img.shields.io/badge/%F0%9F%92%A1-examples-8C8E93.svg?style=flat-square
|
||||
[examples]: https://github.com/kentcdodds/babel-plugin-preval/blob/master/other/EXAMPLES.md
|
||||
[github-watch-badge]: https://img.shields.io/github/watchers/kentcdodds/babel-plugin-preval.svg?style=social
|
||||
[github-watch]: https://github.com/kentcdodds/babel-plugin-preval/watchers
|
||||
[github-star-badge]: https://img.shields.io/github/stars/kentcdodds/babel-plugin-preval.svg?style=social
|
||||
[github-star]: https://github.com/kentcdodds/babel-plugin-preval/stargazers
|
||||
[twitter]: https://twitter.com/intent/tweet?text=Check%20out%20babel-plugin-preval!%20https://github.com/kentcdodds/babel-plugin-preval%20%F0%9F%91%8D
|
||||
[twitter-badge]: https://img.shields.io/twitter/url/https/github.com/kentcdodds/babel-plugin-preval.svg?style=social
|
||||
[emojis]: https://github.com/kentcdodds/all-contributors#emoji-key
|
||||
[all-contributors]: https://github.com/kentcdodds/all-contributors
|
||||
[prepack]: https://github.com/facebook/prepack
|
||||
[preval.macro]: https://github.com/kentcdodds/preval.macro
|
||||
[webpack]: https://webpack.js.org/
|
||||
[webpack-loaders]: https://webpack.js.org/concepts/loaders/
|
||||
[val-loader]: https://github.com/webpack-contrib/val-loader
|
||||
49
themes/keepit/node_modules/babel-plugin-preval/dist/helpers.js
generated
vendored
Normal file
49
themes/keepit/node_modules/babel-plugin-preval/dist/helpers.js
generated
vendored
Normal file
@@ -0,0 +1,49 @@
|
||||
"use strict";
|
||||
|
||||
const p = require('path');
|
||||
|
||||
const requireFromStringOfCode = require('require-from-string');
|
||||
|
||||
const objectToAST = require('./object-to-ast');
|
||||
|
||||
module.exports = {
|
||||
getReplacement,
|
||||
requireFromString
|
||||
};
|
||||
|
||||
function requireFromString({
|
||||
string: stringToPreval,
|
||||
fileOpts: {
|
||||
filename
|
||||
},
|
||||
args = []
|
||||
}) {
|
||||
let mod = requireFromStringOfCode(String(stringToPreval), filename);
|
||||
mod = mod && mod.__esModule ? mod.default : mod;
|
||||
|
||||
if (typeof mod === 'function') {
|
||||
mod = mod(...args);
|
||||
} else if (args.length) {
|
||||
throw new Error(`\`preval.require\`-ed module (${p.relative(process.cwd(), filename)}) cannot accept arguments because it does not export a function. You passed the arguments: ${args.join(', ')}`);
|
||||
}
|
||||
|
||||
return mod;
|
||||
}
|
||||
|
||||
function getReplacement({
|
||||
string,
|
||||
fileOpts,
|
||||
args,
|
||||
babel
|
||||
}) {
|
||||
const mod = requireFromString({
|
||||
string,
|
||||
fileOpts,
|
||||
args,
|
||||
babel
|
||||
});
|
||||
return objectToAST(mod, {
|
||||
babel,
|
||||
fileOptions: fileOpts
|
||||
});
|
||||
}
|
||||
208
themes/keepit/node_modules/babel-plugin-preval/dist/index.js
generated
vendored
Normal file
208
themes/keepit/node_modules/babel-plugin-preval/dist/index.js
generated
vendored
Normal file
@@ -0,0 +1,208 @@
|
||||
"use strict";
|
||||
|
||||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
||||
|
||||
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
||||
|
||||
const p = require('path');
|
||||
|
||||
const fs = require('fs'); // const printAST = require('ast-pretty-print')
|
||||
|
||||
|
||||
const {
|
||||
getReplacement,
|
||||
requireFromString
|
||||
} = require('./helpers');
|
||||
|
||||
module.exports = prevalPlugin;
|
||||
|
||||
function prevalPlugin(babel) {
|
||||
const {
|
||||
types: t,
|
||||
template,
|
||||
transformFromAst
|
||||
} = babel;
|
||||
const assignmentBuilder = template('const NAME = VALUE');
|
||||
return {
|
||||
name: 'preval',
|
||||
visitor: {
|
||||
Program(path, {
|
||||
file: {
|
||||
opts: fileOpts
|
||||
}
|
||||
}) {
|
||||
const firstNode = path.node.body[0] || {};
|
||||
const comments = firstNode.leadingComments || [];
|
||||
const isPreval = comments.some(isPrevalComment);
|
||||
|
||||
if (!isPreval) {
|
||||
return;
|
||||
}
|
||||
|
||||
comments.find(isPrevalComment).value = ' this file was prevaled';
|
||||
const {
|
||||
code: string
|
||||
} = transformFromAst(path.node, null,
|
||||
/* istanbul ignore next (babel 6 vs babel 7 check) */
|
||||
/^6\./.test(babel.version) ? {} : {
|
||||
babelrc: false,
|
||||
configFile: false
|
||||
});
|
||||
const replacement = getReplacement({
|
||||
string,
|
||||
fileOpts,
|
||||
babel
|
||||
});
|
||||
const moduleExports = (0, _extends2.default)({}, t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.identifier('module'), t.identifier('exports')), replacement)), {
|
||||
leadingComments: comments
|
||||
});
|
||||
path.replaceWith(t.program([moduleExports]));
|
||||
},
|
||||
|
||||
TaggedTemplateExpression(path, {
|
||||
file: {
|
||||
opts: fileOpts
|
||||
}
|
||||
}) {
|
||||
const isPreval = path.node.tag.name === 'preval';
|
||||
|
||||
if (!isPreval) {
|
||||
return;
|
||||
}
|
||||
|
||||
const string = path.get('quasi').evaluate().value;
|
||||
|
||||
if (!string) {
|
||||
throw new Error('Unable to determine the value of your preval string');
|
||||
}
|
||||
|
||||
const replacement = getReplacement({
|
||||
string,
|
||||
fileOpts,
|
||||
babel
|
||||
});
|
||||
path.replaceWith(replacement);
|
||||
},
|
||||
|
||||
ImportDeclaration(path, {
|
||||
file: {
|
||||
opts: fileOpts
|
||||
}
|
||||
}) {
|
||||
const isPreval = looksLike(path, {
|
||||
node: {
|
||||
source: {
|
||||
leadingComments(comments) {
|
||||
return comments && comments.some(isPrevalComment);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (!isPreval) {
|
||||
return;
|
||||
}
|
||||
|
||||
const prevalComment = path.node.source.leadingComments.find(isPrevalComment).value.trim();
|
||||
let argValues;
|
||||
|
||||
if (prevalComment !== 'preval') {
|
||||
const arg = prevalComment.replace(/preval\((.*)\)/, '$1').trim();
|
||||
const argValue = requireFromString({
|
||||
string: `module.exports = ${arg}`,
|
||||
fileOpts
|
||||
});
|
||||
argValues = [argValue];
|
||||
}
|
||||
|
||||
const absolutePath = p.resolve(p.dirname(fileOpts.filename), path.node.source.value);
|
||||
const code = fs.readFileSync(require.resolve(absolutePath));
|
||||
const replacement = getReplacement({
|
||||
string: code,
|
||||
fileOpts,
|
||||
args: argValues,
|
||||
babel
|
||||
});
|
||||
path.replaceWith(assignmentBuilder({
|
||||
NAME: t.identifier(path.node.specifiers[0].local.name),
|
||||
VALUE: replacement
|
||||
}));
|
||||
},
|
||||
|
||||
CallExpression(path, {
|
||||
file: {
|
||||
opts: fileOpts
|
||||
}
|
||||
}) {
|
||||
const isPreval = looksLike(path, {
|
||||
node: {
|
||||
callee: {
|
||||
type: 'MemberExpression',
|
||||
object: {
|
||||
name: 'preval'
|
||||
},
|
||||
property: {
|
||||
name: 'require'
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (!isPreval) {
|
||||
return;
|
||||
}
|
||||
|
||||
const [source, ...args] = path.get('arguments');
|
||||
const argValues = args.map(a => {
|
||||
const result = a.evaluate();
|
||||
|
||||
if (!result.confident) {
|
||||
throw new Error('preval cannot determine the value of an argument in preval.require');
|
||||
}
|
||||
|
||||
return result.value;
|
||||
});
|
||||
const absolutePath = p.resolve(p.dirname(fileOpts.filename), source.node.value);
|
||||
const code = fs.readFileSync(require.resolve(absolutePath));
|
||||
const replacement = getReplacement({
|
||||
string: code,
|
||||
fileOpts,
|
||||
args: argValues,
|
||||
babel
|
||||
});
|
||||
path.replaceWith(replacement);
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function isPrevalComment(comment) {
|
||||
const normalisedComment = comment.value.trim().split(' ')[0].trim();
|
||||
return normalisedComment.startsWith('preval') || normalisedComment.startsWith('@preval');
|
||||
}
|
||||
|
||||
function looksLike(a, b) {
|
||||
return a && b && Object.keys(b).every(bKey => {
|
||||
const bVal = b[bKey];
|
||||
const aVal = a[bKey];
|
||||
|
||||
if (typeof bVal === 'function') {
|
||||
return bVal(aVal);
|
||||
}
|
||||
|
||||
return isPrimitive(bVal) ? bVal === aVal : looksLike(aVal, bVal);
|
||||
});
|
||||
}
|
||||
|
||||
function isPrimitive(val) {
|
||||
// eslint-disable-next-line
|
||||
return val == null || /^[sbn]/.test(typeof val);
|
||||
}
|
||||
/*
|
||||
eslint
|
||||
import/no-unassigned-import: off,
|
||||
import/no-dynamic-require: off,
|
||||
max-lines-per-function: off,
|
||||
*/
|
||||
87
themes/keepit/node_modules/babel-plugin-preval/dist/macro.js
generated
vendored
Normal file
87
themes/keepit/node_modules/babel-plugin-preval/dist/macro.js
generated
vendored
Normal file
@@ -0,0 +1,87 @@
|
||||
"use strict";
|
||||
|
||||
// const printAST = require('ast-pretty-print')
|
||||
const {
|
||||
createMacro
|
||||
} = require('babel-plugin-macros');
|
||||
|
||||
const {
|
||||
getReplacement
|
||||
} = require('./helpers');
|
||||
|
||||
module.exports = createMacro(prevalMacros);
|
||||
|
||||
function prevalMacros({
|
||||
references,
|
||||
state,
|
||||
babel
|
||||
}) {
|
||||
references.default.forEach(referencePath => {
|
||||
if (referencePath.parentPath.type === 'TaggedTemplateExpression') {
|
||||
asTag(referencePath.parentPath.get('quasi'), state, babel);
|
||||
} else if (referencePath.parentPath.type === 'CallExpression') {
|
||||
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')) {
|
||||
throw new Error(`babel-plugin-preval/macro can only be used as tagged template expression, function call or JSX element. You tried ${referencePath.parentPath.type}.`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function asTag(quasiPath, {
|
||||
file: {
|
||||
opts: fileOpts
|
||||
}
|
||||
}, babel) {
|
||||
const string = quasiPath.parentPath.get('quasi').evaluate().value;
|
||||
quasiPath.parentPath.replaceWith(getReplacement({
|
||||
string,
|
||||
fileOpts,
|
||||
babel
|
||||
}));
|
||||
}
|
||||
|
||||
function asFunction(argumentsPaths, {
|
||||
file: {
|
||||
opts: fileOpts
|
||||
}
|
||||
}, babel) {
|
||||
const string = argumentsPaths[0].evaluate().value;
|
||||
argumentsPaths[0].parentPath.replaceWith(getReplacement({
|
||||
string,
|
||||
fileOpts,
|
||||
babel
|
||||
}));
|
||||
} // eslint-disable-next-line no-unused-vars
|
||||
|
||||
|
||||
function asJSX({
|
||||
attributes,
|
||||
children
|
||||
}, {
|
||||
file: {
|
||||
opts: fileOpts
|
||||
}
|
||||
}, babel) {
|
||||
// It's a shame you cannot use evaluate() with JSX
|
||||
const string = children[0].node.value;
|
||||
children[0].replaceWith(getReplacement({
|
||||
string,
|
||||
fileOpts,
|
||||
babel
|
||||
}));
|
||||
const {
|
||||
parentPath: {
|
||||
node: {
|
||||
openingElement,
|
||||
closingElement
|
||||
}
|
||||
}
|
||||
} = children[0];
|
||||
openingElement.name.name = 'div';
|
||||
closingElement.name.name = 'div';
|
||||
}
|
||||
39
themes/keepit/node_modules/babel-plugin-preval/dist/object-to-ast.js
generated
vendored
Normal file
39
themes/keepit/node_modules/babel-plugin-preval/dist/object-to-ast.js
generated
vendored
Normal file
@@ -0,0 +1,39 @@
|
||||
"use strict";
|
||||
|
||||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
||||
|
||||
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
||||
|
||||
module.exports = objectToAST;
|
||||
|
||||
function objectToAST(object, {
|
||||
babel,
|
||||
fileOptions
|
||||
}) {
|
||||
const stringified = stringify(object);
|
||||
const variableDeclarationNode = babel.template(`var x = ${stringified}`, (0, _extends2.default)({
|
||||
preserveComments: true,
|
||||
placeholderPattern: false
|
||||
}, fileOptions.parserOpts, {
|
||||
sourceType: 'module'
|
||||
}))();
|
||||
return variableDeclarationNode.declarations[0].init;
|
||||
}
|
||||
|
||||
function stringify(object) {
|
||||
let str = JSON.stringify(object, function (key, value) {
|
||||
if (typeof value === 'function') {
|
||||
return `__FUNCTION_START__${value.toString()}__FUNCTION_END__`;
|
||||
}
|
||||
|
||||
return value;
|
||||
});
|
||||
|
||||
if (str === undefined) {
|
||||
str = 'undefined';
|
||||
}
|
||||
|
||||
return str.replace(/"__FUNCTION_START__(.*?)__FUNCTION_END__"/g, function (match, p1) {
|
||||
return p1.replace(/\\"/g, '"').replace(/\\n/g, '\n');
|
||||
});
|
||||
}
|
||||
3
themes/keepit/node_modules/babel-plugin-preval/macro.js
generated
vendored
Normal file
3
themes/keepit/node_modules/babel-plugin-preval/macro.js
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
// this is here to make the import/require API nicer:
|
||||
// import preval from 'babel-plugin-preval/macro'
|
||||
module.exports = require('./dist/macro')
|
||||
66
themes/keepit/node_modules/babel-plugin-preval/package.json
generated
vendored
Normal file
66
themes/keepit/node_modules/babel-plugin-preval/package.json
generated
vendored
Normal file
@@ -0,0 +1,66 @@
|
||||
{
|
||||
"name": "babel-plugin-preval",
|
||||
"version": "4.0.0",
|
||||
"description": "Pre-evaluate code at build-time",
|
||||
"engines": {
|
||||
"node": ">=8",
|
||||
"npm": ">=6"
|
||||
},
|
||||
"main": "dist/index.js",
|
||||
"scripts": {
|
||||
"build": "kcd-scripts build",
|
||||
"lint": "kcd-scripts lint",
|
||||
"test": "kcd-scripts test",
|
||||
"test:update": "npm test -- --updateSnapshot --coverage",
|
||||
"validate": "kcd-scripts validate"
|
||||
},
|
||||
"husky": {
|
||||
"hooks": {
|
||||
"pre-commit": "kcd-scripts pre-commit"
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"dist",
|
||||
"macro.js"
|
||||
],
|
||||
"keywords": [
|
||||
"babel",
|
||||
"babel-plugin",
|
||||
"eval",
|
||||
"precompile",
|
||||
"babel-plugin-macros"
|
||||
],
|
||||
"author": "Kent C. Dodds <kent@doddsfamily.us> (http://kentcdodds.com/)",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.7.2",
|
||||
"babel-plugin-macros": "^2.6.1",
|
||||
"require-from-string": "^2.0.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"ast-pretty-print": "^2.0.1",
|
||||
"babel-plugin-tester": "^7.0.3",
|
||||
"kcd-scripts": "^1.11.0"
|
||||
},
|
||||
"eslintConfig": {
|
||||
"extends": "./node_modules/kcd-scripts/eslint.js"
|
||||
},
|
||||
"eslintIgnore": [
|
||||
"node_modules",
|
||||
"coverage",
|
||||
"dist"
|
||||
],
|
||||
"babel": {
|
||||
"presets": [
|
||||
"kcd-scripts/babel"
|
||||
]
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/kentcdodds/babel-plugin-preval.git"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/kentcdodds/babel-plugin-preval/issues"
|
||||
},
|
||||
"homepage": "https://github.com/kentcdodds/babel-plugin-preval#readme"
|
||||
}
|
||||
Reference in New Issue
Block a user