\begin{quote}
This README is for babel-\/loader v8 + Babel v7 Check the \href{https://github.com/babel/babel-loader/tree/7.x}{\texttt{ 7.\+x branch}} for docs with Babel v6 \end{quote}
\href{https://www.npmjs.com/package/babel-loader}{\texttt{ }} \href{https://travis-ci.org/babel/babel-loader}{\texttt{ }} \href{https://ci.appveyor.com/project/babel/babel-loader/branch/master}{\texttt{ }} \href{https://codecov.io/gh/babel/babel-loader}{\texttt{ }}

 \href{https://github.com/babel/babel}{\texttt{  }} \href{https://github.com/webpack/webpack}{\texttt{  }} 

\doxysection*{Babel Loader}

This package allows transpiling Java\+Script files using \href{https://github.com/babel/babel}{\texttt{ Babel}} and \href{https://github.com/webpack/webpack}{\texttt{ webpack}}.

{\bfseries{Note}}\+: Issues with the output should be reported on the Babel \href{https://github.com/babel/babel/issues}{\texttt{ Issues}} tracker.

\doxysubsection*{Install}

\begin{quote}
webpack 4.\+x $\vert$ babel-\/loader 8.\+x $\vert$ babel 7.\+x \end{quote}

\begin{DoxyCode}{0}
\DoxyCodeLine{npm install -\/D babel-\/loader @babel/core @babel/preset-\/env webpack }

\end{DoxyCode}


\doxysubsection*{Usage}

webpack documentation\+: \href{https://webpack.js.org/loaders/}{\texttt{ Loaders}}

Within your webpack configuration object, you\textquotesingle{}ll need to add the babel-\/loader to the list of modules, like so\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{module: \{}
\DoxyCodeLine{  rules: [}
\DoxyCodeLine{    \{}
\DoxyCodeLine{      test: /\(\backslash\).m?js\$/,}
\DoxyCodeLine{      exclude: /(node\_modules|bower\_components)/,}
\DoxyCodeLine{      use: \{}
\DoxyCodeLine{        loader: 'babel-\/loader',}
\DoxyCodeLine{        options: \{}
\DoxyCodeLine{          presets: ['@babel/preset-\/env']}
\DoxyCodeLine{        \}}
\DoxyCodeLine{      \}}
\DoxyCodeLine{    \}}
\DoxyCodeLine{  ]}
\DoxyCodeLine{\} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_babel_loader_README_autotoc_md3758}{}\doxysubsubsection{Options}\label{md_sfcode_node_modules_babel_loader_README_autotoc_md3758}
See the {\ttfamily babel} \href{https://babeljs.io/docs/en/options}{\texttt{ options}}.

You can pass options to the loader by using the \href{https://webpack.js.org/configuration/module/\#rule-options-rule-query}{\texttt{ {\ttfamily options}}} property\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{module: \{}
\DoxyCodeLine{  rules: [}
\DoxyCodeLine{    \{}
\DoxyCodeLine{      test: /\(\backslash\).m?js\$/,}
\DoxyCodeLine{      exclude: /(node\_modules|bower\_components)/,}
\DoxyCodeLine{      use: \{}
\DoxyCodeLine{        loader: 'babel-\/loader',}
\DoxyCodeLine{        options: \{}
\DoxyCodeLine{          presets: ['@babel/preset-\/env'],}
\DoxyCodeLine{          plugins: ['@babel/plugin-\/proposal-\/object-\/rest-\/spread']}
\DoxyCodeLine{        \}}
\DoxyCodeLine{      \}}
\DoxyCodeLine{    \}}
\DoxyCodeLine{  ]}
\DoxyCodeLine{\} }

\end{DoxyCode}


This loader also supports the following loader-\/specific option\+:


\begin{DoxyItemize}
\item {\ttfamily cache\+Directory}\+: Default {\ttfamily false}. 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. If the value is set to {\ttfamily true} in options ({\ttfamily \{cache\+Directory\+: true\}}), the loader will use the default cache directory in {\ttfamily node\+\_\+modules/.cache/babel-\/loader} or fallback to the default OS temporary file directory if no {\ttfamily node\+\_\+modules} folder could be found in any root directory.
\item {\ttfamily cache\+Identifier}\+: Default is a string composed by the {\ttfamily @babel/core}\textquotesingle{}s version, the {\ttfamily babel-\/loader}\textquotesingle{}s version, the contents of {\ttfamily .babelrc} file if it exists, and the value of the environment variable {\ttfamily BABEL\+\_\+\+ENV} with a fallback to the {\ttfamily NODE\+\_\+\+ENV} environment variable. This can be set to a custom value to force cache busting if the identifier changes.
\item {\ttfamily cache\+Compression}\+: Default {\ttfamily true}. When set, each Babel transform output will be compressed with Gzip. If you want to opt-\/out of cache compression, set it to {\ttfamily false} -- your project may benefit from this if it transpiles thousands of files.
\item {\ttfamily customize}\+: Default {\ttfamily null}. The path of a module that exports a {\ttfamily custom} callback \href{\#customized-loader}{\texttt{ like the one that you\textquotesingle{}d pass to {\ttfamily .custom()}}}. Since you already have to make a new file to use this, it is recommended that you instead use {\ttfamily .custom} to create a wrapper loader. Only use this if you {\itshape must} continue using {\ttfamily babel-\/loader} directly, but still want to customize.
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_babel_loader_README_autotoc_md3759}{}\doxysubsection{Troubleshooting}\label{md_sfcode_node_modules_babel_loader_README_autotoc_md3759}
\hypertarget{md_sfcode_node_modules_babel_loader_README_autotoc_md3760}{}\doxysubsubsection{babel-\/loader is slow!}\label{md_sfcode_node_modules_babel_loader_README_autotoc_md3760}
Make sure you are transforming as few files as possible. Because you are probably matching {\ttfamily /\textbackslash{}.m?js\$/}, you might be transforming the {\ttfamily node\+\_\+modules} folder or other unwanted source.

To exclude {\ttfamily node\+\_\+modules}, see the {\ttfamily exclude} option in the {\ttfamily loaders} config as documented above.

You can also speed up babel-\/loader by as much as 2x by using the {\ttfamily cache\+Directory} option. This will cache transformations to the filesystem.\hypertarget{md_sfcode_node_modules_babel_loader_README_autotoc_md3761}{}\doxysubsubsection{Babel is injecting helpers into each file and bloating my code!}\label{md_sfcode_node_modules_babel_loader_README_autotoc_md3761}
Babel uses very small helpers for common functions such as {\ttfamily \+\_\+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, requiring {\ttfamily @babel/plugin-\/transform-\/runtime} instead and making all helper references use it.

See the \href{https://babeljs.io/docs/plugins/transform-runtime/}{\texttt{ docs}} for more information.

{\bfseries{NOTE}}\+: You must run {\ttfamily npm install -\/D @babel/plugin-\/transform-\/runtime} to include this in your project and {\ttfamily @babel/runtime} itself as a dependency with {\ttfamily npm install @babel/runtime}.


\begin{DoxyCode}{0}
\DoxyCodeLine{rules: [}
\DoxyCodeLine{  // the 'transform-\/runtime' plugin tells Babel to}
\DoxyCodeLine{  // require the runtime instead of inlining it.}
\DoxyCodeLine{  \{}
\DoxyCodeLine{    test: /\(\backslash\).m?js\$/,}
\DoxyCodeLine{    exclude: /(node\_modules|bower\_components)/,}
\DoxyCodeLine{    use: \{}
\DoxyCodeLine{      loader: 'babel-\/loader',}
\DoxyCodeLine{      options: \{}
\DoxyCodeLine{        presets: ['@babel/preset-\/env'],}
\DoxyCodeLine{        plugins: ['@babel/plugin-\/transform-\/runtime']}
\DoxyCodeLine{      \}}
\DoxyCodeLine{    \}}
\DoxyCodeLine{  \}}
\DoxyCodeLine{] }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_babel_loader_README_autotoc_md3762}{}\doxyparagraph{$<$strong$>$\+NOTE$<$/strong$>$\+: transform-\/runtime \& custom polyfills (e.\+g. Promise library)}\label{md_sfcode_node_modules_babel_loader_README_autotoc_md3762}
Since \href{https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-runtime}{\texttt{ @babel/plugin-\/transform-\/runtime}} includes a polyfill that includes a custom \href{https://github.com/facebook/regenerator/blob/master/packages/regenerator-runtime/runtime.js}{\texttt{ regenerator-\/runtime}} and \href{https://github.com/zloirock/core-js}{\texttt{ core-\/js}}, the following usual shimming method using {\ttfamily webpack.\+Provide\+Plugin} will not work\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{// ...}
\DoxyCodeLine{        new webpack.ProvidePlugin(\{}
\DoxyCodeLine{            'Promise': 'bluebird'}
\DoxyCodeLine{        \}),}
\DoxyCodeLine{// ... }

\end{DoxyCode}


The following approach will not work either\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{require('@babel/runtime/core-\/js/promise').default = require('bluebird');}
\DoxyCodeLine{}
\DoxyCodeLine{var promise = new Promise; }

\end{DoxyCode}


which outputs to (using {\ttfamily runtime})\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{'use strict';}
\DoxyCodeLine{}
\DoxyCodeLine{var \_Promise = require('@babel/runtime/core-\/js/promise')['default'];}
\DoxyCodeLine{}
\DoxyCodeLine{require('@babel/runtime/core-\/js/promise')['default'] = require('bluebird');}
\DoxyCodeLine{}
\DoxyCodeLine{var promise = new \_Promise(); }

\end{DoxyCode}


The previous {\ttfamily Promise} library is referenced and used before it is overridden.

One approach is to have a \char`\"{}bootstrap\char`\"{} step in your application that would first override the default globals before your application\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{// bootstrap.js}
\DoxyCodeLine{}
\DoxyCodeLine{require('@babel/runtime/core-\/js/promise').default = require('bluebird');}
\DoxyCodeLine{}
\DoxyCodeLine{// ...}
\DoxyCodeLine{}
\DoxyCodeLine{require('./app'); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_babel_loader_README_autotoc_md3763}{}\doxysubsubsection{The Node.\+js API for $<$tt$>$babel$<$/tt$>$ has been moved to $<$tt$>$babel-\/core$<$/tt$>$.}\label{md_sfcode_node_modules_babel_loader_README_autotoc_md3763}
If you receive this message, it means that you have the npm package {\ttfamily babel} installed and are using the short notation of the loader in the webpack config (which is not valid anymore as of webpack 2.\+x)\+: 
\begin{DoxyCode}{0}
\DoxyCodeLine{\{}
\DoxyCodeLine{  test: /\(\backslash\).m?js\$/,}
\DoxyCodeLine{  loader: 'babel',}
\DoxyCodeLine{\} }

\end{DoxyCode}


webpack then tries to load the {\ttfamily babel} package instead of the {\ttfamily babel-\/loader}.

To fix this, you should uninstall the npm package {\ttfamily babel}, as it is deprecated in Babel v6. (Instead, install {\ttfamily @babel/cli} or {\ttfamily @babel/core}.) In the case one of your dependencies is installing {\ttfamily babel} and you cannot uninstall it yourself, use the complete name of the loader in the webpack config\+: 
\begin{DoxyCode}{0}
\DoxyCodeLine{\{}
\DoxyCodeLine{  test: /\(\backslash\).m?js\$/,}
\DoxyCodeLine{  loader: 'babel-\/loader',}
\DoxyCodeLine{\} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_babel_loader_README_autotoc_md3764}{}\doxysubsection{Customize config based on webpack target}\label{md_sfcode_node_modules_babel_loader_README_autotoc_md3764}
Webpack supports bundling multiple \href{https://webpack.js.org/concepts/targets/}{\texttt{ targets}}. For cases where you may want different Babel configurations for each target (like {\ttfamily web} {\itshape and} {\ttfamily node}), this loader provides a {\ttfamily target} property via Babel\textquotesingle{}s \href{https://babeljs.io/docs/en/config-files\#apicallercb}{\texttt{ caller}} API.

For example, to change the environment targets passed to {\ttfamily @babel/preset-\/env} based on the webpack target\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{// babel.config.js}
\DoxyCodeLine{}
\DoxyCodeLine{module.exports = api => \{}
\DoxyCodeLine{  return \{}
\DoxyCodeLine{    plugins: [}
\DoxyCodeLine{      "{}@babel/plugin-\/proposal-\/nullish-\/coalescing-\/operator"{},}
\DoxyCodeLine{      "{}@babel/plugin-\/proposal-\/optional-\/chaining"{}}
\DoxyCodeLine{    ],}
\DoxyCodeLine{    presets: [}
\DoxyCodeLine{      [}
\DoxyCodeLine{        "{}@babel/preset-\/env"{},}
\DoxyCodeLine{        \{}
\DoxyCodeLine{          useBuiltIns: "{}entry"{},}
\DoxyCodeLine{          // caller.target will be the same as the target option from webpack}
\DoxyCodeLine{          targets: api.caller(caller => caller \&\& caller.target === "{}node"{})}
\DoxyCodeLine{            ? \{ node: "{}current"{} \}}
\DoxyCodeLine{            : \{ chrome: "{}58"{}, ie: "{}11"{} \}}
\DoxyCodeLine{        \}}
\DoxyCodeLine{      ]}
\DoxyCodeLine{    ]}
\DoxyCodeLine{  \}}
\DoxyCodeLine{\} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_babel_loader_README_autotoc_md3765}{}\doxysubsection{Customized Loader}\label{md_sfcode_node_modules_babel_loader_README_autotoc_md3765}
{\ttfamily babel-\/loader} exposes a loader-\/builder utility that allows users to add custom handling of Babel\textquotesingle{}s configuration for each file that it processes.

{\ttfamily .custom} accepts a callback that will be called with the loader\textquotesingle{}s instance of {\ttfamily babel} so that tooling can ensure that it using exactly the same {\ttfamily @babel/core} instance as the loader itself.

In cases where you want to customize without actually having a file to call {\ttfamily .custom}, you may also pass the {\ttfamily customize} option with a string pointing at a file that exports your {\ttfamily custom} callback function.\hypertarget{md_sfcode_node_modules_babel_loader_README_autotoc_md3766}{}\doxysubsubsection{Example}\label{md_sfcode_node_modules_babel_loader_README_autotoc_md3766}

\begin{DoxyCode}{0}
\DoxyCodeLine{// Export from "{}./my-\/custom-\/loader.js"{} or whatever you want.}
\DoxyCodeLine{module.exports = require("{}babel-\/loader"{}).custom(babel => \{}
\DoxyCodeLine{  function myPlugin() \{}
\DoxyCodeLine{    return \{}
\DoxyCodeLine{      visitor: \{\},}
\DoxyCodeLine{    \};}
\DoxyCodeLine{  \}}
\DoxyCodeLine{}
\DoxyCodeLine{  return \{}
\DoxyCodeLine{    // Passed the loader options.}
\DoxyCodeLine{    customOptions(\{ opt1, opt2, ...loader \}) \{}
\DoxyCodeLine{      return \{}
\DoxyCodeLine{        // Pull out any custom options that the loader might have.}
\DoxyCodeLine{        custom: \{ opt1, opt2 \},}
\DoxyCodeLine{}
\DoxyCodeLine{        // Pass the options back with the two custom options removed.}
\DoxyCodeLine{        loader,}
\DoxyCodeLine{      \};}
\DoxyCodeLine{    \},}
\DoxyCodeLine{}
\DoxyCodeLine{    // Passed Babel's 'PartialConfig' object.}
\DoxyCodeLine{    config(cfg) \{}
\DoxyCodeLine{      if (cfg.hasFilesystemConfig()) \{}
\DoxyCodeLine{        // Use the normal config}
\DoxyCodeLine{        return cfg.options;}
\DoxyCodeLine{      \}}
\DoxyCodeLine{}
\DoxyCodeLine{      return \{}
\DoxyCodeLine{        ...cfg.options,}
\DoxyCodeLine{        plugins: [}
\DoxyCodeLine{          ...(cfg.options.plugins || []),}
\DoxyCodeLine{}
\DoxyCodeLine{          // Include a custom plugin in the options.}
\DoxyCodeLine{          myPlugin,}
\DoxyCodeLine{        ],}
\DoxyCodeLine{      \};}
\DoxyCodeLine{    \},}
\DoxyCodeLine{}
\DoxyCodeLine{    result(result) \{}
\DoxyCodeLine{      return \{}
\DoxyCodeLine{        ...result,}
\DoxyCodeLine{        code: result.code + "{}\(\backslash\)n// Generated by some custom loader"{},}
\DoxyCodeLine{      \};}
\DoxyCodeLine{    \},}
\DoxyCodeLine{  \};}
\DoxyCodeLine{\}); }

\end{DoxyCode}



\begin{DoxyCode}{0}
\DoxyCodeLine{// And in your Webpack config}
\DoxyCodeLine{module.exports = \{}
\DoxyCodeLine{  // ..}
\DoxyCodeLine{  module: \{}
\DoxyCodeLine{    rules: [\{}
\DoxyCodeLine{      // ...}
\DoxyCodeLine{      loader: path.join(\_\_dirname, 'my-\/custom-\/loader.js'),}
\DoxyCodeLine{      // ...}
\DoxyCodeLine{    \}]}
\DoxyCodeLine{  \}}
\DoxyCodeLine{\}; }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_babel_loader_README_autotoc_md3767}{}\doxysubsubsection{$<$tt$>$custom\+Options(options\+: Object)\+: \{ custom\+: Object, loader\+: Object \}$<$/tt$>$}\label{md_sfcode_node_modules_babel_loader_README_autotoc_md3767}
Given the loader\textquotesingle{}s options, split custom options out of {\ttfamily babel-\/loader}\textquotesingle{}s options.\hypertarget{md_sfcode_node_modules_babel_loader_README_autotoc_md3768}{}\doxysubsubsection{$<$tt$>$config(cfg\+: Partial\+Config)\+: Object$<$/tt$>$}\label{md_sfcode_node_modules_babel_loader_README_autotoc_md3768}
Given Babel\textquotesingle{}s {\ttfamily Partial\+Config} object, return the {\ttfamily options} object that should be passed to {\ttfamily babel.\+transform}.\hypertarget{md_sfcode_node_modules_babel_loader_README_autotoc_md3769}{}\doxysubsubsection{$<$tt$>$result(result\+: Result)\+: Result$<$/tt$>$}\label{md_sfcode_node_modules_babel_loader_README_autotoc_md3769}
Given Babel\textquotesingle{}s result object, allow loaders to make additional tweaks to it.\hypertarget{md_sfcode_node_modules_babel_loader_README_autotoc_md3770}{}\doxysubsection{License}\label{md_sfcode_node_modules_babel_loader_README_autotoc_md3770}
\href{https://couto.mit-license.org/}{\texttt{ MIT}} 