项目原始demo,不改动
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

README.md 14 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. # Now that `babel-preset-env` has stabilized, it has been [moved into the main Babel mono-repo](https://github.com/babel/babel/tree/master/packages/babel-preset-env) and this repo has been archived.
  2. The move makes it much easier to release and develop in sync with the rest of Babel!
  3. This repo will be made read-only, as all of the issues/labels have been moved over as well. Please report any bugs and open pull requests over on the [main mono-repo](https://github.com/babel/babel).
  4. ---
  5. # babel-preset-env [![npm](https://img.shields.io/npm/v/babel-preset-env.svg)](https://www.npmjs.com/package/babel-preset-env) [![travis](https://img.shields.io/travis/babel/babel-preset-env/master.svg)](https://travis-ci.org/babel/babel-preset-env) [![npm-downloads](https://img.shields.io/npm/dm/babel-preset-env.svg)](https://www.npmjs.com/package/babel-preset-env) [![codecov](https://img.shields.io/codecov/c/github/babel/babel-preset-env/master.svg?maxAge=43200)](https://codecov.io/github/babel/babel-preset-env)
  6. > A Babel preset that compiles [ES2015+](https://github.com/tc39/proposals/blob/master/finished-proposals.md) down to ES5 by automatically determining the Babel plugins and polyfills you need based on your targeted browser or runtime environments.
  7. ```sh
  8. npm install babel-preset-env --save-dev
  9. ```
  10. Without any configuration options, babel-preset-env behaves exactly the same as babel-preset-latest (or babel-preset-es2015, babel-preset-es2016, and babel-preset-es2017 together).
  11. > However, we don't recommend using `preset-env` this way because it doesn't take advantage of it's greater capabilities of targeting specific browsers.
  12. ```json
  13. {
  14. "presets": ["env"]
  15. }
  16. ```
  17. You can also configure it to only include the polyfills and transforms needed for the browsers you support. Compiling only what's needed can make your bundles smaller and your life easier.
  18. This example only includes the polyfills and code transforms needed for coverage of users > 0.25%, ignoring Internet Explorer 11 and Opera Mini.. We use [browserslist](https://github.com/ai/browserslist) to parse this information, so you can use [any valid query format supported by browserslist](https://github.com/ai/browserslist#queries).
  19. ```js
  20. {
  21. "presets": [
  22. ["env", {
  23. "targets": {
  24. // The % refers to the global coverage of users from browserslist
  25. "browsers": [ ">0.25%", "not ie 11", "not op_mini all"]
  26. }
  27. }]
  28. ]
  29. }
  30. ```
  31. > You can also target individual versions of browsers instead of using a query with `"targets": { "chrome": "52" }`.
  32. Similarly, if you're targeting Node.js instead of the browser, you can configure babel-preset-env to only include the polyfills and transforms necessary for a particular version:
  33. ```json
  34. {
  35. "presets": [
  36. ["env", {
  37. "targets": {
  38. "node": "6.10"
  39. }
  40. }]
  41. ]
  42. }
  43. ```
  44. For convenience, you can use `"node": "current"` to only include the necessary polyfills and transforms for the Node.js version that you use to run Babel:
  45. ```json
  46. {
  47. "presets": [
  48. ["env", {
  49. "targets": {
  50. "node": "current"
  51. }
  52. }]
  53. ]
  54. }
  55. ```
  56. Check out the many options (especially `useBuiltIns` to polyfill less)!
  57. - [How it Works](#how-it-works)
  58. - [Install](#install)
  59. - [Usage](#usage)
  60. - [Options](#options)
  61. - [Examples](#examples)
  62. - [Caveats](#caveats)
  63. - [Other Cool Projects](#other-cool-projects)
  64. ## How it Works
  65. ### Determine environment support for ECMAScript features
  66. Use external data such as [`compat-table`](https://github.com/kangax/compat-table) to determine browser support. (We should create PRs there when necessary)
  67. ![](https://cloud.githubusercontent.com/assets/588473/19214029/58deebce-8d48-11e6-9004-ee3fbcb75d8b.png)
  68. We can periodically run [build-data.js](https://github.com/babel/babel-preset-env/blob/master/scripts/build-data.js) which generates [plugins.json](https://github.com/babel/babel-preset-env/blob/master/data/plugins.json).
  69. Ref: [#7](https://github.com/babel/babel-preset-env/issues/7)
  70. ### Maintain a mapping between JavaScript features and Babel plugins
  71. > Currently located at [plugin-features.js](https://github.com/babel/babel-preset-env/blob/master/data/plugin-features.js).
  72. This should be straightforward to do in most cases. There might be cases where plugins should be split up more or certain plugins aren't standalone enough (or impossible to do).
  73. ### Support all plugins in Babel that are considered `latest`
  74. > Default behavior without options is the same as `babel-preset-latest`.
  75. It won't include `stage-x` plugins. env will support all plugins in what we consider the latest version of JavaScript (by matching what we do in [`babel-preset-latest`](http://babeljs.io/docs/plugins/preset-latest/)).
  76. Ref: [#14](https://github.com/babel/babel-preset-env/issues/14)
  77. ### Determine the lowest common denominator of plugins to be included in the preset
  78. If you are targeting IE 8 and Chrome 55 it will include all plugins required by IE 8 since you would need to support both still.
  79. ### Support a target option `"node": "current"` to compile for the currently running node version.
  80. For example, if you are building on Node 6, arrow functions won't be converted, but they will if you build on Node 0.12.
  81. ### Support a `browsers` option like autoprefixer
  82. Use [browserslist](https://github.com/ai/browserslist) to declare supported environments by performing queries like `> 1%, last 2 versions`.
  83. Ref: [#19](https://github.com/babel/babel-preset-env/pull/19)
  84. ## Install
  85. With [npm](https://www.npmjs.com):
  86. ```sh
  87. npm install --save-dev babel-preset-env
  88. ```
  89. Or [yarn](https://yarnpkg.com):
  90. ```sh
  91. yarn add babel-preset-env --dev
  92. ```
  93. ## Usage
  94. The default behavior without options runs all transforms (behaves the same as [babel-preset-latest](https://babeljs.io/docs/plugins/preset-latest/)).
  95. ```json
  96. {
  97. "presets": ["env"]
  98. }
  99. ```
  100. ## Options
  101. For more information on setting options for a preset, refer to the [plugin/preset options](http://babeljs.io/docs/plugins/#plugin-preset-options) documentation.
  102. ### `targets`
  103. `{ [string]: number | string }`, defaults to `{}`.
  104. Takes an object of environment versions to support.
  105. Each target environment takes a number or a string (we recommend using a string when specifying minor versions like `node: "6.10"`).
  106. Example environments: `chrome`, `opera`, `edge`, `firefox`, `safari`, `ie`, `ios`, `android`, `node`, `electron`.
  107. The [data](https://github.com/babel/babel-preset-env/blob/master/data/plugins.json) for this is generated by running the [build-data script](https://github.com/babel/babel-preset-env/blob/master/scripts/build-data.js) which pulls in data from [compat-table](https://kangax.github.io/compat-table).
  108. ### `targets.node`
  109. `number | string | "current" | true`
  110. If you want to compile against the current node version, you can specify `"node": true` or `"node": "current"`, which would be the same as `"node": process.versions.node`.
  111. ### `targets.browsers`
  112. `Array<string> | string`
  113. A query to select browsers (ex: last 2 versions, > 5%) using [browserslist](https://github.com/ai/browserslist).
  114. Note, browsers' results are overridden by explicit items from `targets`.
  115. ### `targets.uglify`
  116. `true`
  117. When using `uglify-js` to minify your code, you may run into syntax errors when targeting later browsers since `uglify-js` does not support any ES2015+ syntax.
  118. To prevent these errors - set the `uglify` option to `true`, which enables all transformation plugins and as a result, your code is fully compiled to ES5. However, the `useBuiltIns` option will still work as before and only include the polyfills that your target(s) need.
  119. > Uglify has support for ES2015 syntax via [uglify-es](https://github.com/mishoo/UglifyJS2/tree/harmony). If you are using syntax unsupported by `uglify-es`, we recommend using [babel-minify](https://github.com/babel/minify).
  120. > Note: This option is deprecated in 2.x and replaced with a [`forceAllTransforms` option](https://github.com/babel/babel-preset-env/pull/264).
  121. ### `spec`
  122. `boolean`, defaults to `false`.
  123. Enable more spec compliant, but potentially slower, transformations for any plugins in this preset that support them.
  124. ### `loose`
  125. `boolean`, defaults to `false`.
  126. Enable "loose" transformations for any plugins in this preset that allow them.
  127. ### `modules`
  128. `"amd" | "umd" | "systemjs" | "commonjs" | false`, defaults to `"commonjs"`.
  129. Enable transformation of ES6 module syntax to another module type.
  130. Setting this to `false` will not transform modules.
  131. ### `debug`
  132. `boolean`, defaults to `false`.
  133. Outputs the targets/plugins used and the version specified in [plugin data version](https://github.com/babel/babel-preset-env/blob/master/data/plugins.json) to `console.log`.
  134. ### `include`
  135. `Array<string>`, defaults to `[]`.
  136. > NOTE: `whitelist` is deprecated and will be removed in the next major in favor of this.
  137. An array of plugins to always include.
  138. Valid options include any:
  139. - [Babel plugins](https://github.com/babel/babel-preset-env/blob/master/data/plugin-features.js) - both with (`babel-plugin-transform-es2015-spread`) and without prefix (`transform-es2015-spread`) are supported.
  140. - [Built-ins](https://github.com/babel/babel-preset-env/blob/master/data/built-in-features.js), such as `map`, `set`, or `object.assign`.
  141. This option is useful if there is a bug in a native implementation, or a combination of a non-supported feature + a supported one doesn't work.
  142. For example, Node 4 supports native classes but not spread. If `super` is used with a spread argument, then the `transform-es2015-classes` transform needs to be `include`d, as it is not possible to transpile a spread with `super` otherwise.
  143. > NOTE: The `include` and `exclude` options _only_ work with the [plugins included with this preset](https://github.com/babel/babel-preset-env/blob/master/data/plugin-features.js); so, for example, including `transform-do-expressions` or excluding `transform-function-bind` will throw errors. To use a plugin _not_ included with this preset, add them to your [config](https://babeljs.io/docs/usage/babelrc/) directly.
  144. ### `exclude`
  145. `Array<string>`, defaults to `[]`.
  146. An array of plugins to always exclude/remove.
  147. The possible options are the same as the `include` option.
  148. This option is useful for "blacklisting" a transform like `transform-regenerator` if you don't use generators and don't want to include `regeneratorRuntime` (when using `useBuiltIns`) or for using another plugin like [fast-async](https://github.com/MatAtBread/fast-async) instead of [Babel's async-to-gen](http://babeljs.io/docs/plugins/transform-async-generator-functions/).
  149. ### `useBuiltIns`
  150. `boolean`, defaults to `false`.
  151. A way to apply `babel-preset-env` for polyfills (via "babel-polyfill").
  152. > NOTE: This does not currently polyfill experimental/stage-x built-ins like the regular "babel-polyfill" does.
  153. > This will only work with npm >= 3 (which should be used with Babel 6 anyway)
  154. ```
  155. npm install babel-polyfill --save
  156. ```
  157. This option enables a new plugin that replaces the statement `import "babel-polyfill"` or `require("babel-polyfill")` with individual requires for `babel-polyfill` based on environment.
  158. > NOTE: Only use `require("babel-polyfill");` once in your whole app.
  159. > Multiple imports or requires of `babel-polyfill` will throw an error since it can cause global collisions and other issues that are hard to trace.
  160. > We recommend creating a single entry file that only contains the `require` statement.
  161. **In**
  162. ```js
  163. import "babel-polyfill";
  164. ```
  165. **Out (different based on environment)**
  166. ```js
  167. import "core-js/modules/es7.string.pad-start";
  168. import "core-js/modules/es7.string.pad-end";
  169. import "core-js/modules/web.timers";
  170. import "core-js/modules/web.immediate";
  171. import "core-js/modules/web.dom.iterable";
  172. ```
  173. This will also work for `core-js` directly (`import "core-js";`)
  174. ```
  175. npm install core-js --save
  176. ```
  177. ---
  178. ## Examples
  179. ### Export with various targets
  180. ```js
  181. export class A {}
  182. ```
  183. #### Target only Chrome 52
  184. **.babelrc**
  185. ```json
  186. {
  187. "presets": [
  188. ["env", {
  189. "targets": {
  190. "chrome": 52
  191. }
  192. }]
  193. ]
  194. }
  195. ```
  196. **Out**
  197. ```js
  198. class A {}
  199. exports.A = A;
  200. ```
  201. #### Target Chrome 52 with webpack 2/rollup and loose mode
  202. **.babelrc**
  203. ```json
  204. {
  205. "presets": [
  206. ["env", {
  207. "targets": {
  208. "chrome": 52
  209. },
  210. "modules": false,
  211. "loose": true
  212. }]
  213. ]
  214. }
  215. ```
  216. **Out**
  217. ```js
  218. export class A {}
  219. ```
  220. #### Target specific browsers via browserslist
  221. **.babelrc**
  222. ```json
  223. {
  224. "presets": [
  225. ["env", {
  226. "targets": {
  227. "chrome": 52,
  228. "browsers": ["last 2 versions", "safari 7"]
  229. }
  230. }]
  231. ]
  232. }
  233. ```
  234. **Out**
  235. ```js
  236. export var A = function A() {
  237. _classCallCheck(this, A);
  238. };
  239. ```
  240. #### Target latest node via `node: true` or `node: "current"`
  241. **.babelrc**
  242. ```json
  243. {
  244. "presets": [
  245. ["env", {
  246. "targets": {
  247. "node": "current"
  248. }
  249. }]
  250. ]
  251. }
  252. ```
  253. **Out**
  254. ```js
  255. class A {}
  256. exports.A = A;
  257. ```
  258. ### Show debug output
  259. **.babelrc**
  260. ```json
  261. {
  262. "presets": [
  263. [ "env", {
  264. "targets": {
  265. "safari": 10
  266. },
  267. "modules": false,
  268. "useBuiltIns": true,
  269. "debug": true
  270. }]
  271. ]
  272. }
  273. ```
  274. **stdout**
  275. ```sh
  276. Using targets:
  277. {
  278. "safari": 10
  279. }
  280. Modules transform: false
  281. Using plugins:
  282. transform-exponentiation-operator {}
  283. transform-async-to-generator {}
  284. Using polyfills:
  285. es7.object.values {}
  286. es7.object.entries {}
  287. es7.object.get-own-property-descriptors {}
  288. web.timers {}
  289. web.immediate {}
  290. web.dom.iterable {}
  291. ```
  292. ### Include and exclude specific plugins/built-ins
  293. > always include arrow functions, explicitly exclude generators
  294. ```json
  295. {
  296. "presets": [
  297. ["env", {
  298. "targets": {
  299. "browsers": ["last 2 versions", "safari >= 7"]
  300. },
  301. "include": ["transform-es2015-arrow-functions", "es6.map"],
  302. "exclude": ["transform-regenerator", "es6.set"]
  303. }]
  304. ]
  305. }
  306. ```
  307. ## Caveats
  308. If you get a `SyntaxError: Unexpected token ...` error when using the [object-rest-spread](https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-object-rest-spread) transform then make sure the plugin has been updated to, at least, `v6.19.0`.
  309. ## Other Cool Projects
  310. - [babel-preset-modern-browsers](https://github.com/christophehurpeau/babel-preset-modern-browsers)
  311. - ?