-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Use ESM #1621
Conversation
This pull request is being automatically deployed with Vercel (learn more). 🔍 Inspect: https://vercel.com/mdx/mdx/5CJ94n8dabCW7aGRbychj2ouQ4G9 |
.eslintrc.yml
Outdated
import: | ||
resolver: | ||
typescript: null | ||
import/resolver: | ||
typescript: | ||
project: packages/*/types |
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.
So eslint-import-resolver-typescript can find @mdx-js/mdx.Options
:
✘ https://google.com/#q=import%2Fnamed
Options not found in '@mdx-js/mdx'
packages/runtime/types/index.d.ts:4:9
2 |
3 | import {FunctionComponent} from 'react'
> 4 | import {Options} from '@mdx-js/mdx'
| ^
5 | import {ComponentsProp} from '@mdx-js/react'
6 |
7 | /**
"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", |
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.
--experimental-vm-modules
for tests written in ESM.
"webpack": "^4.0.0" | ||
"webpack": "^5.51.1" |
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.
Upgrade to webpack 5 for webpack/loader-runner#40, although it's still missing webpack/webpack#14077.
context: __dirname, | ||
context: fileURLToPath(new URL('.', import.meta.url)), |
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.
// Webpack 5: | ||
// resolve( | ||
// {source: fs.readFileSync(path.join(__dirname, '..', 'dist', 'main.js'), 'utf8')} | ||
// ) | ||
resolve(stats.toJson().modules.find(m => m.name === filePath)) | ||
resolve({ | ||
source: fs.readFileSync( | ||
new URL('../dist/main.js', import.meta.url), | ||
'utf8' | ||
) | ||
}) |
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.
Upgrade to webpack 5 with the help of the existing comments.
@@ -2,66 +2,39 @@ | |||
|
|||
import {Plugin, Compiler, Processor} from 'unified' | |||
|
|||
declare namespace mdx { |
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.
Update types correspondingly.
bundler.addAssetType('mdx', require.resolve('./MDXAsset.js')) | ||
import {createRequire} from 'module' | ||
|
||
export default function (bundler) { | ||
bundler.addAssetType( | ||
'mdx', | ||
createRequire(import.meta.url).resolve('./MDXAsset.js') |
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.
createRequire()
works, until Jest supports import.meta.resolve()
.
"main": "dist/mdx-react.js", | ||
"main": "dist/mdx-react.cjs", |
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.
"type": "module"
implies that dist/mdx-react.js
is ESM. dist/mdx-react.cjs
is identical but explicitly CJS. This avoids:
FAIL test/index.test.js
● Test suite failed to run
SyntaxError: The requested module '@mdx-js/react' does not provide an export named 'MDXProvider'
at Runtime.linkAndEvaluateModule (../../node_modules/jest-runtime/build/index.js:669:5)
at TestScheduler.scheduleTests (../../node_modules/@jest/core/build/TestScheduler.js:333:13)
at runJest (../../node_modules/@jest/core/build/runJest.js:387:19)
at _run10000 (../../node_modules/@jest/core/build/cli/index.js:408:7)
at runCLI (../../node_modules/@jest/core/build/cli/index.js:261:3)
|
||
// See `loader`’s tests for how to upgrade these to webpack 5. |
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.
Upgrade to webpack 5, same as packages/loader
.
FYI, current CI failure is pending jestjs/jest#11790. |
Thanks @jablko! A related project, XDM, has ESM support https://github.com/wooorm/xdm you may want to check it out. There's been discussion of MDX and XDM merging back together, which would likely bring ESM support into MDX. In the meantime, could you expand a bit on what features/fixes from hast-util-to-estree v2 you are looking for? |
Thanks for your work on this Jack. |
This is fantastic and exciting, many thanks for your tremendous work! |
I think this is a prerequisite of upgrading to hast-util-to-estree v2? Otherwise I get the following error, because hast-util-to-estree is pure ESM, which
require()
doesn't support:To replace
require()
withimport
I ran the following, plus manual fix-ups:Depends on jestjs/jest#11790
Depends on webpack/webpack#14077