Skip to content

Commit

Permalink
Add support for ES modules
Browse files Browse the repository at this point in the history
Closes GH-13.
  • Loading branch information
wooorm committed Jun 24, 2019
1 parent 08b9409 commit 66e31c2
Show file tree
Hide file tree
Showing 17 changed files with 79 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/generate/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function config(ctx) {
main = resolveFrom(mainRoot, mainId)
} catch (error) {}

ctx.experimentalModules = settings.experimentalModules || false
ctx.cwd = cwd
ctx.main = main
ctx.name = settings.name || pkg.name || null
Expand Down
3 changes: 3 additions & 0 deletions lib/generate/instrument.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ function instrument(ctx, next) {

return {
visitor: {
ImportDeclaration: function(path) {
instrumentMainReference(path.node.source)
},
CallExpression: function(path) {
var callee = path.get('callee')

Expand Down
10 changes: 9 additions & 1 deletion lib/generate/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ var exec = require('child_process').exec
module.exports = run

function run(ctx, next) {
exec('node ' + ctx.exampleInstrumentedPath, onexec)
var cmd = [process.execPath]

if (ctx.experimentalModules) {
cmd.push('--experimental-modules')
}

cmd.push(ctx.exampleInstrumentedPath)

exec(cmd.join(' '), onexec)

function onexec(err, stdout) {
var logs = ctx.logs
Expand Down
3 changes: 2 additions & 1 deletion lib/generate/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ module.exports = write

function write(ctx, next) {
var dir = path.dirname(ctx.examplePath)
var filePath = path.join(dir, ctx.id + '.js')
var extname = path.extname(ctx.examplePath)
var filePath = path.join(dir, ctx.id + extname)

fs.writeFile(filePath, ctx.exampleInstrumented, onwrite)

Expand Down
9 changes: 9 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ If given, resolved from [`file.cwd`][file-cwd].
If inferred from `package.json`, resolved relating to that package root.
Used to rewrite `require('.')` to `require('name')`.

###### `options.experimentalModules`

Pass [`--experimental-modules`][experimental-modules] when running the example
in Node.
This lets you use ECMAScript Modules if the current version of Node does support
this flag, but *does not* support ES modules natively.

## Contribute

See [`contributing.md`][contributing] in [`remarkjs/.github`][health] for ways
Expand Down Expand Up @@ -213,6 +220,8 @@ abide by its terms.

[file-cwd]: https://github.com/vfile/vfile#vfilecwd

[experimental-modules]: https://nodejs.org/api/esm.html

[usage]: #use

[example-js]: example.js
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/es-module-mjs/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"experimentalModules": true
}
4 changes: 4 additions & 0 deletions test/fixtures/es-module-mjs/example.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { pi } from "./index.mjs";

// `pi` yields:
console.log('text', pi)
1 change: 1 addition & 0 deletions test/fixtures/es-module-mjs/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const pi = Math.floor(Math.PI * 1e6) / 1e6
13 changes: 13 additions & 0 deletions test/fixtures/es-module-mjs/output.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# PI

## Usage

```javascript
import { pi } from "pi";
```

`pi` yields:

```text
3.141592
```
3 changes: 3 additions & 0 deletions test/fixtures/es-module-mjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "pi"
}
3 changes: 3 additions & 0 deletions test/fixtures/es-module-mjs/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# PI

## Usage
3 changes: 3 additions & 0 deletions test/fixtures/es-module-type-module/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"experimentalModules": true
}
4 changes: 4 additions & 0 deletions test/fixtures/es-module-type-module/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { pi } from "./index.js";

// `pi` yields:
console.log('text', pi)
1 change: 1 addition & 0 deletions test/fixtures/es-module-type-module/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const pi = Math.floor(Math.PI * 1e6) / 1e6
13 changes: 13 additions & 0 deletions test/fixtures/es-module-type-module/output.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# PI

## Usage

```javascript
import { pi } from "pi";
```

`pi` yields:

```text
3.141592
```
4 changes: 4 additions & 0 deletions test/fixtures/es-module-type-module/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "pi",
"type": "module"
}
3 changes: 3 additions & 0 deletions test/fixtures/es-module-type-module/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# PI

## Usage

0 comments on commit 66e31c2

Please sign in to comment.