Skip to content

Commit fb350d1

Browse files
committed
prefer es6 import over require
1 parent b57c29c commit fb350d1

File tree

4 files changed

+19
-21
lines changed

4 files changed

+19
-21
lines changed

content/concepts/module-resolution.md

Lines changed: 9 additions & 9 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
@@ -24,26 +24,26 @@ The dependency module can be from the application code or a third party library.
2424
* 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

3333
* 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

4242
* 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-
* A JavaScript `require()` statement
2221
* An ECMAScript2015 `import` statement
22+
* A JavaScript `require()` 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/resolve.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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/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)