{"library":"preval.macro","title":"preval.macro: Build-Time Code Evaluation","description":"preval.macro is a Babel macro designed to pre-evaluate code at build-time, embedding the results directly into the JavaScript bundle. It leverages `babel-plugin-preval` under the hood, offering a streamlined way to achieve static code evaluation without direct Babel plugin configuration, thanks to its integration with `babel-plugin-macros`. The current stable version is 5.0.0. This package is particularly useful for tasks like inlining static file contents, injecting environment variables known at build time, or pre-calculating complex values, thereby reducing runtime computations and potentially optimizing bundle size. Its release cadence is generally aligned with updates to the broader Babel ecosystem and its upstream dependencies, `babel-plugin-preval` and `babel-plugin-macros`.","language":"javascript","status":"active","last_verified":"Tue Apr 21","install":{"commands":["npm install preval.macro"],"cli":null},"imports":["import preval from 'preval.macro'","const preval = require('preval.macro')","preval`module.exports = 'some_value'`"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"/*\n  1. Install dependencies:\n  npm install --save-dev @babel/cli @babel/core babel-plugin-macros preval.macro\n\n  2. Create `.babelrc.js` in your project root:\n  module.exports = {\n    plugins: ['macros'],\n  };\n\n  3. Create `src/index.js`:\n*/\n\nimport preval from 'preval.macro';\n\n// Pre-evaluate a simple arithmetic expression during build\nconst sum = preval`module.exports = 10 + 20`;\nconsole.log('The sum is:', sum); // During build, 'sum' becomes 30\n\n// Pre-evaluate and embed an environment variable (e.g., BUILD_ID=abc)\n// To see this in action, run Babel with an environment variable:\n// BUILD_ID=123 babel src --out-dir dist\nconst buildId = preval`module.exports = process.env.BUILD_ID || 'N/A'`;\nconsole.log('Build ID:', buildId); // 'buildId' becomes '123' at build time\n\n// Read a file at build-time and embed its content\n// Create a file `src/data.txt` with content: 'Hello Preval World!'\nconst fileContent = preval`\n  const fs = require('fs');\n  const path = require('path');\n  module.exports = fs.readFileSync(path.resolve(__dirname, 'data.txt'), 'utf8');\n`;\nconsole.log('File Content:', fileContent); // 'fileContent' becomes 'Hello Preval World!'\n\n/*\n  4. Create `src/data.txt` in the same directory as `src/index.js`\n     with content: `Hello Preval World!`\n\n  5. Compile with Babel from your project root:\n  BUILD_ID=123 babel src --out-dir dist\n\n  6. Run the compiled output:\n  node dist/index.js\n  // Expected output:\n  // The sum is: 30\n  // Build ID: 123\n  // File Content: Hello Preval World!\n*/","lang":"javascript","description":"Demonstrates how to use `preval.macro` to evaluate arithmetic expressions, inject environment variables, and read file contents at build-time, embedding the results directly into the output JavaScript bundle.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}