Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Babelify is a browserify transform for Babel, which allows you to use Babel to transpile your JavaScript files when bundling them with Browserify. This enables you to use the latest JavaScript features and syntax in your code, and have them transpiled to a version of JavaScript that is compatible with older browsers.
Transpile ES6 to ES5
This feature allows you to transpile modern JavaScript (ES6) to ES5, making it compatible with older browsers. The code sample demonstrates how to use Babelify with Browserify to transform a source file using the '@babel/preset-env' preset.
const browserify = require('browserify');
const babelify = require('babelify');
browserify('./src/app.js')
.transform(babelify, { presets: ['@babel/preset-env'] })
.bundle()
.pipe(process.stdout);
Use JSX with React
This feature allows you to use JSX syntax with React. The code sample shows how to configure Babelify to transform a JSX file using the '@babel/preset-react' preset.
const browserify = require('browserify');
const babelify = require('babelify');
browserify('./src/app.jsx')
.transform(babelify, { presets: ['@babel/preset-react'] })
.bundle()
.pipe(process.stdout);
Custom Babel Plugins
This feature allows you to use custom Babel plugins. The code sample demonstrates how to use the '@babel/plugin-transform-arrow-functions' plugin to transform arrow functions in your code.
const browserify = require('browserify');
const babelify = require('babelify');
browserify('./src/app.js')
.transform(babelify, { plugins: ['@babel/plugin-transform-arrow-functions'] })
.bundle()
.pipe(process.stdout);
Babel-loader is a webpack loader that allows you to transpile JavaScript files using Babel and webpack. It is similar to Babelify in that it uses Babel to transpile code, but it is designed to work with webpack instead of Browserify.
Rollup-plugin-babel is a Rollup plugin that allows you to use Babel to transpile your JavaScript files when bundling them with Rollup. It provides similar functionality to Babelify but is designed to work with Rollup instead of Browserify.
Gulp-babel is a Gulp plugin that allows you to use Babel to transpile your JavaScript files in a Gulp build pipeline. It offers similar functionality to Babelify but is intended for use with Gulp instead of Browserify.
Babel browserify transform
$ npm install --save-dev babelify
$ browserify script.js -t babelify --outfile bundle.js
var fs = require("fs");
var browserify = require("browserify");
var babelify = require("babelify");
browserify("./script.js", { debug: true })
.transform(babelify)
.bundle()
.on("error", function (err) { console.log("Error : " + err.message); })
.pipe(fs.createWriteStream("bundle.js"));
Selected options are discussed below. See the babel docs for the complete list.
browserify().transform(babelify.configure({
blacklist: ["regenerator"]
}))
$ browserify -d -e script.js -t [ babelify --blacklist regenerator ]
By default Babel's experimental transforms
are disabled. You can turn them on individually by passing optional
as a configuration option.
browserify().transform(babelify.configure({
optional: ["es7.asyncFunctions"]
}))
$ browserify -d -e script.js -t [ babelify --optional es7.asyncFunctions ]
Alternatively, you can enable an entire TC39 category of experimental ES7 features via the stage
configuration option.
browserify().transform(babelify.configure({
stage: 0
}))
$ browserify -d -e script.js -t [ babelify --stage 0 ]
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.configure({
extensions: [".babel"]
}))
$ browserify -d -e script.js -t [ babelify --extensions .babel ]
NOTE: Keep in mind that to get browserify to find files with extensions it doesn't include by default, you may also need to configure them there. For example, to have require('./script')
in a browserified file resolve to a ./script.babel
file, you'd need to configure browserify to also look for the .babel
extension. See the extensions
option documentation.
Browserify passes an absolute path so there's no way to determine what folder
it's relative to. You can pass a relative path that'll be removed from the
absolute path with the sourceMapRelative
option.
browserify().transform(babelify.configure({
sourceMapRelative: "/Users/sebastian/Projects/my-cool-website/assets"
}))
$ browserify -d -e script.js -t [ babelify --sourceMapRelative . ]
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 -d -e script.js -t [ babelify --ignore regex --only my_es6_folder ]
As a convenience, the babelify polyfill is exposed in babelify. If you've got a browserify-only package this may alleviate the necessity to have both babel & babelify installed.
// In browser code
require("babelify/polyfill");
node_modules
being transformed?This is default browserify behaviour and can not be overriden. 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", { "optional": ["es7.asyncFunctions"] }]]
}
}
FAQs
Babel browserify transform
The npm package babelify receives a total of 240,172 weekly downloads. As such, babelify popularity was classified as popular.
We found that babelify demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.