1 | babel-standalone
|
2 | ================
|
3 |
|
4 | 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)](http://dl.vc/babili-standalone) is optionally available too.
|
5 |
|
6 | But why?!
|
7 | =========
|
8 |
|
9 | 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:
|
10 |
|
11 | - Sites like [JSFiddle](https://jsfiddle.net/), [JS Bin](https://jsbin.com/), the [REPL on the Babel site](http://babeljs.io/repl/), etc. These sites compile user-provided JavaScript in real-time.
|
12 | - Apps that embed a JavaScript engine such as V8 directly, and want to use Babel for compilation
|
13 | - Apps that want to use JavaScript as a scripting language for extending the app itself, including all the goodies that ES2015 provides.
|
14 | - Integration of Babel into a non-Node.js environment ([ReactJS.NET](http://reactjs.net/), [ruby-babel-transpiler](https://github.com/babel/ruby-babel-transpiler), [php-babel-transpiler](https://github.com/talyssonoc/php-babel-transpiler), etc).
|
15 |
|
16 | Installation
|
17 | ============
|
18 |
|
19 | There are several ways to get a copy of babel-standalone. Pick whichever one you like:
|
20 |
|
21 | - Use it via UNPKG: https://unpkg.com/babel-standalone@6/babel.min.js. This is a simple way to embed it on a webpage without having to do any other setup.
|
22 | - Install via Bower: `bower install babel-standalone`
|
23 | - Install via NPM: `npm install --save babel-standalone`
|
24 | - Manually grab `babel.js` and/or `babel.min.js` from the [GitHub releases page](https://github.com/Daniel15/babel-standalone/releases). Every release includes these files.
|
25 | - Install it via Git: You can use the repo at https://github.com/Daniel15/babel-standalone-bower to pull a prebuilt version from Git. Note that this is generally only advised for systems that *must* pull artifacts from Git, such as Bower.
|
26 |
|
27 | Usage
|
28 | =====
|
29 |
|
30 | Load `babel.js` or `babel.min.js` in your environment. This will expose [Babel's API](http://babeljs.io/docs/usage/api/) in a `Babel` object:
|
31 |
|
32 | ```js
|
33 | var input = 'const getMessage = () => "Hello World";';
|
34 | var output = Babel.transform(input, { presets: ['es2015'] }).code;
|
35 | ```
|
36 |
|
37 | When loaded in a browser, babel-standalone will automatically compile and execute all script tags with type `text/babel` or `text/jsx`:
|
38 | ```html
|
39 | <div id="output"></div>
|
40 | <!-- Load Babel -->
|
41 | <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
|
42 | <!-- Your custom script here -->
|
43 | <script type="text/babel">
|
44 | const getMessage = () => "Hello World";
|
45 | document.getElementById('output').innerHTML = getMessage();
|
46 | </script>
|
47 | ```
|
48 |
|
49 | You can use the `data-plugins` and `data-presets` attributes to specify the Babel plugins/presets to use:
|
50 | ```html
|
51 | <script type="text/babel" data-presets="es2015,stage-2">
|
52 | ```
|
53 |
|
54 | Loading external scripts via `src` attribute is supported too:
|
55 | ```html
|
56 | <script type="text/babel" src="foo.js"></script>
|
57 | ```
|
58 |
|
59 | 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`.
|
60 |
|
61 | Customisation
|
62 | =============
|
63 | Custom plugins and presets can be added using the `registerPlugin` and `registerPreset` methods respectively:
|
64 |
|
65 | ```js
|
66 | // Simple plugin that converts every identifier to "LOL"
|
67 | function lolizer() {
|
68 | return {
|
69 | visitor: {
|
70 | Identifier(path) {
|
71 | path.node.name = 'LOL';
|
72 | }
|
73 | }
|
74 | }
|
75 | }
|
76 | Babel.registerPlugin('lolizer', lolizer);
|
77 | ```
|
78 |
|
79 | Once registered, just use the name of the plugin:
|
80 |
|
81 | ```js
|
82 | var output = Babel.transform(
|
83 | 'function helloWorld() { alert(hello); }',
|
84 | {plugins: ['lolizer']}
|
85 | );
|
86 | // Returns "function LOL() { LOL(LOL); }"
|
87 | ```
|
88 |
|
89 | Custom plugins also work for inline `<script>`s:
|
90 |
|
91 | ```html
|
92 | <script type="text/babel" data-plugins="lolizer">
|
93 | ```
|
94 |
|
95 | Manually Building
|
96 | =================
|
97 |
|
98 | If you want to manually upgrade the Babel version used by babel-standalone (or build your own release), follow these steps:
|
99 |
|
100 | 1. Upgrade the Babel versions in `package.json`. This can be done with `npm-check-upgrades` (eg. `npm-check-updates -u -a --packageFile ./package.json /^babel\-/`)
|
101 | 2. Delete `node_modules`, as npm often produces inefficient directory layouts if you upgrade in-place
|
102 | 3. Run `npm install && npm run build`
|
103 | 4. Run `npm run test` to ensure it works
|
104 | 5. Open `examples/example.htm` and ensure it works
|