babel-plugin-preval is a Babel plugin designed to pre-evaluate JavaScript code at build-time, effectively replacing dynamic runtime computations with static, precomputed values. Its current stable version is 5.1.0. While the last major release was in 2022, the package continues to be used, with discussions and related macro packages still active in the community. This plugin allows for tasks like reading file systems or performing complex calculations during the build process, which are typically impossible or inefficient at runtime, especially in browser environments. Unlike generic build-time evaluators, preval integrates directly into the Babel compilation pipeline and provides multiple mechanisms for invocation, including template literal tags, `preval.require` calls, and `/* preval */` import comments. A crucial distinction is that the code executed by `preval` runs directly in a Node.js environment, is not sandboxed, and is *not* automatically transpiled by Babel, meaning it must be compatible with the Node.js version used for the build process.
npm install babel-plugin-prevalVerified import paths — ran on the pinned version, not inferred.
Demonstrates `babel-plugin-preval` reading a file and evaluating a module at build-time, embedding static results into the output bundle. This requires a Babel setup with the plugin configured.
Ensure your build environment uses Node.js version 10 or newer. It is recommended to use the latest LTS Node.js version for compatibility.
Update prevaled code to use syntax compatible with your Node.js build environment (e.g., CommonJS modules instead of ESM, older JavaScript features). If modern syntax is required, pre-transpile the prevaled code using a separate Babel pass or ensure your Node.js version supports it. For instance, do not use `import/export` syntax within `preval` code blocks, stick to `require()` and `module.exports`.
Only preval code from trusted sources and carefully review any third-party code that might be evaluated by the plugin. Treat prevaled code with the same security considerations as any Node.js script.
Refactor prevaled code to exclusively use synchronous Node.js APIs (e.g., `fs.readFileSync` instead of `fs.readFile`). Ensure all operations complete before `module.exports` is assigned a value.
To force a recompile, you may need to touch the file containing the `preval` call or clear your Babel/Webpack cache. Some build systems might require specific configurations to properly track dependencies for `preval` files.
Ensure 'babel-plugin-preval' is listed in the `plugins` array of your `.babelrc` or `babel.config.js` file. If using `preval.macro`, ensure `babel-plugin-macros` is configured and you are importing `preval` from 'preval.macro'.
Rewrite the prevaled code to use CommonJS `require()` and `module.exports` syntax. Ensure all code executed by `preval` is compatible with the Node.js version you are using to run Babel, or pre-transpile that specific code.
Refactor the prevaled code to be entirely synchronous. For file system operations, use synchronous methods like `fs.readFileSync` instead of `fs.readFile`. Avoid Promises and `async/await` within prevaled blocks.
Ensure your Babel configuration correctly defines `plugins` as an array, even if it contains only one plugin, e.g., `plugins: ['babel-plugin-preval']`.