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 naive import syntax support #2

Merged
merged 1 commit into from
Feb 13, 2020

Conversation

ctavan
Copy link
Contributor

@ctavan ctavan commented Feb 13, 2020

Given running esmodules in a node vm to truly evaluate code snippets as
esmodules is still experimental and pretty low level
(https://nodejs.org/docs/latest-v12.x/api/vm.html#vm_class_vm_module)
this hack might work in many cases already.

Demo:

console.log(
`
import { v1 as uuidv1 } from 'uuid';
import { v1 } from 'uuid';
import uuid from 'uuid';
import {
  v1 as uuidv1
} from 'uuid';
`
.replace(
  /import\s*\{\s*([^\s]+)\s+as\s+([^\s]+)\s*\}\s*from\s*['"]([^']+)['"]/g,
  'const { $1: $2 } = require("$3")',
)
.replace(
  /import\s*\{\s*([^\s]+)\s*\}\s*from\s*['"]([^']+)['"]/g,
  'const { $1 } = require("$2")',
)
.replace(
  /import\s([^\s]+)\sfrom\s*['"]([^']+)['"]/g,
  'const $1 = require("$2")',
)
);

Output:

const { v1: uuidv1 } = require("uuid");
const { v1 } = require("uuid");
const uuid = require("uuid");
const { v1: uuidv1 } = require("uuid");

Given running esmodules in a node vm to truly evaluate code snippets as
esmodules is still experimental and pretty low level
(https://nodejs.org/docs/latest-v12.x/api/vm.html#vm_class_vm_module)
this hack might work in many cases already.

Demo:

```javascript
console.log(
`
import { v1 as uuidv1 } from 'uuid';
import { v1 } from 'uuid';
import uuid from 'uuid';
import {
  v1 as uuidv1
} from 'uuid';
`
.replace(
  /import\s*\{\s*([^\s]+)\s+as\s+([^\s]+)\s*\}\s*from\s*['"]([^']+)['"]/g,
  'const { $1: $2 } = require("$3")',
)
.replace(
  /import\s*\{\s*([^\s]+)\s*\}\s*from\s*['"]([^']+)['"]/g,
  'const { $1 } = require("$2")',
)
.replace(
  /import\s([^\s]+)\sfrom\s*['"]([^']+)['"]/g,
  'const $1 = require("$2")',
)
);
```

Output:

```
const { v1: uuidv1 } = require("uuid");
const { v1 } = require("uuid");
const uuid = require("uuid");
const { v1: uuidv1 } = require("uuid");
```
@ctavan ctavan requested a review from broofa February 13, 2020 14:22
@broofa broofa merged commit 0556a22 into broofa:master Feb 13, 2020
@broofa
Copy link
Owner

broofa commented Feb 13, 2020

Woot! :shipit:

@ctavan
Copy link
Contributor Author

ctavan commented Feb 13, 2020

BTW I also tried to get runmd to work in two different wais:

  1. With true esmodule support / https://nodejs.org/docs/latest-v12.x/api/vm.html#vm_class_vm_module but I just couldn't wrap my head around it. Also it still feels very bleeding edge.
  2. By bable-ing the source. That however didn't work well because you always have to bable full files in order to transform import statements… Since runmd executes code block-by-block that didn't work well.

After 3h I gave up and decided to go with this hacky workaround for now.

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.

2 participants