Skip to content

Commit 0f62bce

Browse files
committedOct 18, 2023
node-loader: remove fixRuntimeWithoutExportMap
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.
1 parent 8eb11e2 commit 0f62bce

File tree

7 files changed

+6
-61
lines changed

7 files changed

+6
-61
lines changed
 

‎packages/node-loader/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @typedef {import('./lib/index.js').CompileOptions} Options
2+
* @typedef {import('./lib/index.js').Options} Options
33
*/
44

55
import {createLoader} from './lib/index.js'

‎packages/node-loader/lib/index.js

+3-36
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
11
/**
2-
* @typedef {import('@mdx-js/mdx/lib/compile.js').CompileOptions} CompileOptions
3-
*/
4-
5-
/**
6-
* @typedef LoaderOptions
7-
* Extra configuration.
8-
* @property {boolean | null | undefined} [fixRuntimeWithoutExportMap=true]
9-
* Several JSX runtimes, notably React below 18 and Emotion below 11.10.0,
10-
* don’t yet have a proper export map set up (default: `true`).
11-
* Export maps are needed to map `xxx/jsx-runtime` to an actual file in ESM.
12-
* This option fixes React et al by turning those into `xxx/jsx-runtime.js`.
13-
*
14-
* @typedef {CompileOptions & LoaderOptions} Options
15-
* Configuration.
2+
* @typedef {import('@mdx-js/mdx/lib/compile.js').CompileOptions} Options
163
*/
174

185
import fs from 'node:fs/promises'
@@ -32,14 +19,6 @@ export function createLoader(options) {
3219
const options_ = options || {}
3320
const {extnames, process} = createFormatAwareProcessors(options_)
3421
const regex = extnamesToRegex(extnames)
35-
let fixRuntimeWithoutExportMap = options_.fixRuntimeWithoutExportMap
36-
37-
if (
38-
fixRuntimeWithoutExportMap === null ||
39-
fixRuntimeWithoutExportMap === undefined
40-
) {
41-
fixRuntimeWithoutExportMap = true
42-
}
4322

4423
return {load, getFormat, transformSource}
4524

@@ -60,14 +39,8 @@ export function createLoader(options) {
6039
if (url.protocol === 'file:' && regex.test(url.pathname)) {
6140
const value = await fs.readFile(url)
6241
const file = await process(new VFile({value, path: url}))
63-
let source = String(file)
64-
65-
/* c8 ignore next 3 -- to do: remove. */
66-
if (fixRuntimeWithoutExportMap) {
67-
source = String(file).replace(/\/jsx-runtime(?=["'])/, '$&.js')
68-
}
6942

70-
return {format: 'module', shortCircuit: true, source}
43+
return {format: 'module', shortCircuit: true, source: String(file)}
7144
}
7245

7346
return defaultLoad(href, context, defaultLoad)
@@ -113,13 +86,7 @@ export function createLoader(options) {
11386

11487
if (url.protocol === 'file:' && regex.test(url.pathname)) {
11588
const file = await process(new VFile({path: new URL(context.url), value}))
116-
let source = String(file)
117-
118-
if (fixRuntimeWithoutExportMap) {
119-
source = String(file).replace(/\/jsx-runtime(?=["'])/, '$&.js')
120-
}
121-
122-
return {source}
89+
return {source: String(file)}
12390
}
12491

12592
return defaultTransformSource(value, context, defaultTransformSource)

‎packages/node-loader/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"devDependencies": {},
4444
"scripts": {
4545
"test": "npm run test-coverage",
46-
"test-api": "node --conditions development --loader=./test/react-18-node-loader.js test/index.js",
46+
"test-api": "node --conditions development --loader=@mdx-js/node-loader test/index.js",
4747
"test-coverage": "c8 --100 --reporter lcov npm run test-api"
4848
},
4949
"xo": {

‎packages/node-loader/readme.md

-14
Original file line numberDiff line numberDiff line change
@@ -112,20 +112,6 @@ Create a Node ESM loader to compile MDX to JS.
112112
`options` are the same as [`compile` from `@mdx-js/mdx`][options].
113113
One extra field is supported:
114114

115-
###### `options.fixRuntimeWithoutExportMap`
116-
117-
Fix broken export maps (`boolean`, default: `true`).
118-
119-
Several JSX runtimes, notably React below 18 and Emotion below 11.10.0, don’t
120-
have a proper export map set up.
121-
Export maps are needed to map `xxx/jsx-runtime` to an actual file in ESM.
122-
This option fixes React et al by turning those into `xxx/jsx-runtime.js`.
123-
124-
> 👉 **Note**: If you are using recent React, or other proper packages, you
125-
> have to turn this field off.
126-
> See the example below on how to configure your loader.
127-
> Pass `fixRuntimeWithoutExportMap: false` in options to it.
128-
129115
###### Example
130116

131117
`my-loader.js`:

‎packages/node-loader/test/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ test('@mdx-js/node-loader', async function (t) {
3838
await fs.rm(mdxUrl)
3939

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

‎packages/node-loader/test/react-18-node-loader.js

-6
This file was deleted.

‎website/mdx-config.js

-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ import {config} from '../docs/_config.js'
4848

4949
/** @type {Readonly<CompileOptions>} */
5050
const options = {
51-
// To do: remove.
52-
fixRuntimeWithoutExportMap: false,
5351
recmaPlugins: [recmaInjectMeta],
5452
rehypePlugins: [
5553
rehypePrettyCodeBlocks,

0 commit comments

Comments
 (0)
Please sign in to comment.