-
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 all 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
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,47 @@ | ||
# Using ES modules in AVA | ||
|
||
As of Node.js 8.5.0, [ES modules](http://2ality.com/2017/09/native-esm-node.html) are natively supported, but behind the `--experimental-modules` command line flag. It works using the `.mjs` file extension. AVA does not currently support the command line option or the new file extension, but you *can* use the [`@std/esm`](https://github.com/standard-things/esm) module to use the new syntax. | ||
|
||
Here's how you get it working with AVA. | ||
|
||
First, install [`@std/esm`](https://github.com/standard-things/esm): | ||
|
||
``` | ||
$ npm install @std/esm | ||
``` | ||
|
||
Configure it in your `package.json` file, and add it to AVA's `"require"` option as well. Make sure to add it as the first item: | ||
|
||
```json | ||
{ | ||
"ava": { | ||
"require": [ | ||
"@std/esm" | ||
] | ||
}, | ||
"@std/esm": "js" | ||
} | ||
``` | ||
|
||
By default AVA converts ES module syntax to CommonJS. [You can disable this](./babel.md#preserve-es-module-syntax). | ||
|
||
You can now use native ES modules with AVA: | ||
|
||
```js | ||
// sum.mjs | ||
export default function sum(a, b) { | ||
return a + b; | ||
}; | ||
``` | ||
|
||
```js | ||
// test.js | ||
import test from 'ava'; | ||
import sum from './sum.mjs'; | ||
|
||
test('2 + 2 = 4', t => { | ||
t.is(sum(2, 2), 4); | ||
}); | ||
``` | ||
|
||
Note that test files still need to use the `.js` extension. |
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
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.
You don't need to prefix the file path with
./
. It works without too.