Coder Social home page Coder Social logo

alexbaizeau / babel-plugin-inline-replace-variables Goto Github PK

View Code? Open in Web Editor NEW

This project forked from wssgcg1213/babel-plugin-inline-replace-variables

0.0 2.0 0.0 14 KB

babel plugin to replace inline variables

Home Page: https://www.npmjs.org/package/babel-plugin-inline-replace-variables

JavaScript 100.00%

babel-plugin-inline-replace-variables's Introduction

babel-plugin-inline-replace-variables

Build Status

It replace an Identifier to a literial (LVal), if you want to transfrom a identifier to another identifier, you can see: babel-plugin-replace-identifiers

Usage

npm i babel-plugin-inline-replace-variables --save-dev

configure in .babelrc(should transfer to json format) or any babel queries:

{
  plugins: [
    ['inline-replace-variables', {
      "__SERVER__": true,
      "__VERSION__": "v1.2.3"
    }]
  ]
}

EFFECT:

if (__SERVER__) {
  console.log('this is server, version: %s', __VERSION__)
} else {
  alert('this is browser')
}

will be transformed to

if (true) {
  console.log('this is server, version: %s', "v1.2.3")
} else {
  alert('this is browser')
}

Support Replace With Expression:

{
  plugins: [
    ['inline-replace-variables', {
      "__TYPE__": {
        type: 'node',
        replacement: 'process.env.NODE_ENV'
      }
    }]
  ]
}

EFFECT

if (__TYPE__) {
  // code
}

to

if (process.env.NODE_ENV) {
  // code
}

Also support babel AST Node

const t = require('babel-types');
const nodeEnv = t.memberExpression(t.memberExpression(t.identifier('process'), t.identifier('env')), t.identifier('NODE_ENV'));

{
  plugins: [
    ['inline-replace-variables', {
      "__TYPE__": nodeEnv
    }]
  ]
}

EFFECT

The effect is the same above.

tip:

Version 1.0.1 fix the misspelling of 'varibles' to 'variables'

so babel-plugin-inline-replace-varibles is deprecated, you should instead it of babel-plugin-inline-replace-variables

Authors: https://github.com/wssgcg1213, https://github.com/rtsao

babel-plugin-inline-replace-variables's People

Contributors

rtsao avatar wssgcg1213 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.