Security News
JavaScript Leaders Demand Oracle Release the JavaScript Trademark
In an open letter, JavaScript community leaders urge Oracle to give up the JavaScript trademark, arguing that it has been effectively abandoned through nonuse.
babel-loader
Advanced tools
The babel-loader npm package is a Webpack plugin that integrates Babel with Webpack. Babel is a JavaScript compiler that allows you to use next-generation JavaScript, today. It compiles ES6 and beyond into ES5 code that can run in current browser environments. babel-loader enables Webpack to process and bundle JavaScript files using Babel.
Transpiling ES6+ to ES5
This feature allows developers to write modern JavaScript code without worrying about compatibility issues with older browsers. The code sample shows a Webpack configuration that uses babel-loader to process files ending in .js, excluding node_modules, and transpiles them using the preset '@babel/preset-env'.
module.exports = {
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
}
]
}
};
Using Babel plugins
babel-loader can be configured to use specific Babel plugins to enable experimental features or custom transformations. The code sample demonstrates how to include the '@babel/plugin-proposal-class-properties' plugin to allow the use of class properties syntax in JavaScript.
module.exports = {
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
plugins: ['@babel/plugin-proposal-class-properties']
}
}
}
]
}
};
Source Maps support
babel-loader supports source maps, which help in debugging transpiled code by mapping the transformed code back to the original source code. The code sample shows how to enable source maps in the babel-loader options.
module.exports = {
module: {
rules: [
{
test: /\.js$/,
use: {
loader: 'babel-loader',
options: {
sourceMaps: true
}
}
}
]
}
};
ts-loader is a Webpack loader for TypeScript, similar to how babel-loader works for JavaScript. It compiles TypeScript code to JavaScript, allowing developers to use TypeScript's static typing features. While babel-loader focuses on JavaScript and its newer syntax, ts-loader is specifically designed for TypeScript.
awesome-typescript-loader is another Webpack loader for TypeScript, providing similar functionality to ts-loader. It also compiles TypeScript to JavaScript and offers features like Babel integration and type checking. It is known for being fast and providing a smoother integration with Babel compared to ts-loader.
esbuild-loader is a Webpack loader that uses the esbuild bundler. It is designed for extremely fast bundling and minifying of JavaScript and TypeScript code. Unlike babel-loader, which focuses on compatibility and syntax transformations, esbuild-loader emphasizes performance and speed.
Babel is a compiler for writing next generation JavaScript.
This package allows the use babel with webpack
Notes: Issues with the output should be reported on the babel issue tracker;
npm install babel-loader --save-dev
Note: npm will deprecate peerDependencies on the next major release, so required dependencies like babel-core and webpack will have to be installed manually.
Within your webpack configuration object, you'll need to add the babel-loader to the list of modules, like so:
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel'
}
]
}
See the babel
options.
You can pass options to the loader by writting them as a query string:
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel?optional[]=runtime&stage=0'
}
]
}
or by using the query property:
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel',
query: {
optional: ['runtime'],
stage: 0
}
}
]
}
This loader also supports the following loader-specific option:
cacheDirectory
: When set, the given directory will be used to cache the results of the loader. Future webpack builds will attempt to read from the cache to avoid needing to run the potentially expensive Babel recompilation process on each run. A value of true
will cause the loader to use the default OS temporary file directory.
cacheIdentifier
: When set, it will add the given identifier to the cached files. This can be used to force cache busting if the identifier changes. By default the identifier is made by using the babel-core's version and the babel-loader's version.
Note: The sourceMap
option is ignored, instead sourceMaps are automatically enabled when webpack is configured to use them (via the devtool
config option).
Make sure you are transforming as few files as possible. Because you are probably
matching /\.js$/
, you might be transforming the node_modules
folder or other unwanted
source.
See the exclude
option in the loaders
config as documented above.
babel uses very small helpers for common functions such as _extend
. By default
this will be added to every file that requires it.
You can instead require the babel runtime as a separate module to avoid the duplication.
The following configuration disables automatic per-file runtime injection in babel, instead
requiring babel-runtime
and making all helper references use it.
See the docs for more information.
NOTE: You must run npm install babel-runtime --save
to include this in your project.
loaders: [
// the optional 'runtime' transformer tells babel to require the runtime
// instead of inlining it.
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel-loader?optional[]=runtime'
}
]
Since Babel includes a polyfill that includes a custom regenerator runtime and core.js, the following usual shimming method using webpack.ProvidePlugin
will not work:
// ...
new webpack.ProvidePlugin({
'Promise': 'bluebird'
}),
// ...
The following approach will not work either:
require('babel-runtime/core-js/promise').default = require('bluebird');
var promise = new Promise;
which outputs to (using runtime
):
'use strict';
var _Promise = require('babel-runtime/core-js/promise')['default'];
require('babel-runtime/core-js/promise')['default'] = require('bluebird');
var promise = new _Promise();
The previous Promise
library is referenced and used before it is overridden.
One approach is to have a "bootstrap" step in your application that would first override the default globals before your application:
// bootstrap.js
require('babel-runtime/core-js/promise').default = require('bluebird');
// ...
require('./app');
FAQs
babel module loader for webpack
The npm package babel-loader receives a total of 14,738,157 weekly downloads. As such, babel-loader popularity was classified as popular.
We found that babel-loader demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 5 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
In an open letter, JavaScript community leaders urge Oracle to give up the JavaScript trademark, arguing that it has been effectively abandoned through nonuse.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.