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

Use ESM #1621

Closed
wants to merge 2 commits into from
Closed

Use ESM #1621

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,8 @@ overrides:
- '*.tsx'
extends: 'plugin:@typescript-eslint/recommended'
settings:
import:
resolver:
typescript: null
import/resolver:
typescript: null

- files:
- '*.d.ts'
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ jobs:
run: yarn --frozen-lockfile
env:
CI: 'true'
- run: curl https://patch-diff.githubusercontent.com/raw/webpack/webpack/pull/14077.patch | patch --directory packages/loader/node_modules/webpack --strip 1
- name: Test
run: yarn test
18 changes: 9 additions & 9 deletions docs/advanced/api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ You should use the async API, unless you have very good reasons to use the
You can use the library directly:

```js
var mdx = require('@mdx-js/mdx')
import {mdx} from '@mdx-js/mdx'

const content = `
# Hello, world!
Expand Down Expand Up @@ -107,8 +107,8 @@ string synchronously.
It’s important to note that if you have any async plugins, they will be ignored.

```js
const fs = require('fs')
const mdx = require('@mdx-js/mdx')
import fs from 'fs'
import {mdx} from '@mdx-js/mdx'

const mdxText = fs.readFileSync('hello.mdx', 'utf8')

Expand All @@ -123,12 +123,12 @@ If you want to extract data from a MDX file, you can access the
compiler directly:

```js
const {createCompiler} = require('@mdx-js/mdx')
const detectFrontmatter = require('remark-frontmatter')
const vfile = require('vfile')
const visit = require('unist-util-visit')
const remove = require('unist-util-remove')
const yaml = require('yaml')
import {createCompiler} from '@mdx-js/mdx'
import detectFrontmatter from 'remark-frontmatter'
import vfile from 'vfile'
import visit from 'unist-util-visit'
import remove from 'unist-util-remove'
import yaml from 'yaml'

const file = vfile(
`
Expand Down
22 changes: 11 additions & 11 deletions docs/advanced/plugins.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ They are passed in the options to the core MDX library, but you’d typically pa
them through a loader like so:

```js
const images = require('remark-images')
const emoji = require('remark-emoji')
import images from 'remark-images'
import emoji from 'remark-emoji'

module.exports = {
export default {
module: {
rules: [
{
Expand All @@ -74,10 +74,10 @@ module.exports = {
If you’re using MDX directly, they can be passed like so:

```js
const fs = require('fs')
const mdx = require('@mdx-js/mdx')
const images = require('remark-images')
const emoji = require('remark-emoji')
import fs from 'fs'
import {mdx} from '@mdx-js/mdx'
import images from 'remark-images'
import emoji from 'remark-emoji'

const mdxText = fs.readFileSync('hello.mdx', 'utf8')
const jsx = mdx.sync(mdxText, {
Expand Down Expand Up @@ -114,10 +114,10 @@ Luckily, it’s possible to build a custom plugin that visits all text nodes wit
This works in most cases, and could look something like this:

```js
const visit = require('unist-util-visit')
const retext = require('retext')
const retextSmartypants = require('retext-smartypants')
const mdx = require('@mdx-js/mdx')
import visit from 'unist-util-visit'
import retext from 'retext'
import retextSmartypants from 'retext-smartypants'
import {mdx} from '@mdx-js/mdx'

function remarkSmartypants(options) {
const processor = retext().use(retextSmartypants, options)
Expand Down
7 changes: 3 additions & 4 deletions docs/advanced/transform-content.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ Content.
And our script, `example.js`, looks as follows:

```js
const {read, write} = require('to-vfile')
const remark = require('remark')
const mdx = require('remark-mdx')

import {read, write} from 'to-vfile'
import remark from 'remark'
import mdx from 'remark-mdx'
;(async () => {
const path = './example.mdx'
const file = await read(path)
Expand Down
10 changes: 5 additions & 5 deletions docs/getting-started/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -421,11 +421,11 @@ custom use case, you can of course do it yourself.
The below rendering function is what we use for our MDX integration tests:

```js
const babel = require('@babel/core')
const React = require('react')
const {renderToStaticMarkup} = require('react-dom/server')
const mdx = require('@mdx-js/mdx')
const {MDXProvider, mdx: createElement} = require('@mdx-js/react')
import babel from '@babel/core'
import React from 'react'
import {renderToStaticMarkup} from 'react-dom/server'
import {mdx} from '@mdx-js/mdx'
import {MDXProvider, mdx as createElement} from '@mdx-js/react'

const transform = code =>
babel.transform(code, {
Expand Down
10 changes: 5 additions & 5 deletions docs/getting-started/webpack.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ For webpack projects you can define the following `webpack.config.js` extension
handler for `.md` and `.mdx` files:

```js
module.exports = {
export default {
module: {
// ...

Expand Down Expand Up @@ -49,9 +49,9 @@ If you’d like to configure [plugins](/advanced/plugins) you can do that by
passing them as options:

```js
const images = require('remark-images')
const emoji = require('remark-emoji')
module.exports = {
import images from 'remark-images'
import emoji from 'remark-emoji'
export default {
module: {
rules: [
{
Expand Down Expand Up @@ -79,7 +79,7 @@ If you’re running MDX in the browser you will need to configure webpack to ign
the `fs` module:

```js
module.exports = {
export default {
node: {
fs: 'empty'
}
Expand Down
8 changes: 4 additions & 4 deletions docs/guides/custom-loader.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ Consider a scenario where you wanted to add frontmatter support to all your MDX

```js
// lib/fm-loader.js
const matter = require('gray-matter')
const stringifyObject = require('stringify-object')
import matter from 'gray-matter'
import stringifyObject from 'stringify-object'

module.exports = async function (src) {
export default async function (src) {
const callback = this.async()
const {content, data} = matter(src)

Expand All @@ -26,7 +26,7 @@ The loader code above parses out the frontmatter, exports it as the named export
Then, you can use it with the following config:

```js
module.exports = {
export default {
module: {
rules: [
{
Expand Down
4 changes: 2 additions & 2 deletions docs/guides/syntax-highlighting.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ ecosystem. One solid library for syntax highlighting is

```js
// webpack.config.js
const rehypePrism = require('@mapbox/rehype-prism')
import rehypePrism from '@mapbox/rehype-prism'

module.exports = {
export default {
module: {
// ...
rules: [
Expand Down
16 changes: 8 additions & 8 deletions docs/guides/terminal.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ It’s important to ensure that the MDXProvider wraps the MDX runtime
component.

```js
const React = require('react')
const {Text, render} = require('ink')
const {MDXProvider} = require('@mdx-js/react')
const MDX = require('@mdx-js/runtime')
import React from 'react'
import {Text, render} from 'ink'
import {MDXProvider} from '@mdx-js/react'
import MDX from '@mdx-js/runtime'

const components = {
h1: ({children}) => <Text bold>{children}</Text>,
Expand Down Expand Up @@ -83,10 +83,10 @@ From <Color bgBlack white bold> MDX! </Color>
## All together

```js
const React = require('react')
const {render, Color, Box, Text} = require('ink')
const MDX = require('@mdx-js/runtime')
const {MDXProvider} = require('@mdx-js/react')
import React from 'react'
import {render, Color, Box, Text} from 'ink'
import MDX from '@mdx-js/runtime'
import {MDXProvider} from '@mdx-js/react'

const MDXContent = `
# Hello, world!
Expand Down
14 changes: 7 additions & 7 deletions docs/guides/writing-a-plugin.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ for you.
Then you can first log out the nodes to see it in action:

```js
const visit = require('unist-util-visit')
import visit from 'unist-util-visit'

module.exports = () => (tree, file) => {
export default () => (tree, file) => {
visit(tree, 'heading', node => {
console.log(node)
})
Expand All @@ -34,9 +34,9 @@ text there is also an emphasis node.
To walk the text nodes, you can use `unist-util-visit` again:

```js
const visit = require('unist-util-visit')
import visit from 'unist-util-visit'

module.exports = () => (tree, file) => {
export default () => (tree, file) => {
visit(tree, 'heading', node => {
visit(node, 'text', textNode => {
console.log(textNode)
Expand All @@ -51,10 +51,10 @@ Now that you are logging out the text nodes, you can now manipulate them with th
to the node:

```js
const title = require('title')
const visit = require('unist-util-visit')
import title from 'title'
import visit from 'unist-util-visit'

module.exports = () => (tree, file) => {
export default () => (tree, file) => {
visit(tree, 'heading', node => {
visit(node, 'text', textNode => {
const text = textNode.value ? textNode.value.trim() : ''
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"gatsby": "^2.24.3",
"gatsby-theme-mdx1": "npm:gatsby-theme-mdx@^1.0.0",
"husky": "^4.3.0",
"jest": "^26.6.0",
"jest": "^27.1.0",
"lerna": "^3.22.1",
"lint-staged": "^10.5.0",
"patch-package": "^6.4.7",
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-theme-mdx/src/components/code-preview.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, {useContext} from 'react'
import {LiveProvider, LivePreview, LiveEditor, LiveError} from 'react-live'
import mdx from '@mdx-js/mdx'
import {mdx} from '@mdx-js/mdx'
import {MDXProvider, mdx as createElement} from '@mdx-js/react'
import * as Rebass from '@rebass/emotion'
import {ThemeContext} from '@emotion/core'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, {useContext, useState} from 'react'
import {LiveProvider, LivePreview, LiveEditor, LiveError} from 'react-live'
import mdx from '@mdx-js/mdx'
import {mdx} from '@mdx-js/mdx'
import {MDXProvider, mdx as createElement} from '@mdx-js/react'
import * as Rebass from '@rebass/emotion'
import {ThemeContext} from '@emotion/core'
Expand Down
6 changes: 3 additions & 3 deletions packages/loader/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const {getOptions} = require('loader-utils')
const {createCompiler} = require('@mdx-js/mdx')
import {getOptions} from 'loader-utils'
import {createCompiler} from '@mdx-js/mdx'

const DEFAULT_RENDERER = `
import React from 'react'
Expand All @@ -14,7 +14,7 @@ const pragma = `

const compilerCache = new Map()

module.exports = async function (content) {
export default async function (content) {
if (!compilerCache.has(this.query)) {
const {renderer = DEFAULT_RENDERER, ...opts} = getOptions(this)
compilerCache.set(this.query, [renderer, createCompiler(opts)])
Expand Down
8 changes: 4 additions & 4 deletions packages/loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
"loader"
],
"scripts": {
"test-api": "jest test",
"test-coverage": "jest test --coverage",
"test-api": "NODE_OPTIONS=--experimental-vm-modules jest test",
"test-coverage": "NODE_OPTIONS=--experimental-vm-modules jest test --coverage",
Comment on lines -38 to +39
Copy link
Contributor Author

Choose a reason for hiding this comment

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

--experimental-vm-modules for tests written in ESM.

"test-types": "dtslint types",
"test": "yarn test-coverage && yarn test-types"
},
Expand All @@ -49,11 +49,11 @@
"loader-utils": "^2.0.0"
},
"devDependencies": {
"memory-fs": "^0.5.0",
"react": "^17.0.0",
"react-dom": "^17.0.0",
"remark-slug": "^6.0.0",
"webpack": "^4.0.0"
"webpack": "^5.51.1"
Comment on lines -56 to +55
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Upgrade to webpack 5 for webpack/loader-runner#40, although it's still missing webpack/webpack#14077.

},
"type": "module",
"gitHead": "bf7deab69996449cb99c2217dff75e65855eb2c1"
}