Skip to content

Commit

Permalink
node-loader: remove fixRuntimeWithoutExportMap
Browse files Browse the repository at this point in the history
Previously, we by default fixed automatic JSX runtimes
without a valid export map.
That is because React 16 and 17 were broken.
All other JSX runtimes, such as Vue or Preact, were valid.
As of React 18, they now use a valid export map.
Meaning this workaround is no longer needed.
  • Loading branch information
wooorm committed Oct 18, 2023
1 parent 8eb11e2 commit 0f62bce
Show file tree
Hide file tree
Showing 7 changed files with 6 additions and 61 deletions.
2 changes: 1 addition & 1 deletion packages/node-loader/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @typedef {import('./lib/index.js').CompileOptions} Options
* @typedef {import('./lib/index.js').Options} Options
*/

import {createLoader} from './lib/index.js'
Expand Down
39 changes: 3 additions & 36 deletions packages/node-loader/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
/**
* @typedef {import('@mdx-js/mdx/lib/compile.js').CompileOptions} CompileOptions
*/

/**
* @typedef LoaderOptions
* Extra configuration.
* @property {boolean | null | undefined} [fixRuntimeWithoutExportMap=true]
* Several JSX runtimes, notably React below 18 and Emotion below 11.10.0,
* don’t yet have a proper export map set up (default: `true`).
* Export maps are needed to map `xxx/jsx-runtime` to an actual file in ESM.
* This option fixes React et al by turning those into `xxx/jsx-runtime.js`.
*
* @typedef {CompileOptions & LoaderOptions} Options
* Configuration.
* @typedef {import('@mdx-js/mdx/lib/compile.js').CompileOptions} Options
*/

import fs from 'node:fs/promises'
Expand All @@ -32,14 +19,6 @@ export function createLoader(options) {
const options_ = options || {}
const {extnames, process} = createFormatAwareProcessors(options_)
const regex = extnamesToRegex(extnames)
let fixRuntimeWithoutExportMap = options_.fixRuntimeWithoutExportMap

if (
fixRuntimeWithoutExportMap === null ||
fixRuntimeWithoutExportMap === undefined
) {
fixRuntimeWithoutExportMap = true
}

return {load, getFormat, transformSource}

Expand All @@ -60,14 +39,8 @@ export function createLoader(options) {
if (url.protocol === 'file:' && regex.test(url.pathname)) {
const value = await fs.readFile(url)
const file = await process(new VFile({value, path: url}))
let source = String(file)

/* c8 ignore next 3 -- to do: remove. */
if (fixRuntimeWithoutExportMap) {
source = String(file).replace(/\/jsx-runtime(?=["'])/, '$&.js')
}

return {format: 'module', shortCircuit: true, source}
return {format: 'module', shortCircuit: true, source: String(file)}
}

return defaultLoad(href, context, defaultLoad)
Expand Down Expand Up @@ -113,13 +86,7 @@ export function createLoader(options) {

if (url.protocol === 'file:' && regex.test(url.pathname)) {
const file = await process(new VFile({path: new URL(context.url), value}))
let source = String(file)

if (fixRuntimeWithoutExportMap) {
source = String(file).replace(/\/jsx-runtime(?=["'])/, '$&.js')
}

return {source}
return {source: String(file)}
}

return defaultTransformSource(value, context, defaultTransformSource)
Expand Down
2 changes: 1 addition & 1 deletion packages/node-loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"devDependencies": {},
"scripts": {
"test": "npm run test-coverage",
"test-api": "node --conditions development --loader=./test/react-18-node-loader.js test/index.js",
"test-api": "node --conditions development --loader=@mdx-js/node-loader test/index.js",
"test-coverage": "c8 --100 --reporter lcov npm run test-api"
},
"xo": {
Expand Down
14 changes: 0 additions & 14 deletions packages/node-loader/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,6 @@ Create a Node ESM loader to compile MDX to JS.
`options` are the same as [`compile` from `@mdx-js/mdx`][options].
One extra field is supported:

###### `options.fixRuntimeWithoutExportMap`

Fix broken export maps (`boolean`, default: `true`).

Several JSX runtimes, notably React below 18 and Emotion below 11.10.0, don’t
have a proper export map set up.
Export maps are needed to map `xxx/jsx-runtime` to an actual file in ESM.
This option fixes React et al by turning those into `xxx/jsx-runtime.js`.

> 👉 **Note**: If you are using recent React, or other proper packages, you
> have to turn this field off.
> See the example below on how to configure your loader.
> Pass `fixRuntimeWithoutExportMap: false` in options to it.
###### Example

`my-loader.js`:
Expand Down
2 changes: 1 addition & 1 deletion packages/node-loader/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ test('@mdx-js/node-loader', async function (t) {
await fs.rm(mdxUrl)

throw new Error(
'Please run Node with `--loader=./test/react-18-node-loader.js` to test the ESM loader'
'Please run Node with `--loader=@mdx-js/node-loader` to test the ESM loader'
)
}

Expand Down
6 changes: 0 additions & 6 deletions packages/node-loader/test/react-18-node-loader.js

This file was deleted.

2 changes: 0 additions & 2 deletions website/mdx-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ import {config} from '../docs/_config.js'

/** @type {Readonly<CompileOptions>} */
const options = {
// To do: remove.
fixRuntimeWithoutExportMap: false,
recmaPlugins: [recmaInjectMeta],
rehypePlugins: [
rehypePrettyCodeBlocks,
Expand Down

0 comments on commit 0f62bce

Please sign in to comment.