Webpack processes ES2015 module definitions by default and transforms them into code. It does not transform specific syntax, such as const
, though. The resulting code can be problematic especially in the older browsers.
Webpack默认状况下处理ES015模块定义并转换成代码。但它 不能 转换特殊语法, 如const
。生成的代码可能会有问题,尤为是对于传统浏览器。 javascript
To get a better idea of the default transform, consider the example output below (npm run build -- --devtool false --mode development
):
为了更好的理解默认转换,参考下面示例的输出(npm run build -- --devtool false --mode development
):java
dist/main.jsnode
... /***/ (function(module, __webpack_exports__, __webpack_require__) { "use strict"; __webpack_require__.r(__webpack_exports__); /* harmony default export */ __webpack_exports__["default"] = ((text = "Hello world") => { const element = document.createElement("div"); element.className = "pure-button"; element.innerHTML = text; return element; }); ...
The problem can be worked around by processing the code through Babel, a famous JavaScript compiler that supports ES2015+ features and more. It resembles ESLint in that it's built on top of presets and plugins. Presets are collections of plugins, and you can define your own as well.<br/>
用Babel来处理代码能够绕过这个问题, 它是一个知名的Javascript编译器,支持ES015+特性和其余一些功能. 它与ESLint相似,也是在预设(presets)和插件之上构建。预设(Presets)是插件的集合,你也能够本身定义<br/>react
T> Given sometimes extending existing presets is not enough, modify-babel-preset allows you to go a step further and configure the base preset in a more flexible way.<br/>
T> 鉴于有时扩展示有预设是不够的, modify-babel-preset 让你能更进一步并以更灵活的方式配置基本预设。webpack
Even though Babel can be used standalone, as you can see in the SurviveJS - Maintenance book, you can hook it up with webpack as well. During development, it can make sense to skip processing if you are using language features supported by your browser.<br/>
即便Babel能够单独使用,正如你在 SurvieJS-Maintenance书中所看到的,你也能够把它和Webpack链接起来。在开发的时候,若是你的浏览器支持语言特性,跳过这步处理仍是不错的。git
Skipping processing is a good option primarily if you don't rely on any custom language features and work using a modern browser. Processing through Babel becomes almost a necessity when you compile your code for production, though.<br/>
若是不依赖特定的语言特性并工做在现代浏览器上,跳过处理是一个不错的选择。可是当你为生产环境编译代码时,经过Babel处理源码几乎是一个必选项。es6
You can use Babel with webpack through babel-loader. It can pick up project level Babel configuration, or you can configure it at the webpack loader itself. babel-webpack-plugin is another lesser-known option.<br/>
你能够经过babel-loader在Webpack中使用Babel。它可使用项目级别的配置,或者在Webpack的加载器中配置它。babel-webpack-plugin 是另外一个相对不太知名的选择。github
Connecting Babel with a project allows you to process webpack configuration through it. To achieve this, name your webpack configuration using the webpack.config.babel.js convention. interpret package enables this, and it supports other compilers as well.
将Babel与项目相连,就能经过它来处理Webpack配置了。要实现这个功能,根据约定用 webpack.config.babel.js 命名你的Webpack配置文件. interpret 包实现了这个功能,它还支持其余编译器。web
T> Given that Node supports the ES2015 specification well these days, you can use a lot of ES2015 features without having to process configuration through Babel.<br/>
T> 考虑到近来 Node supports the ES2015 specification well , 你能够直接使用不少ES2015的特性而没必要经过Babel来处理配置了。typescript
W> If you use webpack.config.babel.js, take care with the "modules": false,
setting. If you want to use ES2015 modules, you could skip the setting in your global Babel configuration and then configure it per environment as discussed below.
W> 若是使用webpack.config.babel.js, 请注意配置项"modules": false,
. 若是想使用ES017的模块特性,能够跳过Babel的全局配置,而后像下面那样根据不一样环境的须要单独配置。
{pagebreak}
The first step towards configuring Babel to work with webpack is to set up babel-loader. It takes the code and turns it into a format older browsers can understand. Install babel-loader and include its peer dependency @babel/core:
让Babel和Webpack连携工做的第一步是配置babel-loader。它将代码转换成传统浏览器可以识别的格式。安装 babel-loader 以及它的同侪依赖(peer dependency) @babel/core。
npm install babel-loader @babel/core --save-dev
As usual, let's define a function for Babel:
和往常同样,咱们为Babel定义一个函数:
webpack.parts.js
exports.loadJavaScript = ({ include, exclude } = {}) => ({ module: { rules: [ { test: /\.js$/, include, exclude, use: "babel-loader", }, ], }, });
Next, you need to connect this to the main configuration. If you are using a modern browser for development, you can consider processing only the production code through Babel. It's used for both production and development environments in this case. Also, only application code is processed through Babel.<br/>
下面,你须要把这个函数与主配置联接。若是在使用现代浏览器开发,能够只有在处理生产环境代码时使用Babel。在这个例子中,生产环境和开发环境同时使用了Babel。而且,只有应用代码才经过Babel处理。
{pagebreak}
Adjust as below:
以下调整:
webpack.config.js
const commonConfig = merge([ ... leanpub-start-insert parts.loadJavaScript({ include: PATHS.app }), leanpub-end-insert ]);
Even though you have Babel installed and set up, you are still missing one bit: Babel configuration. The configuration can be set up using a .babelrc dotfile as then other tooling can use the same.
即便安装了Babel,并进行了配置,仍是差了一点:Babel的配置。可使用 .babelrc 点文件设置配置,其余工具也可使用相同的点文件。
W> If you try to import files outside of your configuration root directory and then process them through babel-loader, this fails. It's a known issue, and there are workarounds including maintaining .babelrc at a higher level in the project and resolving against Babel presets through require.resolve
at webpack configuration.
W> 若是从配置的根目录 之外 的地方导入文件这会致使 babel-loader 处理失败。这是一个已和的问题 a known issue, 不过有其余的方法来解决,包括在项目中把 .babelrc 级别设置得更高并经过Webpack配置中的 require.resolve
对Babel的预设进行处理。
At a minimum, you need babel-preset-env. It's a Babel preset that enables the required plugins based on the optional environment definition you pass to it.<br/>
你最少须要 babel-preset-env。 它是一个Babel预设,根据传递给它的可选环境定义启用所需的插件。
Install the preset first:
首先安装预设:
npm install @babel/preset-env --save-dev
To make Babel aware of the preset, you need to write a .babelrc. Given webpack supports ES2015 modules out of the box, you can tell Babel to skip processing them. Jumping over this step would break webpack's HMR mechanism although the production build would still work. You can also constrain the build output to work only in recent versions of Chrome.
建立一个 .babelrc 来让它注意到预设。 假设Webpack直接支持ES2015 模块特性(Module),可让Babel直接跳过处理他们。尽管生产环境构建还会工做,可是跳过这一步会破坏Webpack的HMR机制。您还能够限制构建输出只能在最新版本的Chrome中工做。
Adjust the target definition as you like. As long as you follow browserslist, it should work. Here's a sample configuration:
若是愿意的话能够调整目标定义。只要按照browserslist,应该可以工做。下面是一个配置的示例:
.babelrc
{ "presets": [ [ "@babel/preset-env", { "modules": false, } ] ] }
If you execute npm run build -- --devtool false --mode development
now and examine dist/main.js, you will see something different based on your .browserslistrc
file.
若是你如今执行 npm run build -- --devtool false --mode development
并检查 dist/main.js, 基于 .browserslistrc
文件这里发生了一些改变.
{pagebreak}
Try to include only a definition like IE 8
there, and the code should change accordingly:
试着在里面仅包含一个定义如 IE 8
,代码也发生了相应的改变:
dist/main.js
... /***/ (function(module, __webpack_exports__, __webpack_require__) { "use strict"; __webpack_require__.r(__webpack_exports__); /* harmony default export */ __webpack_exports__["default"] = (function () { var text = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "Hello world"; var element = document.createElement("div"); element.className = "pure-button"; element.innerHTML = text; return element; }); ...
Note especially how the function was transformed. You can try out different browser definitions and language features to see how the output changes based on the selection.
尤为要注意函数是如何发生改变的。您能够尝试不一样的浏览器定义和语言特性,以查看输出是如何根据选择进行更改的。
<翻译略,主要是针对传统浏览器的兼容问题>
babel-preset-env allows you to polyfill certain language features for older browsers. For this to work, you should enable its useBuiltIns
option ("useBuiltIns": true
) and install babel-polyfill. You have to include it in your project either through an import or an entry (app: ["babel-polyfill", PATHS.app]
). babel-preset-env rewrites the import based on your browser definition and loads only the polyfills that are needed.
babel-polyfill pollutes the global scope with objects like Promise
. Given this can be problematic for library authors, there's transform-runtime option. It can be enabled as a Babel plugin, and it avoids the problem of globals by rewriting the code in such way that they aren't be needed.
W> Certain webpack features, such as Code Splitting, write Promise
based code to webpack's bootstrap after webpack has processed loaders. The problem can be solved by applying a shim before your application code is executed. Example: entry: { app: ["core-js/es6/promise", PATHS.app] }
.
There are other possible .babelrc options beyond the ones covered here. Like ESLint, .babelrc supports JSON5 as its configuration format meaning you can include comments in your source, use single quoted strings, and so on.
除了介绍的选项,还有其余一些.babelrc options. 和ESLint同样, .babelrc 用JSON5 做为它的配置格式,这样就能够在代码中包含注释,使用单引号等等。
Sometimes you want to use experimental features that fit your project. Although you can find a lot of them within so-called stage presets, it's a good idea to enable them one by one and even organize them to a preset of their own unless you are working on a throwaway project. If you expect your project to live a long time, it's better to document the features you are using well.
有时要在项目中使用一些试验中的特性。尽管能够找到不少所谓的阶段性预设,可是最好仍是一个一个地启用它们,甚至将它们组织成本身的预设,除非您正在处理一个一次性项目。若是想让你的项目活得长点儿,最好文档化你使用得比较好的特性。
Babel isn't the only option although it's the most popular one. Buble by Rich Harris is another compiler worth checking out. There's experimental buble-loader that allows you to use it with webpack. Buble doesn't support ES2015 modules, but that's not a problem as webpack provides that functionality.
Babel尽管深受欢迎但不是惟一选择。Rich Harris的Buble 是另外一个值得一试的编译器。 有个试验用的加载器buble-loader
{pagebreak}
Perhaps the greatest thing about Babel is that it's possible to extend with plugins:
或许Babel最伟大之处就在于它容许用插件扩展:
import { Button } from "antd";
instead of pointing to the module through an exact path.import { Button } from "antd";
而不用明确的指定模块的路径。console.warn
to functions that have @deprecate
annotation in their comment.@deprecate
标志的函数添加console.warn
.console.log
calls with information about invocation context, so it's easier to see where they logged.console.log
的调用注释中 , 这样就能很容易得找到在哪里被调用的了.propType
related code from your production build. It also allows component authors to generate code that's wrapped so that setting environment at DefinePlugin
can kick in as discussed in the book.propType
相关的代码删除掉. 它还容许组件做者生成封装的代码,以便在' DefinePlugin '设置环境,如书中讨论的那样。T> It's possible to connect Babel with Node through babel-register or babel-cli. These packages can be handy if you want to execute your code through Babel without using webpack.
T> 能够经过babel-register 或 babel-cli 来链接 Babel和 Node。若是你想只经过Babel而不用Webpack来执行你的代码,这些包会很是方便。
Babel allows you to control which presets and plugins are used per environment through its env option. You can manage Babel's behavior per build target this way.
经过env option, Babel让你可以根据环境控制哪些预设和插件可用. 能够根据构建目标的不一样来管理Babel的行为.
env
checks both NODE_ENV
and BABEL_ENV
and adds functionality to your build based on that. If BABEL_ENV
is set, it overrides any possible NODE_ENV
.env
会同时检查 NODE_ENV
和 BABEL_ENV
并基于此向你的构建中添加功能。 若是 BABEL_ENV
被设置, 它将覆盖全部可能的 NODE_ENV
.
Consider the example below:
参考下面的例子:
.babelrc
{ ... "env": { "development": { "plugins": [ "annotate-console-log" ] } } }
Any shared presets and plugins are available to all targets still. env
allows you to specialize your Babel configuration further.
任何共享的预设和插件对目标仍然可用。env
让你进一步定制你的Babel配置。
{pagebreak}
It's possible to pass the webpack environment to Babel with a tweak:
经过微调,能够将webpack环境传递给Babel:
webpack.config.js
module.exports = mode => { leanpub-start-insert process.env.BABEL_ENV = mode; leanpub-end-insert ... };
T> The way env
works is subtle. Consider logging env
and make sure it matches your Babel configuration or otherwise the functionality you expect is not applied to your build.
T> env
运行方式比较微妙. 考虑将env
记录到日志中,以确保它与你的Babel配置相匹配,不然你所指望的功能可能不会应用到构建上 .
Microsoft's TypeScript is a compiled language that follows a similar setup as Babel. The neat thing is that in addition to JavaScript, it can emit type definitions. A good editor can pick those up and provide enhanced editing experience. Stronger typing is valuable for development as it becomes easier to state your type contracts.
微软的 TypeScript 是一种编译语言,它的配置方式与Babel相似。 除了兼容Javascript以外,它还可使用类型定义。一个好的编辑器除了可使用这些特性还能加强编写体验。更强的类型对于开发颇有价值,由于它更容易声明类型契约。
Compared to Facebook's type checker Flow, TypeScript is a more secure option. As a result, you find more premade type definitions for it, and overall, the quality of support should be better.
与Facebook的类型检查程序流相比,TypeScript是一个更安全的选择。所以,您会发现它有更多预先定义的类型,总的来讲,支持的质量应该更好。
You can use TypeScript with webpack using the following loaders:
能够经过下面的加载器在Webpack中使用Typescript:
T> There's a TypeScript parser for ESLint. It's also possible to lint it through tslint.
T> 有一个 TypeScript parser for ESLint. 也能够经过 tslint对其进行处理.
Flow performs static analysis based on your code and its type annotations. You have to install it as a separate tool and then run it against your code. There's flow-status-webpack-plugin that allows you to run it through webpack during development.
Flow 基于代码和类型注释进行静态分析. 你不得不把它安装为一个独立的工具并在你的代码上运行它。有一个 flow-status-webpack-plugin 包可让你在开发时经过Webpack执行它.
If you use React, the React specific Babel preset does most of the work through babel-plugin-syntax-flow. It can strip Flow annotations and convert your code into a format that is possible to transpile further.
若是在使用React, 经过包 babel-plugin-syntax-flow, Webpack特定的Babel预设完成了大部分工做。它能够剥离流注释,并将代码转换为能够进一步转换的格式。
There's also babel-plugin-typecheck that allows you to perform runtime checks based on your Flow annotations. flow-runtime goes a notch further and provides more functionality. These approaches complement Flow static checker and allow you to catch even more issues.
还有一个包 babel-plugin-typecheck 能够基于流注释进行运行时检查. flow-runtime 更进一步,提供了更多的功能。这些方法补充了流静态检查器,并容许您捕获更多的问题。
T> flow-coverage-report shows how much of your code is covered by Flow type annotations.
T> flow-coverage-report 显示流类型注释覆盖了多少代码。
{pagebreak}
Babel has become an indispensable tool for developers given it bridges the standard with older browsers. Even if you targeted modern browsers, transforming through Babel is an option.
Babel已经成为开发人员不可或缺的工具,由于它链接了标准与旧浏览器。即便是针对现代浏览器,经过Babel进行转换也是一种选择。
To recap:(总结)