Skip to content

Commit 446b1c4

Browse files
authored
Merge pull request #311 from webpack/feature/es6
prefer es6 import over require
2 parents 3e3eb2b + d0b20f8 commit 446b1c4

File tree

9 files changed

+34
-36
lines changed

9 files changed

+34
-36
lines changed

content/api/node.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Then require the webpack module in your Node.js script:
2020
``` js
2121
const webpack = require("webpack");
2222

23-
// Or if you prefer ES6:
23+
// Or if you prefer ES2015:
2424
import webpack from "webpack";
2525
```
2626

content/concepts/module-resolution.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ A resolver is a library which helps find the absolute path of a module.
88
A module can be required as a dependency from another module as
99

1010
```js
11-
require('/path/to/module')
12-
//or
13-
import mymodule from '/path/to/module'
11+
import mymodule from 'path/to/module'
12+
// or
13+
require('path/to/module')
1414
```
1515

1616
The dependency module can be from the application code or a third party library. The resolver helps
@@ -21,29 +21,29 @@ The dependency module can be from the application code or a third party library.
2121

2222
`webpack` resolves three kinds of file paths
2323

24-
* Absolute paths
24+
### Absolute paths
2525

2626
```js
27-
require("/home/me/file");
28-
require("C:\\Home\\me\\file");
27+
import "/home/me/file";
28+
import "C:\\Users\\me\\file";
2929
```
3030

3131
Since we already have the absolute path to the file, no further resolution is required.
3232

33-
* Relative paths
33+
### Relative paths
3434

3535
```js
36-
require("../src/file");
37-
require("./file");
36+
import "../src/file";
37+
import "./file";
3838
```
3939

4040
In this case, the directory of the resource file is taken to be the context directory (the directory of the currently processed file). The given relative path is joined to the context path to produce the absolute path to the file.
4141

42-
* Module path
42+
### Module path
4343

4444
```js
45-
require("module");
46-
require("module/lib/file");
45+
import "module";
46+
import "module/lib/file";
4747
```
4848

4949
Modules are searched for inside directories which are specified using `resolve.modules`, which is can be an array comprising of different paths.

content/concepts/modules.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ webpack builds on lessons learned from these systems and applies the concept of
1818

1919
In contrast to Node.js modules, webpack _modules_ can express their _dependencies_ in a variety of ways. A few examples are:
2020

21+
* An ES2015 `import` statement
2122
* A JavaScript `require()` statement
22-
* An ECMAScript2015 `import` statement
2323
* An AMD `define` and `require` statement
2424
* An `@import` statement inside of a css/sass/less file.
2525
* An image url in a stylesheet (`url(...)`) or html (`<img src=...>`) file.

content/configuration/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ T> Notice that throughout the configuration we use Node's built-in [path module]
5050
// the name of the exported library
5151
5252
<details><summary>[libraryTarget](/configuration/output#output-librarytarget): "umd", // enum</summary>
53-
[libraryTarget](/configuration/output#output-librarytarget): "umd-module", // ES6 module wrapped in UMD
54-
[libraryTarget](/configuration/output#output-librarytarget): "commonjs-module", // ES6 module wrapped in CommonJs
53+
[libraryTarget](/configuration/output#output-librarytarget): "umd-module", // ES2015 module wrapped in UMD
54+
[libraryTarget](/configuration/output#output-librarytarget): "commonjs-module", // ES2015 module wrapped in CommonJs
5555
[libraryTarget](/configuration/output#output-librarytarget): "commonjs2", // exported with module.exports
5656
[libraryTarget](/configuration/output#output-librarytarget): "commonjs", // exported as properties to exports
5757
[libraryTarget](/configuration/output#output-librarytarget): "amd", // defined with AMD defined method

content/configuration/output.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ Configure how the library will be exposed. Any one of the following options can
225225

226226
`libraryTarget: "commonjs2"` - Expose it using the `module.exports` object (`output.library` is ignored)
227227

228-
`libraryTarget: "commonjs-module"` - Expose it using the `module.exports` object (`output.library` is ignored), `__esModule` is defined (it's threaded as ES6 Module in interop mode)
228+
`libraryTarget: "commonjs-module"` - Expose it using the `module.exports` object (`output.library` is ignored), `__esModule` is defined (it's threaded as ES2015 Module in interop mode)
229229

230230
`libraryTarget: "this"` - Expose it as a property of `this` (i.e. `this["MyLibrary"] = ...`)
231231

content/configuration/resolve.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Have a look at [Module Resolution](/concepts/module-resolution) for more explana
1414

1515
`object`
1616

17-
Configure how modules are resolved. For example, when calling `import "lodash"` in ES6, the `resolve` options can change where webpack goes to look for `"lodash"` (see [`modules`](#resolve-modules)).
17+
Configure how modules are resolved. For example, when calling `import "lodash"` in ES2015, the `resolve` options can change where webpack goes to look for `"lodash"` (see [`modules`](#resolve-modules)).
1818

1919
## `resolve.alias`
2020

@@ -56,7 +56,7 @@ import Test1 from 'xyz'; // Success, file.js is resolved and imported
5656
import Test2 from 'xyz/file.js'; // Error, /path/to/file.js/file.js is invalid
5757
```
5858

59-
| `alias:` | `require("xyz")` | `require("xyz/file.js")` |
59+
| `alias:` | `import "xyz"` | `import "xyz/file.js"` |
6060
| -------- | ---------------- | -------------------------|
6161
| `{}` | `/abc/node_modules/xyz/index.js` | `/abc/node_modules/xyz/file.js` |
6262
| `{ xyz: "/abs/path/to/file.js" }` | `/abs/path/to/file.js` | error |

content/get-started/why-webpack.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ webpack however is not the only module bundler out there. If you are choosing be
5555
| Preprocessing | **loaders, [transforms](https://github.com/webpack/transform-loader)** | loaders | transforms | plugin translate | plugin transforms | compilers, optimizers |
5656
| Replacement for browser | `web_modules`, `.web.js`, package.json field, alias config option | alias option | package.json field, alias option | package.json, alias option | no |
5757
| Requirable files | file system | **web** | file system | through plugins | file system or through plugins | file system |
58-
| Runtime overhead | **243B + 20B per module + 4B per dependency** | 14.7kB + 0B per module + (3B + X) per dependency | 415B + 25B per module + (6B + 2X) per dependency | 5.5kB for self-executing bundles, 38kB for full loader and polyfill, 0 plain modules, 293B CJS, 139B ES6 System.register before gzip | **none for ES2015 modules** (other formats may have)
58+
| Runtime overhead | **243B + 20B per module + 4B per dependency** | 14.7kB + 0B per module + (3B + X) per dependency | 415B + 25B per module + (6B + 2X) per dependency | 5.5kB for self-executing bundles, 38kB for full loader and polyfill, 0 plain modules, 293B CJS, 139B ES2015 System.register before gzip | **none for ES2015 modules** (other formats may have)
5959
| Watch mode | yes | not required | yes | not needed in dev | no | yes |
6060

6161
♦ in production mode (opposite in development mode)

content/how-to/upgrade-from-webpack-1.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ plugins: [
166166

167167
A dependency with only an expression (i. e. `require(expr)`) will now create an empty context instead of an context of the complete directory.
168168

169-
Best refactor this code as it won't work with ES6 Modules. If this is not possible you can use the `ContextReplacementPlugin` to hint the compiler to the correct resolving.
169+
Best refactor this code as it won't work with ES2015 Modules. If this is not possible you can use the `ContextReplacementPlugin` to hint the compiler to the correct resolving.
170170

171171
?> Link to an article about dynamic dependencies.
172172

@@ -235,7 +235,7 @@ To keep compatibility with old loaders, loaders can be switched to debug mode vi
235235
]
236236
```
237237

238-
### Code Splitting with ES6
238+
### Code Splitting with ES2015
239239

240240
In webpack v1, you could use `require.ensure` as a method to lazily-load chunks for your application:
241241

@@ -245,7 +245,7 @@ require.ensure([], function(require) {
245245
});
246246
```
247247

248-
The ES6 Loader spec defines `System.import` as method to load ES6 Modules dynamically on runtime.
248+
The ES2015 Loader spec defines `System.import` as method to load ES2015 Modules dynamically on runtime.
249249

250250
webpack treats `System.import` as a split-point and puts the requested module in a separate chunk.
251251

@@ -285,12 +285,12 @@ function route(path, query) {
285285
// This creates a separate chunk for each possible route
286286
```
287287

288-
### Mixing ES6 with AMD and CommonJS
288+
### Mixing ES2015 with AMD and CommonJS
289289

290290
As for AMD and CommonJS you can freely mix all three module types (even within the same file). Webpack behaves similar to babel in this case:
291291

292292
```javascript
293-
// CommonJS consuming ES6 Module
293+
// CommonJS consuming ES2015 Module
294294
var book = require("./book");
295295

296296
book.currentPage;
@@ -299,7 +299,7 @@ book.default === "This is a book";
299299
```
300300

301301
```javascript
302-
// ES6 Module consuming CommonJS
302+
// ES2015 Module consuming CommonJS
303303
import fs from "fs"; // module.exports map to default
304304
import { readFileSync } from "fs"; // named exports are read from returned object+
305305

content/index.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
## Write your code.
22

3-
**index.js**
3+
**app.js**
44

55
```js
6-
const foo = require('./foo.js')
6+
import bar from './foo';
77

8-
foo.bar()
8+
bar():
99
```
1010

1111
**foo.js**
1212

1313
```js
14-
module.exports = {
15-
bar: function () {
16-
//
17-
}
14+
export function bar() {
15+
//
1816
}
1917
```
2018

@@ -24,9 +22,9 @@ module.exports = {
2422

2523
```js
2624
module.exports = {
27-
entry: './index.js',
25+
entry: './app.js',
2826
output: {
29-
path: 'bundle.js'
27+
filename: 'bundle.js'
3028
}
3129
}
3230
```
@@ -40,7 +38,7 @@ module.exports = {
4038
</head>
4139
<body>
4240
...
43-
<script src="/bundle.js"></script>
41+
<script src="bundle.js"></script>
4442
</body>
4543
</html>
4644
```

0 commit comments

Comments
 (0)