kentcdodds/babel-plugin-preval is a TypeScript project with 1.4k stars. π£ Pre-evaluate code at build-time
Snapshot summary built from the project's own GitHub metadata β there's no written TopGit review yet. The page will update automatically when a full review is published.
WHY NO REVIEW YET
TopGit writes full reviews for the most-starred, most-requested repositories. This page is a snapshot until then β see the READ ME tab for the original README in full.
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:
const x = preval`module.exports = 1`
// β β β β β β
const x = 1
Or, more interestingly:
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
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
Installation
This module is distributed via npm which is bundled with node and
should be installed as one of your project's devDependencies:
npm install --save-dev babel-plugin-preval
Usage
Important notes:
All code run by preval is not run in a sandboxed environment
All code must run synchronously.
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.
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:
// @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:
module.exports = 4
Exporting a function
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:
Once you've
configured babel-plugin-macros
you can import/require the preval macro at babel-plugin-preval/macro. For
example:
import preval from 'babel-plugin-preval/macro'
const one = preval`module.exports = 1 + 2 - 1 - 1`
You could also use preval.macro if you'd prefer to type less
π
Examples
Mastodon saved 40kb
(gzipped) using babel-plugin-preval
glamorous-website
uses preval.macro to determine Algolia options based on
process.env.LOCALE. It also uses 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
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)
Notes
If you use babel-plugin-transform-decorators-legacy, there is a conflict
because both plugins must be placed at the top
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. 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, 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.
Inspiration
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 from webpack.
Related Projects
preval.macro - nicer integration with babel-plugin-macros
define_macro-loader -
Integrate preval with C-Like macros, thus, increasing preval use cases.
Other Solutions
I'm not aware of any, if you are please make a pull request and add it
here!
Issues
Looking to contribute? Look for the Good First Issue
label.
π Bugs
Please file an issue for bugs, missing documentation, or unexpected behavior.
See Bugs
π‘ Feature Requests
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
Contributors β¨
Thanks goes to these people (emoji key):
Kent C. Dodds π» π π β οΈ
Matt Phillips π» π β οΈ
Philip Oliver π
Sorin Davidoi π π» β οΈ
Luke Herrington π‘
Lufty Wiranda π»
Oscar π» β οΈ
pro-nasa π
Sergey Bekrin
Mauro Bringolf π» β οΈ
Joe Lim π»
Marcin Zielinski π»
Tommy π»
Matheus GonΓ§alves da Silva π
Justin Dorfman π
Andrew Rottier π
MichaΓ«l De Boey π»
Braydon Hall π»
Jacob M-G Evans π»
Juhana Jauhiainen π»
Peter HozΓ‘k π»
Michael Peyper π»
Marcelo Silva Nascimento Mancini π π
Minh Nguyen π» β οΈ π
This project follows the all-contributors specification.
Contributions of any kind welcome!
Does kentcdodds/babel-plugin-preval have any tags?
TopGit's last sync did not record any GitHub topics for kentcdodds/babel-plugin-preval. GitHub topics appear in the right sidebar of a repository page; that's the authoritative place to check.
How active is development on kentcdodds/babel-plugin-preval?
The most recent commit recorded on kentcdodds/babel-plugin-preval was 4.6 years ago, based on the GitHub push timestamp. The repository has 48 forks β one of the better signals of community interest.
How many stars does kentcdodds/babel-plugin-preval have?
kentcdodds/babel-plugin-preval has 1.4k GitHub stars β refresh the page for the live number, or check github.com/kentcdodds/babel-plugin-preval. TopGit mirrors GitHub's count but does not claim minute-by-minute accuracy.
Is kentcdodds/babel-plugin-preval open source?
Yes β kentcdodds/babel-plugin-preval ships under the MIT license, which makes its source code freely readable (and, depending on license terms, forkable and reusable). Source: github.com/kentcdodds/babel-plugin-preval.
Where can I see kentcdodds/babel-plugin-preval in action?
The project maintains a homepage at https://npm.im/babel-plugin-preval. The README tab on this page also usually contains screenshots and a quickstart.
Where do I read more about kentcdodds/babel-plugin-preval?
This TopGit page is a snapshot β the READ ME tab shows the project's own README content (links stripped, images preserved). The GitHub repository at github.com/kentcdodds/babel-plugin-preval is the definitive source.
Read full README in the tab above.
Is babel-plugin-preval worth your time?
ChatGPT, Claude and Perplexity can all read this page. Ask one of them what it makes of babel-plugin-preval.