-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ES Modules recipe #1593
Merged
Merged
Add ES Modules recipe #1593
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1a7c234
Create [email protected]
motss 64d31e0
Update [email protected]
motss cf7c347
Update [email protected]
motss be0d84f
Update [email protected]
motss b593506
Update [email protected]
motss f815af7
Update [email protected]
motss 347d8be
Update [email protected]
sindresorhus e530aaf
Update [email protected]
sindresorhus 3474d52
Merge branch 'master' into pr/1593
novemberborn 3f33a74
Focus recipe on @std/esm
novemberborn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,146 @@ | ||
# ES Modules | ||
|
||
As of Node.js 8.5.0, ES modules are natively supported, behind a command line option `--experimental-modules`. | ||
|
||
However, it works a bit different on Node.js than on web browsers that also natively supported ES Modules. In Node.js, you need to write `.mjs` files instead of commonly known `.js` files. The `.mjs` files do work on the web browsers but they have to be served with the correct Media Type (`text/javascript` or `application/javascript`). There is a [ongoing effort](https://tools.ietf.org/html/draft-bfarias-javascript-mjs-00) working on standardizing `.mjs`. | ||
|
||
Good news is that there's already a library named [@std/esm](https://github.com/standard-things/esm) that allows you to write and run ES modules with transpiling. It basically does everything the specs says to make ES modules work on Node.js 4.x and above. | ||
|
||
`ava` should be able to do right? Yes, it definitely can but it requires a bit more effort to make things work magically! | ||
|
||
## @std/esm | ||
|
||
First, install [@std/esm](https://github.com/standard-things/esm) first: | ||
|
||
```sh | ||
# Install @std/esm | ||
$ npm install --save @std/esm # or yarn add @std/esm | ||
``` | ||
|
||
Here's all the configuration that you need to write native ES modules with `ava`: | ||
|
||
```json | ||
// package.json | ||
|
||
{ | ||
... | ||
"scripts": { | ||
... | ||
"test": "ava" | ||
}, | ||
"dependencies": { | ||
... | ||
"@std/esm": "^0.15.0" | ||
}, | ||
"ava": { | ||
"require": [ | ||
"@std/esm" | ||
] | ||
}, | ||
"@std/esm": { | ||
"esm": "all", | ||
"cjs": true, | ||
"await": true, | ||
"gz": true | ||
} | ||
... | ||
} | ||
``` | ||
|
||
That's basically it. You can now write native ES modules with `ava`. | ||
|
||
```js | ||
// test/index.js | ||
|
||
import test from 'ava'; | ||
|
||
test('2 + 2 = 4', async (t) => { | ||
try { | ||
t.true(2 + 2 === 4); | ||
} catch (e) { | ||
t.fail(); | ||
} | ||
}); | ||
``` | ||
|
||
## @std/esm + Typescript | ||
|
||
For [TypeScript](https://github.com/Microsoft/TypeScript) users, you can do that too! Let's dive in. | ||
|
||
First install `typescript`. | ||
|
||
```sh | ||
# Install typescript | ||
$ npm install --save-dev typescript # or yarn add -D typescript | ||
``` | ||
|
||
To use native ES modules, please modify `tsconfig.json` accordingly. | ||
|
||
```json | ||
// tsconfig.json | ||
|
||
{ | ||
... | ||
"moduleResolution": "node", | ||
"module": "es2015", // or "esnext" | ||
"target": "es2015" // or "esnext" | ||
... | ||
} | ||
``` | ||
|
||
Then add `@std/esm` configuration into `package.json`. | ||
|
||
```json | ||
// package.json | ||
|
||
{ | ||
... | ||
"scripts": { | ||
... | ||
"test": "tsc && ava" | ||
}, | ||
"dependencies": { | ||
... | ||
"@std/esm": "^0.15.0" | ||
}, | ||
"devDependencies": { | ||
"typescript": "^2.6.1" | ||
}, | ||
"ava": { | ||
"require": [ | ||
"@std/esm" | ||
], | ||
}, | ||
"@std/esm": { | ||
"esm": "all", | ||
"cjs": true, | ||
"await": true, | ||
"gz": true | ||
} | ||
... | ||
} | ||
``` | ||
|
||
Effortless writing test scripts with `ava` + `@std/esm` + `TypeScript` is done! | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👆 So rad! |
||
```ts | ||
// test/index.ts | ||
|
||
// @ts-check | ||
|
||
import test from 'ava'; | ||
|
||
test('2 + 2 = 4', async (ts) => { | ||
try { | ||
t.true(2 + 2 === 4); | ||
} catch (e) { | ||
t.fail(); | ||
} | ||
}); | ||
``` | ||
|
||
## Execute test with ava | ||
|
||
```sh | ||
$ npm run test # or npm t | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👆 I don't believe all those options are needed. Can you try snipping it down to something like:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jdalton Sure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sweet 🍬 !