Skip to content

arash16/babel-plugin-ceval

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

babel-plugin-ceval

NPM version

This plugin allows Babel to execute ceval functions at compile time. It can be used for any kind of code generations that needs to happen at build time. Instead of writing complicated build scripts, you can write those code generation logics right inside your code.

Table of Contents

Installation

npm install --save-dev babel-plugin-ceval

Usage

Via .babelrc (Recommended)

.babelrc

{
  "plugins": ["ceval"]
}

Via CLI

babel --plugins ceval script.js

API

ceval(expression: string)

Return value is used as is, it cannot return code fragments.

ceval(fn: function([args..])[, args..])

If it returns a string, it is expected to be a code fragment. If you need to return a string value, simply enclose it with quotations (If string contains quotations, use JSON.stringify).

Examples

Reading environment variables

// In:
var envPath = ceval('process.env.PATH');
// Out:
var envPath = '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games';

Current source directory / filename

// In:
var dirname = ceval('__dirname');
var filename= ceval('__filename');
// Out:
var dirname = '/home/arash16/Projects/ceval-test';
var filename = '/home/arash16/Projects/ceval-test/test.js';

Version information from package.json

// In:
var version = ceval('require("./package.json").version');
// Out:
var version = '1.0.0';

Generate js code via js code

// In:
ceval(function() {
  var r = '';
  for (var i=0; i<4; ++i)
    r += 'console.log('+i+');';
  return r;
});
// Out:
console.log(0);console.log(1);console.log(2);console.log(3);

Return string from ceval(fn: function)

// In:
var code = ceval(function() {
  var r = '';
  for (var i=0; i<3; ++i)
    r += 'console.log('+i+');';
  return JSON.stringify(r);
});
// Out:
var code = 'console.log(0);console.log(1);console.log(2);';

Different functions for different environments

// In:
ceval(function() {
  if (process.env.SERVER)
    return function checker(x) { 
      return x>2; 
    };
  
  return function checker(x) { 
    return x>0; 
  };
});
// Out:
function checker(x) {
  return x > 0;
}

Reading outside variables (they must be statically evaluatable)

// In:
const X = 1, Y = 2;
ceval(function(a, b) {
  return 'console.log(' + (a+b) + ');';
}, X, Y);
// Out:
const X = 1,
      Y = 2;
console.log(3);

Return complete objects

// In:
var obj = ceval(function() {
  return {
    regex: /abc/g,
    str: 'asdas',
    arr: [1,2, { x: 1}],
    fn: function (a, b) {
      return a + b;
    }
  };
});
// Out:
var obj = {
  'regex': /abc/g,
  'str': 'asdas',
  'arr': [1, 2, {
    'x': 1
  }],
  'fn': function (a, b) {
    return a + b;
  }
};

Return a Promise

If you need to return a promise, make sure to install deasync too.

npm install --save-dev deasync
// In:
ceval(function() {
  return Promise
    .resolve('read from database')
    .then(x => `function dyna() {
      return ${JSON.stringify(x.split(' '))};
    }`);
});
// Out:
function dyna() {
  return ["read", "from", "database"];
}

About

Compile time evaluation

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published