Skip to content

Commit a1efa41

Browse files
committed
Merge remote-tracking branch 'upstream/cn' into cn
2 parents 71b50d1 + 9a6ecd5 commit a1efa41

File tree

8 files changed

+397
-316
lines changed

8 files changed

+397
-316
lines changed

content/guides/caching.md

Lines changed: 49 additions & 47 deletions
Large diffs are not rendered by default.

content/guides/migrating.md

Lines changed: 93 additions & 102 deletions
Large diffs are not rendered by default.

content/guides/shimming.md

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ contributors:
77
- simon04
88
---
99

10-
`webpack` as a module bundler can understand modules written as ES2015 modules, CommonJS or AMD. But many times, while using third party libraries, we see that they expect dependencies which are global aka `$` for `jquery`. They might also be creating global variables which need to be exported. Here we will see different ways to help webpack understand these __broken modules__.
10+
`webpack` 作为模块打包工具可以支持 ES2015 模块,根据 CommonJS 或者 AMD 规范编写的模块。但是很多时候,当使用第三方 library 的时候,可以看出我们还期望有一些全局依赖,比如对于大家都知道 `jquery``$`。这可能也会创建一些需要被导出的全局变量。在此,我们会看到通过不同的方式去帮助 webpack 支持这些**彼此割裂的模块**
1111

12-
## Prefer unminified CommonJS/AMD files over bundled `dist` versions.
12+
## 未压缩的 CommonJS/AMD 文件优先于 `dist` 打包版本。
1313

14-
Most modules link the `dist` version in the `main` field of their `package.json`. While this is useful for most developers, for webpack it is better to alias the src version because this way webpack is able to optimize dependencies better. However in most cases `dist` works fine as well.
14+
多数模块会在 `package.json``main` 字段中链接到 library 的 `dist` 版本。虽然对多数开发者来说这是有用的,但对于 webpack 来说更好的方式是链接到 src 版本的别名上,因为这样 webpack 能够更好地优化依赖。然而多数情况下 `dist` 版本也能正常运行。
1515

1616
``` javascript
1717
// webpack.config.js
@@ -27,8 +27,8 @@ module.exports = {
2727
```
2828

2929
## `provide-plugin`
30-
The [`provide-plugin`](/plugins/provide-plugin) makes a module available as a variable in every other module required by `webpack`. The module is required only if you use the variable.
31-
Most legacy modules rely on the presence of specific globals, like jQuery plugins do on `$` or `jQuery`. In this scenario, you can configure webpack to prepend `var $ = require(“jquery”)` every time it encounters the global `$` identifier.
30+
The [`provide-plugin`](/plugins/provide-plugin) 可以将模块作为一个变量,被 `webpack` 在其他每个模块中引用。只有你需要使用此变量的时候,这个模块才会被 require 进来。
31+
多数之前遗留的模块,会依赖于已存在的某些特定全局变量,比如 jQuery 插件中的 `$` 或者 `jQuery`。在这种场景,你可以在每次遇到全局标识符 `$` 的时候,在 webpack 中预先设置 `var $ = require(“jquery”)`
3232

3333
```javascript
3434
module.exports = {
@@ -43,8 +43,8 @@ module.exports = {
4343

4444
## `imports-loader`
4545

46-
[`imports-loader`](/loaders/imports-loader/) inserts necessary globals into the required legacy module.
47-
For example, Some legacy modules rely on `this` being the `window` object. This becomes a problem when the module is executed in a CommonJS context where `this` equals `module.exports`. In this case you can override `this` using the `imports-loader`.
46+
[`imports-loader`](/loaders/imports-loader/) 在引用了之前遗留模块的代码中,插入必需的全局变量。
47+
例如,某些遗留模块依赖于 `this` 作为 `window` 对象,而在 CommonJS 上下文中执行的 `this` 等同于 `module.exports`。在这种情况下,你可以使用 `imports-loader` 来替换重写 `this`
4848

4949
**webpack.config.js**
5050
```javascript
@@ -58,7 +58,7 @@ module.exports = {
5858
};
5959
```
6060

61-
There are modules that support different [module styles](/concepts/modules), like AMD, CommonJS and legacy. However, most of the time they first check for `define` and then use some quirky code to export properties. In these cases, it could help to force the CommonJS path by setting `define = false`:
61+
webpack 中的模块支持不同的[模块风格](/concepts/modules),比如AMD, CommonJS 以及之前的遗留模块。然而,通常会先检查 `define`,然后使用一些比较怪异的代码来导出属性。在这些情况下,可以通过设置 `define = false`,有助于强制使用 CommonJS 路径:
6262

6363
**webpack.config.js**
6464
```javascript
@@ -74,7 +74,7 @@ module.exports = {
7474

7575
## `exports-loader`
7676

77-
Let's say a library creates a global variable that it expects it's consumers to use. In this case we can use [`exports-loader`](/loaders/exports-loader/), to export that global variable in CommonJS format. For instance, in order to export `file` as `file` and `helpers.parse` as `parse`:
77+
比如说,一个 library 创建了一个全局变量,它期望使用者通过全局变量去使用。在这种情况下,我们能够使用 [`exports-loader`](/loaders/exports-loader/),将全局变量导出为 CommonJS 格式。比如,为了将 `file` 导出为 `file` 以及将 `helpers.parse` 导出为 `parse`
7878

7979
**webpack.config.js**
8080
```javascript
@@ -83,7 +83,7 @@ module.exports = {
8383
rules: [{
8484
test: require.resolve("some-module"),
8585
use: 'exports-loader?file,parse=helpers.parse'
86-
// adds below code the file's source:
86+
// 在文件的源码中加入以下代码
8787
// exports["file"] = file;
8888
// exports["parse"] = helpers.parse;
8989
}]
@@ -93,32 +93,33 @@ module.exports = {
9393

9494
## `script-loader`
9595

96-
The [script-loader](/loaders/script-loader/) evaluates code in the global context, just like you would add the code into a `script` tag. In this mode every normal library should work. require, module, etc. are undefined.
9796

98-
W> The file is added as string to the bundle. It is not minimized by `webpack`, so use a minimized version. There is also no dev tool support for libraries added by this loader.
97+
[script-loader](/loaders/script-loader/) 在全局上下文中执行代码,如同你在 `script` 标签中加入代码。在这种模式下普通的 library 都能够正常运行。访问 require, module 等变量则是 undefined。
9998

100-
Assuming you have a `legacy.js` file containing …
99+
W> 文件作为字符串添加到 bundle 中。它不会被 `webpack` 压缩,因此如果使用了一个压缩后的版本,没有开发工具支持调试此 loader 添加的 library。
100+
101+
假设你有一个 `legacy.js` 文件包含……
101102
```javascript
102103
GLOBAL_CONFIG = {};
103104
```
104105

105-
… using the `script-loader`
106+
...使用 `script-loader`...
106107

107108
```javascript
108109
require('script-loader!legacy.js');
109110
```
110111

111-
… basically yields:
112+
...基本上会生成:
112113

113114
```javascript
114115
eval("GLOBAL_CONFIG = {};");
115116
```
116117

117-
## `noParse` option
118+
## `noParse` 选项
118119

119-
When there is no AMD/CommonJS version of the module and you want to include the `dist`, you can flag this module as [`noParse`](/configuration/module/#module-noparse). Then `webpack` will just include the module without parsing it, which can be used to improve the build time.
120+
当模块没有 AMD/CommonJS 的版本时,并且你希望直接引入 `dist` 版本,你可以将这个模块标记为 [`noParse`](/configuration/module/#module-noparse)。然后 `webpack` 将会直接引入这个模块并且不会解析它,这样可以用来改善构建时间。
120121

121-
W> Any feature requiring the AST, like the `ProvidePlugin`, will not work.
122+
W> 任何用到 AST 特性(比如 `ProvidePlugin`)都不会工作。
122123

123124
```javascript
124125
module.exports = {
@@ -130,4 +131,4 @@ module.exports = {
130131

131132
***
132133

133-
> 原文:https://webpack.js.org/guides/shimming/
134+
> 原文:https://webpack.js.org/guides/shimming/

content/guides/webpack-and-typescript.md

Lines changed: 38 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
---
2-
title: Webpack & Typescript
2+
title: Webpack 和 TypeScript
33
sort: 20
44
contributors:
55
- morsdyce
66
---
77

8-
[TypeScript](https://www.typescriptlang.org) is a typed superset of JavaScript that compiles to plain JavaScript, in this guide we will learn how to integrate Typescript with webpack.
8+
[TypeScript](https://www.typescriptlang.org) 是具有类型系统的 JavaScript 的超集,通过编译转化为普通 JavaScript 代码。这篇指南里我们将会学习 webpack 是如何跟 TypeScript 进行结合。
99

10-
## Basic Setup
10+
## 基础构建
1111

12-
In order to get started with webpack and Typescript, first we must install webpack in our project.
13-
If you didn't do so already please check out [webpack Installation Guide](https://webpack.js.org/guides/installation/).
12+
在开始 webpack 和 TypeScript 整合之前,我们首先必须在项目里安装 webpack。如果你还没安装,请查阅 [webpack 安装指南](https://webpack.js.org/guides/installation/)
1413

15-
To start using webpack with Typescript you need a couple of things:
16-
1. Install the Typescript compiler in your project.
17-
2. Install a Typescript loader (in this case we're using ts-loader).
18-
3. Create a __tsconfig.json__ file to contain our TypeScript compilation configuration.
19-
3. Create __webpack.config.js__ to contain our webpack configuration.
14+
要能在 webpack 里使用 TypeScript,你需要准备好下面这些事情:
15+
1. 在项目里安装 TypeScript 编译器;
16+
2. 选择一个 TypeScript loader 安装(这个示例里使用的是 ts-loader);
17+
3. 创建 __tsconfig.json__ 文件,这是 TypeScript 编辑器的配置文件;
18+
4. 创建 __webpack.config.js__ 文件,这是 webpack 的配置文件。
2019

21-
You can install the TypeScript compiler and the TypeScript loader from npm by running:
20+
你可以通过 npm 安装 TypeScript 编译器和 TypeScript loader,运行下面这个命令来安装:
2221
`npm install --save-dev typescript ts-loader`
2322

2423
__tsconfig.json__
2524

26-
The tsconfig file can start as an empty configuration file, here you can see an example of a basic configuration for TypeScript to compile to es5 as well as providing support for JSX.
25+
tsconfig 配置文件可以从一个空白的文件逐一添加配置项,下面有一个基本的配置示例,用来把 TypeScript 代码编译成 es5 代码,同时支持 JSX
2726

2827
```json
2928
{
@@ -39,11 +38,12 @@ The tsconfig file can start as an empty configuration file, here you can see an
3938
}
4039
```
4140

42-
You can read more about tsconfig.json configuration options at the [TypeScript documentation website](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html)
41+
可以查看 [TypeScript 官方文档](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html)了解更多关于 tsconfig.json 的配置选项。
4342

4443
__webpack.config.js__
4544

46-
A basic webpack with TypeScript config should look along these lines:
45+
使用 TypeScript 编写的 webpack 基本配置大概是这样:
46+
4747
```js
4848
module.exports = {
4949
entry: './index.ts',
@@ -65,28 +65,24 @@ module.exports = {
6565
},
6666
};
6767
```
68-
69-
Here we specify our entry point to be __index.ts__ in our current directory,
70-
an output file called __bundle.js__
71-
and our TypeScript loader that is in charge of compiling our TypeScript file to JavaScript. We also add `resolve.extensions` to instruct webpack what file extensions to use when resolving Typescript modules.
7268

73-
## Typescript loaders
69+
这个例子里我们指定了入口起点为当前目录的 __index.ts__ 文件,输出文件命名为 __bundle.js__,以及负责把 TypeScript 编译成 JavaScript 的 TypeScript loader,同时也添加了 `resolve.extensions` 来告诉 webpack 在解析查找 TypeScript 模块时该检索哪些文件扩展名。
70+
71+
## TypeScript loaders
7472

75-
Currently there are 2 loaders for TypeScript available:
73+
当前有 2 个可用的 TypeScript loader:
7674
* [awesome-typescript-loader](https://github.com/s-panferov/awesome-typescript-loader)
7775
* [ts-loader](https://github.com/TypeStrong/ts-loader)
7876

79-
Awesome TypeScript loader has created a wonderful explanation of the
80-
difference between awesome-typescript-loader and ts-loader.
77+
Awesome TypeScript loader 文档里已经很好的解释了 awesome-typescript-loader 和 ts-loader 的区别。
8178

82-
You can read more about it [here](https://github.com/s-panferov/awesome-typescript-loader#differences-between-ts-loader).
79+
可以阅读 [这篇文章](https://github.com/s-panferov/awesome-typescript-loader#differences-between-ts-loader) 了解更多。
8380

84-
In this guide we will be using ts-loader as currently it is easier enabling additional webpack features such as importing non code assets into your project.
81+
在本指南中,我们将使用ts-loader,因为它更简便地启用额外的 webpack 功能,例如将非代码资源导入到项目中。
8582

86-
## Enabling source maps
83+
## 启用 source maps 功能
8784

88-
In order to enable source maps we first must configure TypeScript to output inline source maps to our compiled JavaScript files.
89-
This is done by setting the sourceMap property to true.
85+
为了启用 source maps 功能,首先必须配置 TypeScript 将 source maps 内联输出到编译好的 JavaScript 文件,可通过将 sourceMap 属性设置为 true 来实现。
9086

9187
__tsconfig.json__
9288
```json
@@ -95,9 +91,7 @@ __tsconfig.json__
9591
}
9692
```
9793

98-
Once TypeScript is configured to output source maps we need to tell webpack
99-
to extract these source maps and pass them to the browser, this way we will get the source file
100-
exactly as we see it in our code editor.
94+
当开启了 TypeScript 的 source maps 输出特性后,我们需要告诉 webpack 来提取这些 source maps 并发送给浏览器,这样我们在浏览器看到的源码文件,就跟在代码编辑器中看到的一样。
10195

10296
__webpack.config.js__
10397
```js
@@ -127,41 +121,31 @@ module.exports = {
127121
devtool: 'inline-source-map',
128122
};
129123
```
130-
131-
First we add a new loader called source-map-loader.
132124

133-
To install it run:
125+
首先我们添加一个新 loader,名为 source-map-loader,运行下面的命令安装:
126+
`npm install --save-dev source-map-loader`
134127

135-
`npm install --save-dev source-map-loader`.
136-
137-
Once the loader is installed we need to tell webpack we want to run this loader before any other loaders by using the `enforce: 'pre'` configuration flag.
138-
Finally we need to enable source maps in webpack by specifying the `devtool` property.
139-
Currently we use the 'inline-source-map' setting, to read more about this setting and see other options check out the [devtool documentation](https://webpack.js.org/configuration/devtool/).
140-
141-
142-
128+
这个 loader 安装完成后,我们想让这个 loader 比其他任何 loader 都要先运行,可以使用 `enforce: 'pre'` 这个配置项来标记。最后,我们需要通过指定 `devtool` 来启用 webpack 的 source maps 功能,当前我们使用的是 'inline-source-map' 这个属性值。想了解更多关于这个属性值的特性和其他属性值选项,可以查看 [devtool 文档](https://webpack.js.org/configuration/devtool/)
143129

144130

145-
## Using 3rd Party Libraries
131+
## 使用第三方库
146132

147-
When installing 3rd party libraries from npm, it is important to remember
148-
to install the typing definition for that library.
133+
当从 npm 安装第三方库时,记住一定要同时安装这个库的类型声明文件。
149134

150-
You can install 3rd party library definitions from the @types repository.
135+
你可以从 @types 仓库找到并安装这些第三方库的类型声明文件。
151136

152-
For example if we want to install lodash we can run the following command to get the typings for it:
137+
举个例子,如果想安装 lodash 这个库的类型声明文件,我们可以运行下面的命令:
153138
`npm install --save-dev @types/lodash`
154139

155-
For more information see [this blog post](https://blogs.msdn.microsoft.com/typescript/2016/06/15/the-future-of-declaration-files/)
140+
想了解更多,可以查看[这篇文章](https://blogs.msdn.microsoft.com/typescript/2016/06/15/the-future-of-declaration-files/)
156141

157-
## Importing non code assets
142+
## 导入非代码资源
158143

159-
To use non code assets with TypeScript, we need to tell TypeScript how to defer the type for these imports.
144+
要在 TypeScript 里使用非代码资源,我们需要告诉 TypeScript 如何兼容这些导入类型。
160145

161-
To do this we need to create a __custom.d.ts__ file.
162-
This file signifies custom definitions for TypeScript in our project.
146+
那么首先,我们需要在项目里创建 __custom.d.ts__ 文件,这个文件用来编写自定义的类型声明。
163147

164-
In our __custom.d.ts__ file we need to provide a definition for svg imports, to do this we need to put the following content in this file:
148+
我们要想兼容 svg 类型的资源导入,就需要在 __custom.d.ts__ 文件里添加以下内容:
165149

166150
```typescript
167151
declare module "*.svg" {
@@ -170,10 +154,9 @@ declare module "*.svg" {
170154
}
171155
```
172156

173-
Here we declare a new module for svg by specifying any import that ends in __.svg__ and define the type for this module as any.
174-
If we wanted to be more explicit about this being a url we could define the type as string.
157+
上面代码为 svg 声明了一个新模块,使得 TypeScript 能够识别到 以 __.svg__ 结尾的资源导入,同时定义了这个模块的类型为任意类型(any)。如果我们想指定更加明确模块类型,假如可以判断出这是一个 url,那么我们可以将类型定义为字符串。
175158

176-
This applies not only to svg but any custom loader you may want to use which includes css, scss, json or any other file you may wish to load in your project.
159+
这不仅适用于 svg,也适用于其他任何你想使用的自定义 loader,包括css,scss,json或是你希望加载到项目中的其他任何文件。
177160

178161
***
179162

0 commit comments

Comments
 (0)