GithubHelp home page GithubHelp logo

trueadm / babel-standalone Goto Github PK

View Code? Open in Web Editor NEW

This project forked from babel/babel-standalone

1.0 3.0 1.0 79 KB

Standalone build of Babel for use in non-Node.js environments, including browsers. Similar to the (now deprecated) babel-browser.

License: MIT License

JavaScript 90.16% PowerShell 9.84%

babel-standalone's Introduction

babel-standalone

babel-standalone is a standalone build of Babel for use in non-Node.js environments, including browsers. It's bundled with all the standard Babel plugins and presets, and a build of babili (babel-minify) is optionally available too.

But why?!

It's true that using Babel through Webpack, Browserify or Gulp should be sufficient for most use cases. However, there are some valid use cases for babel-standalone:

  • Sites like JSFiddle, JS Bin, the REPL on the Babel site, etc. These sites compile user-provided JavaScript in real-time.
  • Apps that embed a JavaScript engine such as V8 directly, and want to use Babel for compilation
  • Apps that want to use JavaScript as a scripting language for extending the app itself, including all the goodies that ES2015 provides.
  • Integration of Babel into a non-Node.js environment (ReactJS.NET, ruby-babel-transpiler, php-babel-transpiler, etc).

Installation

There are several ways to get a copy of babel-standalone. Pick whichever one you like:

Usage

Load babel.js or babel.min.js in your environment. This will expose Babel's API in a Babel object:

var input = 'const getMessage = () => "Hello World";';
var output = Babel.transform(input, { presets: ['es2015'] }).code;

When loaded in a browser, babel-standalone will automatically compile and execute all script tags with type text/babel or text/jsx:

<div id="output"></div>
<!-- Load Babel -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.18.1/babel.min.js"></script>
<!-- Your custom script here -->
<script type="text/babel">
const getMessage = () => "Hello World";
document.getElementById('output').innerHTML = getMessage();
</script>

You can use the data-plugins and data-presets attributes to specify the Babel plugins/presets to use:

<script type="text/babel" data-presets="es2015,stage-2">

Loading external scripts via src attribute is supported too:

<script type="text/babel" src="foo.js"></script>

Note that .babelrc doesn't work in babel-standalone, as no file system access is available. The presets and/or plugins to use must be specified in the options passed to Babel.transform.

Customisation

Custom plugins and presets can be added using the registerPlugin and registerPreset methods respectively:

// Simple plugin that converts every identifier to "LOL"
function lolizer() {
  return {
    visitor: {
      Identifier(path) {
        path.node.name = 'LOL';
      }
    }
  }
}
Babel.registerPlugin('lolizer', lolizer);

Once registered, just use the name of the plugin:

var output = Babel.transform(
  'function helloWorld() { alert(hello); }',
  {plugins: ['lolizer']}
);
// Returns "function LOL() { LOL(LOL); }"

Custom plugins also work for inline <script>s:

<script type="text/babel" data-plugins="lolizer">

babel-standalone's People

Contributors

cansin avatar danbuild avatar daniel15 avatar greenkeeperio-bot avatar hzoo avatar miclaus avatar mweststrate avatar ngyikp avatar ssergiienko avatar taion avatar tpetricek avatar trueadm avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

ykankaya

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.