Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

pirms 3 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. > This README is for babel-loader v8 + Babel v7
  2. > Check the [7.x branch](https://github.com/babel/babel-loader/tree/7.x) for docs with Babel v6
  3. [![NPM Status](https://img.shields.io/npm/v/babel-loader.svg?style=flat)](https://www.npmjs.com/package/babel-loader)
  4. [![Build Status](https://travis-ci.org/babel/babel-loader.svg?branch=master)](https://travis-ci.org/babel/babel-loader)
  5. [![Build Status](https://ci.appveyor.com/api/projects/status/77y5mk6amwqt0q88/branch/master?svg=true)](https://ci.appveyor.com/project/babel/babel-loader/branch/master)
  6. [![codecov](https://codecov.io/gh/babel/babel-loader/branch/master/graph/badge.svg)](https://codecov.io/gh/babel/babel-loader)
  7. <div align="center">
  8. <a href="https://github.com/babel/babel">
  9. <img src="https://rawgit.com/babel/logo/master/babel.svg" alt="Babel logo" width="200" height="200">
  10. </a>
  11. <a href="https://github.com/webpack/webpack">
  12. <img src="https://webpack.js.org/assets/icon-square-big.svg" alt="webpack logo" width="200" height="200">
  13. </a>
  14. </div>
  15. <h1 align="center">Babel Loader</h1>
  16. This package allows transpiling JavaScript files using [Babel](https://github.com/babel/babel) and [webpack](https://github.com/webpack/webpack).
  17. **Note**: Issues with the output should be reported on the Babel [Issues](https://github.com/babel/babel/issues) tracker.
  18. <h2 align="center">Install</h2>
  19. > webpack 4.x | babel-loader 8.x | babel 7.x
  20. ```bash
  21. npm install -D babel-loader @babel/core @babel/preset-env webpack
  22. ```
  23. > webpack 4.x | babel-loader 7.x | babel 6.x
  24. ```bash
  25. npm install -D babel-loader@7 babel-core babel-preset-env webpack
  26. ```
  27. <h2 align="center">Usage</h2>
  28. webpack documentation: [Loaders](https://webpack.js.org/loaders/)
  29. Within your webpack configuration object, you'll need to add the babel-loader to the list of modules, like so:
  30. ```javascript
  31. module: {
  32. rules: [
  33. {
  34. test: /\.m?js$/,
  35. exclude: /(node_modules|bower_components)/,
  36. use: {
  37. loader: 'babel-loader',
  38. options: {
  39. presets: ['@babel/preset-env']
  40. }
  41. }
  42. }
  43. ]
  44. }
  45. ```
  46. ### Options
  47. See the `babel` [options](https://babeljs.io/docs/en/options).
  48. You can pass options to the loader by using the [`options`](https://webpack.js.org/configuration/module/#rule-options-rule-query) property:
  49. ```javascript
  50. module: {
  51. rules: [
  52. {
  53. test: /\.m?js$/,
  54. exclude: /(node_modules|bower_components)/,
  55. use: {
  56. loader: 'babel-loader',
  57. options: {
  58. presets: ['@babel/preset-env'],
  59. plugins: [require('@babel/plugin-proposal-object-rest-spread')]
  60. }
  61. }
  62. }
  63. ]
  64. }
  65. ```
  66. This loader also supports the following loader-specific option:
  67. * `cacheDirectory`: Default `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 blank (`loader: 'babel-loader?cacheDirectory'`) or `true` (`loader: 'babel-loader?cacheDirectory=true'`), the loader will use the default cache directory in `node_modules/.cache/babel-loader` or fallback to the default OS temporary file directory if no `node_modules` folder could be found in any root directory.
  68. * `cacheIdentifier`: Default is a string composed by the `babel-core`'s version, the `babel-loader`'s version, the contents of `.babelrc` file if it exists, and the value of the environment variable `BABEL_ENV` with a fallback to the `NODE_ENV` environment variable. This can be set to a custom value to force cache busting if the identifier changes.
  69. * `cacheCompression`: Default `true`. When set, each Babel transform output will be compressed with Gzip. If you want to opt-out of cache compression, set it to `false` -- your project may benefit from this if it transpiles thousands of files.
  70. * `customize`: Default `null`. The path of a module that exports a `custom` callback [like the one that you'd pass to `.custom()`](#customized-loader). Since you already have to make a new file to use this, it is recommended that you instead use `.custom` to create a wrapper loader. Only use this is you _must_ continue using `babel-loader` directly, but still want to customize.
  71. **Note**: The `sourceMap` option is ignored. Instead, source maps are automatically enabled when webpack is configured to use them (via the [`devtool`](https://webpack.js.org/configuration/devtool/#devtool) config option).
  72. ## Troubleshooting
  73. ### babel-loader is slow!
  74. Make sure you are transforming as few files as possible. Because you are probably matching `/\.m?js$/`, you might be transforming the `node_modules` folder or other unwanted source.
  75. To exclude `node_modules`, see the `exclude` option in the `loaders` config as documented above.
  76. You can also speed up babel-loader by as much as 2x by using the `cacheDirectory` option. This will cache transformations to the filesystem.
  77. ### Babel is injecting helpers into each file and bloating my code!
  78. Babel uses very small helpers for common functions such as `_extend`. By default, this will be added to every file that requires it.
  79. You can instead require the Babel runtime as a separate module to avoid the duplication.
  80. The following configuration disables automatic per-file runtime injection in Babel, requiring `babel-plugin-transform-runtime` instead and making all helper references use it.
  81. See the [docs](https://babeljs.io/docs/plugins/transform-runtime/) for more information.
  82. **NOTE**: You must run `npm install -D @babel/plugin-transform-runtime` to include this in your project and `babel-runtime` itself as a dependency with `npm install @babel/runtime`.
  83. ```javascript
  84. rules: [
  85. // the 'transform-runtime' plugin tells Babel to
  86. // require the runtime instead of inlining it.
  87. {
  88. test: /\.m?js$/,
  89. exclude: /(node_modules|bower_components)/,
  90. use: {
  91. loader: 'babel-loader',
  92. options: {
  93. presets: ['@babel/preset-env'],
  94. plugins: ['@babel/plugin-transform-runtime']
  95. }
  96. }
  97. }
  98. ]
  99. ```
  100. #### **NOTE**: transform-runtime & custom polyfills (e.g. Promise library)
  101. Since [babel-plugin-transform-runtime](https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-runtime) includes a polyfill that includes a custom [regenerator-runtime](https://github.com/facebook/regenerator/blob/master/packages/regenerator-runtime/runtime.js) and [core-js](https://github.com/zloirock/core-js), the following usual shimming method using `webpack.ProvidePlugin` will not work:
  102. ```javascript
  103. // ...
  104. new webpack.ProvidePlugin({
  105. 'Promise': 'bluebird'
  106. }),
  107. // ...
  108. ```
  109. The following approach will not work either:
  110. ```javascript
  111. require('@babel/runtime/core-js/promise').default = require('bluebird');
  112. var promise = new Promise;
  113. ```
  114. which outputs to (using `runtime`):
  115. ```javascript
  116. 'use strict';
  117. var _Promise = require('@babel/runtime/core-js/promise')['default'];
  118. require('@babel/runtime/core-js/promise')['default'] = require('bluebird');
  119. var promise = new _Promise();
  120. ```
  121. The previous `Promise` library is referenced and used before it is overridden.
  122. One approach is to have a "bootstrap" step in your application that would first override the default globals before your application:
  123. ```javascript
  124. // bootstrap.js
  125. require('@babel/runtime/core-js/promise').default = require('bluebird');
  126. // ...
  127. require('./app');
  128. ```
  129. ### The Node.js API for `babel` has been moved to `babel-core`.
  130. If you receive this message, it means that you have the npm package `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):
  131. ```javascript
  132. {
  133. test: /\.m?js$/,
  134. loader: 'babel',
  135. }
  136. ```
  137. webpack then tries to load the `babel` package instead of the `babel-loader`.
  138. To fix this, you should uninstall the npm package `babel`, as it is deprecated in Babel v6. (Instead, install `babel-cli` or `babel-core`.)
  139. In the case one of your dependencies is installing `babel` and you cannot uninstall it yourself, use the complete name of the loader in the webpack config:
  140. ```javascript
  141. {
  142. test: /\.m?js$/,
  143. loader: 'babel-loader',
  144. }
  145. ```
  146. ## Customized Loader
  147. `babel-loader` exposes a loader-builder utility that allows users to add custom handling
  148. of Babel's configuration for each file that it processes.
  149. `.custom` accepts a callback that will be called with the loader's instance of
  150. `babel` so that tooling can ensure that it using exactly the same `@babel/core`
  151. instance as the loader itself.
  152. In cases where you want to customize without actually having a file to call `.custom`, you
  153. may also pass the `customize` option with a string pointing at a file that exports
  154. your `custom` callback function.
  155. ### Example
  156. ```js
  157. // Export from "./my-custom-loader.js" or whatever you want.
  158. module.exports = require("babel-loader").custom(babel => {
  159. function myPlugin() {
  160. return {
  161. visitor: {},
  162. };
  163. }
  164. return {
  165. // Passed the loader options.
  166. customOptions({ opt1, opt2, ...loader }) {
  167. return {
  168. // Pull out any custom options that the loader might have.
  169. custom: { opt1, opt2 },
  170. // Pass the options back with the two custom options removed.
  171. loader,
  172. };
  173. },
  174. // Passed Babel's 'PartialConfig' object.
  175. config(cfg) {
  176. if (cfg.hasFilesystemConfig()) {
  177. // Use the normal config
  178. return cfg.options;
  179. }
  180. return {
  181. ...cfg.options,
  182. plugins: [
  183. ...(cfg.options.plugins || []),
  184. // Include a custom plugin in the options.
  185. myPlugin,
  186. ],
  187. };
  188. },
  189. result(result) {
  190. return {
  191. ...result,
  192. code: result.code + "\n// Generated by some custom loader",
  193. };
  194. },
  195. };
  196. });
  197. ```
  198. ```js
  199. // And in your Webpack config
  200. module.exports = {
  201. // ..
  202. module: {
  203. rules: [{
  204. // ...
  205. loader: path.join(__dirname, 'my-custom-loader.js'),
  206. // ...
  207. }]
  208. }
  209. };
  210. ```
  211. ### `customOptions(options: Object): { custom: Object, loader: Object }`
  212. Given the loader's options, split custom options out of `babel-loader`'s
  213. options.
  214. ### `config(cfg: PartialConfig): Object`
  215. Given Babel's `PartialConfig` object, return the `options` object that should
  216. be passed to `babel.transform`.
  217. ### `result(result: Result): Result`
  218. Given Babel's result object, allow loaders to make additional tweaks to it.
  219. ## [License](https://couto.mit-license.org/)