Pre-evaluate code at build-time
npm install babel-plugin-prevalPre-evaluate code at build-time
---
[![Build Status][build-badge]][build]
[![Code Coverage][coverage-badge]][coverage]
[![version][version-badge]][package]
[![downloads][downloads-badge]][npmtrends]
[![MIT License][license-badge]][license]

[![PRs Welcome][prs-badge]][prs]
[![Code of Conduct][coc-badge]][coc]
[![Babel Macro][macros-badge]][babel-plugin-macros]
[![Examples][examples-badge]][examples]
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 allows you to specify some code that runs in Node and whatever youmodule.exports in there will be swapped. For example:
``jsmodule.exports = 1
const x = preval
// ↓ ↓ ↓ ↓ ↓ ↓
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') andimport x from / preval / './something' (which can both take some arguments)// @preval
or add comment at the top of a file.
See more below.
- Installation
- Usage
- Template Tag
- import comment
- preval.require
- preval file comment (// @preval)
- Exporting a function
- Configure with Babel
- Via .babelrc (Recommended)
- Via CLI
- Via Node API
- Use with babel-plugin-macros
- Examples
- Notes
- FAQ
- How is this different from prepack?
- How is this different from webpack loaders?
- Inspiration
- Related Projects
- Other Solutions
- Issues
- 🐛 Bugs
- 💡 Feature Requests
- Contributors ✨
- LICENSE
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
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
> to get an idea of what preval is and how it can be used.
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'}
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']
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'
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
If you export a function from a module that you're prevaling (whether using
preval.require or the import comment), then that function will be called and
whatever is returned will be the prevaled value.
It's important to know this if you want to have the prevaled value itself be a
function:
Example:
`jsThe message is: ${message}
// example-module.js
const fn = message => `
module.exports = () => fn
Usage of preval:
`js`
const theFn = preval.require('./example-module.js')
Generated code:
`jsThe message is: ${message}
const theFn = message => `
.babelrc
`json`
{
"plugins": ["preval"]
}
`sh`
babel --plugins preval script.js
`javascript`
require('babel-core').transform('code', {
plugins: ['preval'],
})
Once you've
configured babel-plugin-macros
you can import/require the preval macro at babel-plugin-preval/macro. For
example:
`javascript
import preval from 'babel-plugin-preval/macro'
const one = prevalmodule.exports = 1 + 2 - 1 - 1`
> You could also use [preval.macro][preval.macro] if you'd prefer to type less
> 😀
- Mastodon saved 40kb
(gzipped) using babel-plugin-prevalpreval.macro
- glamorous-website
uses [][preval.macro] to determine Algolia options based onprocess.env.LOCALE
. It also uses [preval.macro][preval.macro] to load ansvg
file as a string, base64 encode it, and use it as a background-url
for an input element.
- Generate documentation for React components
- Serverless with webpack
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)
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"]]
}
[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_ workpreval
otherwise. Doing things like reading something from the file system are not
possible in the browser (or with prepack), but enables you to do this.
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 beval-loader
using babel. (If you're not using either, configuring babel for this would be
easier than configuring webpack for ).
In addition, you can implement pretty much any webpack loader using
babel-plugin-preval.
If you want to learn more, check webpack documentations aboutloaders
[][webpack-loaders].
I needed something like this for the
glamorous website. I
live-streamed developing the whole thing. If you're interested you can find
the recording on my youtube channel
(note, screen only recording, no audio).
I was inspired by the [val-loader][val-loader] from webpack.
- [preval.macro][preval.macro] - nicer integration with babel-plugin-macrosdefine_macro-loader` -
-
Integrate preval with C-Like macros, thus, increasing preval use cases.
I'm not aware of any, if you are please [make a pull request][prs] and add it
here!
_Looking to contribute? Look for the [Good First Issue][good-first-issue]
label._
Please file an issue for bugs, missing documentation, or unexpected behavior.
[See Bugs][bugs]
Please file an issue to suggest new features. Vote on feature requests by adding
a 👍. This helps maintainers prioritize what to work on.
[See Feature Requests][requests]
Thanks goes to these people ([emoji key][emojis]):
This project follows the [all-contributors][all-contributors] specification.
Contributions of any kind welcome!
MIT
[npm]: https://www.npmjs.com
[node]: https://nodejs.org
[build-badge]: https://img.shields.io/github/workflow/status/kentcdodds/babel-plugin-preval/validate?logo=github&style=flat-square
[build]: https://github.com/kentcdodds/babel-plugin-preval/actions?query=workflow%3Avalidate
[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/main/LICENSE
[prs-badge]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square
[prs]: http://makeapullrequest.com
[coc-badge]: https://img.shields.io/badge/code%20of-conduct-ff69b4.svg?style=flat-square
[coc]: https://github.com/kentcdodds/babel-plugin-preval/blob/main/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/main/other/EXAMPLES.md
[emojis]: https://github.com/all-contributors/all-contributors#emoji-key
[all-contributors]: https://github.com/all-contributors/all-contributors
[bugs]: https://github.com/kentcdodds/babel-plugin-preval/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+sort%3Acreated-desc+label%3Abug
[requests]: https://github.com/kentcdodds/babel-plugin-preval/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+sort%3Areactions-%2B1-desc+label%3Aenhancement
[good-first-issue]: https://github.com/kentcdodds/babel-plugin-preval/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+sort%3Areactions-%2B1-desc+label%3Aenhancement+label%3A%22good+first+issue%22
[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