Skip to content
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 10 commits into from
Feb 12, 2018
Merged

Add ES Modules recipe #1593

merged 10 commits into from
Feb 12, 2018

Conversation

motss
Copy link
Contributor

@motss motss commented Nov 17, 2017

@sindresorhus

To fix #1590

Recipe for ava + @std/esm + typescript

@motss motss changed the title Create [email protected] [DO NOT MERGE JUST YET] Create [email protected] Nov 17, 2017
@motss motss changed the title [DO NOT MERGE JUST YET] Create [email protected] Create [email protected] Nov 17, 2017
"cjs": true,
"await": true,
"gz": true
}
Copy link
Contributor

@jdalton jdalton Nov 17, 2017

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:

"@std/esm": "cjs"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jdalton Sure.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet 🍬 !

@sindresorhus
Copy link
Member

Thanks for starting this @motss. I've tweaked the text and simplified parts of it a little bit.

Can you also add it to the AVA readme: https://github.com/avajs/ava#recipes

@sindresorhus sindresorhus changed the title Create [email protected] Add ES Modules recipe Nov 19, 2017

However, it works a bit different on Node.js than in web browsers that also natively supports ES modules. In Node.js, you need to use the `.mjs` extension instead of the commonly known `.js` extension. The `.mjs` extension in web browsers too, but they have to be served with the correct media type (`application/javascript`). There is a [ongoing effort](https://tools.ietf.org/html/draft-bfarias-javascript-mjs-00) on standardizing `.mjs`.

The good news is that there's a module called [`@std/esm`](https://github.com/standard-things/esm) that enable you to write and run ES modules in Node.js 4 and above.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that enable -> that enables

```

You can now write tests with AVA, `@std/esm`, and TypeScript:

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👆 So rad!

```json
{
"moduleResolution": "node",
Copy link
Contributor

@jdalton jdalton Nov 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👆 is the ... in the json common in recipe docs? It makes a weird error highlight when using ```json

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just a way to signify that there should be more content than just this in the file. Any better suggestion?

@jdalton
Copy link
Contributor

jdalton commented Nov 19, 2017

Let me know if a specific usage pops out for AVA.
I wouldn't mind making @std/esm detect AVA and applying AVA specific defaults or something.

Copy link
Member

@novemberborn novemberborn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @motss!

Some minor text comments (perhaps those were @sindresorhus' changes). Would be neat if this could show an example of using .mjs files, too.


As of Node.js 8.5.0, [ES modules](http://2ality.com/2017/09/native-esm-node.html) are natively supported, but behind a command line option `--experimental-modules`.

However, it works a bit different on Node.js than in web browsers that also natively supports ES modules. In Node.js, you need to use the `.mjs` extension instead of the commonly known `.js` extension. The `.mjs` extension in web browsers too, but they have to be served with the correct media type (`application/javascript`). There is a [ongoing effort](https://tools.ietf.org/html/draft-bfarias-javascript-mjs-00) on standardizing `.mjs`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should say:

The .mjs extension can work in web browsers too, but such files have to be served with the correct media type

And:

There is an ongoing effort on standardizing .mjs.

(an rather than a)

```js
import test from 'ava';

test('2 + 2 = 4', t => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be nice if this would have an actual example of a .mjs file. Notably the test files are still transpiled using Babel, which means the above really reads const test = require('ava'), and there are no .mjs files being used. Same goes for the TypeScript example below.

@jdalton
Copy link
Contributor

jdalton commented Nov 20, 2017

I'm fine not dwelling on mjs. The recommended config is "@std/esm":"cjs", which enables cjs compat mode.

"test": "ava"
},
"dependencies": {
"@std/esm": "^0.16.0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Latest is 0.17.0 now. Is there a standard way to express a dependency without using a version for the recipe so folks don't lazy copy pasta an out of date version?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know… perhaps the recipe should say to use npm install and not show any package.json? That would make copying really hard.

@motss
Copy link
Contributor Author

motss commented Dec 14, 2017

@sindresorhus @jdalton Does the PR look good? Ready to merge?

@motss
Copy link
Contributor Author

motss commented Dec 24, 2017

See #1618

@sindresorhus
Copy link
Member

What do we do about this PR now that #1618 is merged?

@jdalton
Copy link
Contributor

jdalton commented Jan 14, 2018

The general advice is still solid. Maybe bump the version documented and run with it.

@feross
Copy link

feross commented Jan 15, 2018

You might want to update this PR to point out that .mjs files won't work with ava until #631 (comment) is fixed.

Running ava or ava test/**/*.mjs or ava test/my-test.mjs results in:

  1 exception

  ✖ Couldn't find any files to test

This was surprising to me since the documentation in this PR mentions the .mjs extension explicitly, implying that testing .mjs files would work!

@jdalton
Copy link
Contributor

jdalton commented Jan 17, 2018

The "@std/esm": "cjs" could technically be "@std/esm": "js" since with #1618 it would no longer need the cjs babel interop to be enabled.

novemberborn referenced this pull request Jan 30, 2018
* support @std/esm

* fix path test for windows

* fix linter error

* Stricter regex. Use shorthand .esmrc file in fixture.

* update package-lock.json
@novemberborn
Copy link
Member

I've pushed a rewrite that focuses this recipe on using @std/esm, with AVA 1.0 in mind.

@jdalton is the configuration OK, and the example accurate?

@novemberborn novemberborn dismissed their stale review February 11, 2018 17:46

I've pushed a rewrite that focuses this recipe on using @std/esm, with AVA 1.0 in mind.

@jdalton
Copy link
Contributor

jdalton commented Feb 11, 2018

Looks great! Let's roll with that configuration and loosen it if needed later.

}
```

By default AVA converts ES module syntax to CommonJS. [You can disable this](./babel.md#preserve-es-module-syntax).
Copy link
Member

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.

@novemberborn novemberborn merged commit 0b27db5 into avajs:master Feb 12, 2018
@novemberborn
Copy link
Member

Thanks all!

@motss
Copy link
Contributor Author

motss commented Feb 12, 2018

🎉 Thanks for merging the PR. Well done all! 👏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

HOW-TO: ES Modules with @std/esm
5 participants