-
-
Notifications
You must be signed in to change notification settings - Fork 358
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
535 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,91 @@ | ||
# Prefer ES modules over CommonJS. | ||
# Prefer ES modules over CommonJS | ||
|
||
<!-- More detailed description. Remove this comment. --> | ||
Prefer use ESM over legacy CommonJS module. | ||
|
||
1. Forbid `use strict` directive. | ||
|
||
ESM scripts use Strict Mode by default. | ||
|
||
1. Forbid `Global Return` | ||
|
||
This is a CommonJS-only feature. | ||
|
||
1. Forbid global variables `__dirname` and `__filename`. | ||
|
||
It's not available in ESM. | ||
|
||
1. Forbid `require(…)` | ||
|
||
`import …` is preferred in ESM. | ||
|
||
1. Forbid `exports` and `module.exports` | ||
|
||
`export …` is preferred in ESM. | ||
|
||
This rule is fixable. | ||
|
||
## Fail | ||
|
||
```js | ||
const foo = 'unicorn'; | ||
'use strict'; | ||
|
||
// … | ||
``` | ||
|
||
```js | ||
if (foo) { | ||
return; | ||
} | ||
|
||
// … | ||
``` | ||
|
||
```js | ||
const file = path.join(__dirname, 'foo.js'); | ||
``` | ||
|
||
```js | ||
const {fromPairs} = require('lodash'); | ||
``` | ||
|
||
```js | ||
module.exports = foo; | ||
``` | ||
|
||
```js | ||
module.exports.foo = foo; | ||
``` | ||
|
||
## Pass | ||
|
||
|
||
```js | ||
function run() { | ||
if (foo) { | ||
return; | ||
} | ||
|
||
// … | ||
} | ||
|
||
run(); | ||
``` | ||
|
||
```js | ||
const foo = '🦄'; | ||
const file = fileURLToPath(new URL('foo.js', import.meta.url)); | ||
``` | ||
```js | ||
import {fromPairs} from 'lodash-es'; | ||
``` | ||
```js | ||
export default foo; | ||
``` | ||
```js | ||
export {foo}; | ||
``` | ||
## Resources | ||
- [Get Ready For ESM](https://blog.sindresorhus.com/get-ready-for-esm-aa53530b3f77) by @sindresorhus |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.