• Stars
    star
    1,680
  • Rank 27,795 (Top 0.6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 10 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Browserify transform for Babel

babelify Build Status

Babel browserify transform.

As of Babel 6.0.0 there are no plugins included by default. For babelify to be useful, you must also include some presets and/or plugins.

Installation

# Babel 7
$ npm install --save-dev babelify @babel/core

# Babel 6
$ npm install --save-dev babelify@8 babel-core

Usage

CLI

$ browserify script.js -o bundle.js -t [ babelify --presets [ @babel/preset-env @babel/preset-react ] --plugins [ @babel/plugin-transform-class-properties ] ]

Node

var fs = require("fs");
var browserify = require("browserify");
browserify("./script.js")
  .transform("babelify", {presets: ["@babel/preset-env", "@babel/preset-react"]})
  .bundle()
  .pipe(fs.createWriteStream("bundle.js"));

NOTE: Presets and plugins need to be installed as separate modules. For the above examples to work, you'd need to also install @babel/preset-env and @babel/preset-react:

$ npm install --save-dev @babel/preset-env @babel/preset-react

Options

Selected options are discussed below. See the babel docs for the complete list of options.

Options may be passed in via standard browserify ways:

$ browserify -t [ babelify --presets [ @babel/preset-env @babel/preset-react ] ]
browserify().transform("babelify", {presets: ["@babel/preset-env", "@babel/preset-react"]});
var babelify = require("babelify");
browserify().transform(babelify, {presets: ["@babel/preset-env", "@babel/preset-react"]});

Or, with the configure method:

browserify().transform(babelify.configure({
  presets: ["@babel/preset-env", "@babel/preset-react"]
}));

Customizing extensions

By default, all files with the extensions .js, .es, .es6 and .jsx are compiled. You can change this by passing an array of extensions.

NOTE: This will override the default ones so if you want to use any of them you have to add them back.

browserify().transform("babelify", {extensions: [".babel"]});
$ browserify -t [ babelify --extensions .babel ]

Now you can use:

import NavBar from "nav-bar.babel";
var Panels = require("panels.babel");

NOTE: By default, Browserify will only lookup .js and .json files when the extension is omitted (like node's require). To lookup additional extensions, use browserify's extensions option.

browserify({
  extensions: [".babel"]
}).transform("babelify", {
  extensions: [".babel"]
});
$ browserify --extensions=.babel -t [ babelify --extensions .babel ]

Now you can omit the extension and compile .babel files:

import NavBar from "nav-bar";
var Panels = require("panels");

Source maps

By default, browserify sets the source map sources paths relative to the basedir (or to process.cwd() if not set). To make the sources paths absolute, set the sourceMapsAbsolute option on babelify:

browserify().transform("babelify", {
  sourceMapsAbsolute: true
});
$ browserify -t [ babelify --sourceMapsAbsolute ]

Additional options

browserify().transform(babelify.configure({
  // Optional ignore regex - if any filenames **do** match this regex then
  // they aren't compiled
  ignore: /regex/,

  // Optional only regex - if any filenames **don't** match this regex
  // then they aren't compiled
  only: /my_es6_folder/
}))
$ browserify -t [ babelify --ignore regex --only my_es6_folder ]

Babel result (metadata and others)

Babelify emits a babelify event with Babel's full result object as the first argument, and the filename as the second. Browserify doesn't pass-through the events emitted by a transform, so it's necessary to get a reference to the transform instance before you can attach a listener for the event:

var b = browserify().transform(babelify);

b.on("transform", function(tr) {
  if (tr instanceof babelify) {
    tr.once("babelify", function(result, filename) {
      result; // => { code, map, ast, metadata }
    });
  }
});

FAQ

Why aren't files in node_modules being transformed?

This is the default browserify behavior.

A possible solution is to add:

{
  "browserify": {
    "transform": ["babelify"]
  }
}

to the root of all your modules package.json that you want to be transformed. If you'd like to specify options then you can use:

{
  "browserify": {
    "transform": [["babelify", { "presets": ["@babel/preset-env"] }]]
  }
}

Another solution (proceed with caution!) is to run babelify as a global transform. Use the babel ignore option to narrow the number of files transformed:

browserify().transform("babelify", {
  global: true,
  ignore: /\/node_modules\/(?!app\/)/
});

The above example will result in a transform that also includes the app module in node_modules: the global flag transform all files, and the ignore regular expression then excludes all those in the node_modules directory except those that are in node_modules/app (since ?! will match if the given suffix is absent).

Why am I not getting source maps?

To use source maps, enable them in browserify with the debug option:

browserify({debug: true}).transform("babelify");
$ browserify -d -t [ babelify ]

If you want the source maps to be of the post-transpiled code, then leave debug on, but turn off babelify's sourceMaps:

browserify({debug: true}).transform("babelify", {sourceMaps: false});
$ browserify -d -t [ babelify --no-sourceMaps ]

More Repositories

1

babel

🐠 Babel is a compiler for writing next generation JavaScript.
TypeScript
43,141
star
2

babel-loader

đŸ“Ļ Babel loader for webpack
JavaScript
4,824
star
3

minify

✂ī¸ An ES6+ aware minifier based on the Babel toolchain (beta)
JavaScript
4,391
star
4

babel-preset-env

PSA: this repo has been moved into babel/babel -->
JavaScript
3,500
star
5

babel-sublime

Syntax definitions for ES6 JavaScript with React JSX extensions.
JavaScript
3,256
star
6

babel-eslint

đŸ—ŧ A wrapper for Babel's parser used for ESLint (renamed to @babel/eslint-parser)
JavaScript
2,963
star
7

example-node-server

Example Node Server w/ Babel
JavaScript
2,844
star
8

babylon

PSA: moved into babel/babel as @babel/parser -->
JavaScript
1,714
star
9

gulp-babel

Gulp plugin for Babel
JavaScript
1,320
star
10

babel-upgrade

âŦ†ī¸ A tool for upgrading Babel versions (to v7): `npx babel-upgrade`
JavaScript
1,309
star
11

awesome-babel

😎A list of awesome Babel plugins, presets, etc.
864
star
12

babel-standalone

🎮 Now located in the Babel repo! Standalone build of Babel for use in non-Node.js environments, including browsers.
JavaScript
819
star
13

website

🌐 The Babel documentation website
TypeScript
749
star
14

preset-modules

A Babel preset that targets modern browsers by fixing engine bugs (will be merged into preset-env eventually)
JavaScript
739
star
15

kneden

Transpile ES2017 async/await to vanilla ES6 Promise chains: a Babel plugin
JavaScript
514
star
16

grunt-babel

Grunt plugin for Babel
JavaScript
437
star
17

proposals

✍ī¸ Tracking the status of Babel's implementation of TC39 proposals (may be out of date)
432
star
18

eslint-plugin-babel

An ESlint rule plugin companion to babel-eslint
JavaScript
389
star
19

generator-babel-boilerplate

A Yeoman generator to author libraries in ES2015 (and beyond!) for Node and the browser.
JavaScript
381
star
20

babel-time-travel

Time travel through babel transformations one by one (implemented in the Babel REPL now)
JavaScript
344
star
21

babel-polyfills

A set of Babel plugins that enable injecting different polyfills with different strategies in your compiled code.
TypeScript
330
star
22

babel-sublime-snippets

Next generation JavaScript and React snippets for Sublime
263
star
23

karma-babel-preprocessor

Karma plugin for Babel
JavaScript
167
star
24

ruby-babel-transpiler

Ruby Babel is a bridge to the JS Babel transpiler.
Ruby
159
star
25

babel-jest

Jest plugin for Babel (moved) ->
138
star
26

notes

â™Ŧ Notes from the @Babel Team (discuss in PRs)
122
star
27

generator-babel-plugin

Babel Plugin generator for Yeoman
JavaScript
118
star
28

babel-bridge

A placeholder package that bridges babel-core to @babel/core.
JavaScript
94
star
29

babel-bot

🤖 A helpful bot to automate common tasks on Babel Issues/PRs
JavaScript
75
star
30

babel-brunch

Brunch plugin for Babel
JavaScript
68
star
31

broccoli-babel-transpiler

Broccoli plugin for Babel
JavaScript
57
star
32

jade-babel

Jade plugin for Babel
JavaScript
40
star
33

sandboxes

Babel repl-like codesandbox: check out link =>
JavaScript
39
star
34

jekyll-babel

A Babel converter for Jekyll.
Ruby
34
star
35

acorn-to-esprima

(unmaintained) Converts acorn tokens to esprima tokens
JavaScript
33
star
36

babel-connect

Connect plugin for Babel
JavaScript
27
star
37

podcast.babeljs.io

The Babel Podcast site!
JavaScript
25
star
38

phabricator-to-github

A tool to migrate phabricator issues to github
JavaScript
24
star
39

actions

Babel specific github actions 🤖
JavaScript
24
star
40

rfcs

RFCs for changes to Babel
21
star
41

metalsmith-babel

A Metalsmith plugin to compile JavaScript with Babel
JavaScript
20
star
42

babel-configuration-examples

WIP
JavaScript
18
star
43

logo

Babel logo
15
star
44

duo-babel

Duo plugin for Babel
JavaScript
15
star
45

parser_performance

JavaScript
13
star
46

eslint-config-babel

ESLint config for all Babel repos
JavaScript
10
star
47

gobble-babel

Gobble plugin for Babel
JavaScript
9
star
48

babel-standalone-bower

Bower build of babel-standalone. See https://github.com/Daniel15/babel-standalone
JavaScript
9
star
49

eslint-plugin-babel-plugin

A set of eslint rules to enforce best practices in the development of Babel plugins.
JavaScript
8
star
50

babel-plugin-proposal-private-property-in-object

@babel/plugin-proposal-private-property-in-object, with an added warning for when depending on this package without explicitly listing it in dependencies or devDependencies
JavaScript
8
star
51

babel-archive

🗑 Packages that are deprecated or don't need to be updated
JavaScript
7
star
52

.github

Community health files for the Babel organization
7
star
53

flavortown

what else?
6
star
54

babel-test262-runner

Run test262 tests on Node 0.10 using Babel 7 and `core-js@3`.
JavaScript
6
star
55

babel-plugin-transform-async-functions

https://github.com/MatAtBread/fast-async/issues/46
4
star
56

phabricator-redirects

↩ī¸ Redirects for old phabricator urls
HTML
2
star
57

slack-invite-link

Source of slack.babeljs.io
1
star