1
1
mirror of https://github.com/mdx-js/mdx.git synced 2024-10-03 19:07:42 +03:00

node-loader: remove support for Node 14

Old node loaders used different hooks, such as `getFormat` and
`transformSource`.
A new version was added in Node 17, where we use `load`.
This was backported, so it does work in Node 16.
This commit is contained in:
Titus Wormer 2023-10-18 15:45:19 +02:00
parent 0f62bce94a
commit 5afa48e63c
No known key found for this signature in database
GPG Key ID: E6E581152ED04E2E
5 changed files with 10 additions and 117 deletions

View File

@ -4,7 +4,7 @@
import {createLoader} from './lib/index.js'
const {getFormat, load, transformSource} = createLoader()
const defaultLoader = createLoader()
export {getFormat, load, transformSource}
export {createLoader} from './lib/index.js'
export const load = defaultLoader.load

View File

@ -8,7 +8,7 @@ import {extnamesToRegex} from '@mdx-js/mdx/lib/util/extnames-to-regex.js'
import {VFile} from 'vfile'
/**
* Create smart processors to handle different formats.
* Create a loader to handle markdown and MDX.
*
* @param {Readonly<Options> | null | undefined} [options]
* Configuration (optional).
@ -20,9 +20,8 @@ export function createLoader(options) {
const {extnames, process} = createFormatAwareProcessors(options_)
const regex = extnamesToRegex(extnames)
return {load, getFormat, transformSource}
return {load}
// Node version 17.
/**
* @param {string} href
* URL.
@ -45,51 +44,4 @@ export function createLoader(options) {
return defaultLoad(href, context, defaultLoad)
}
// To do: remove.
/* c8 ignore start */
// Pre version 17.
/**
* @param {string} href
* URL.
* @param {unknown} context
* Context.
* @param {Function} defaultGetFormat
* Default `getFormat` function.
* @deprecated
* This is an obsolete legacy function that no longer works in Node 17.
* @returns
* Result.
*/
function getFormat(href, context, defaultGetFormat) {
const url = new URL(href)
return url.protocol === 'file:' && regex.test(url.pathname)
? {format: 'module'}
: defaultGetFormat(href, context, defaultGetFormat)
}
/**
* @param {string} value
* Code.
* @param {{url: string, [x: string]: unknown}} context
* Context.
* @param {Function} defaultTransformSource
* Default `transformSource` function.
* @deprecated
* This is an obsolete legacy function that no longer works in Node 17.
* @returns
* Result.
*/
async function transformSource(value, context, defaultTransformSource) {
const url = new URL(context.url)
if (url.protocol === 'file:' && regex.test(url.pathname)) {
const file = await process(new VFile({path: new URL(context.url), value}))
return {source: String(file)}
}
return defaultTransformSource(value, context, defaultTransformSource)
}
/* c8 ignore end */
}

View File

@ -119,10 +119,9 @@ One extra field is supported:
```tsx
import {createLoader} from '@mdx-js/node-loader'
// Load is for Node 17+, the rest for 12-16.
const {getFormat, load, transformSource} = createLoader(/* Options… */)
const {load} = createLoader(/* Options… */)
export {getFormat, load, transformSource}
export {load}
```
This example can then be used with `node --experimental-loader=./my-loader.js`.

View File

@ -12,9 +12,7 @@ test('@mdx-js/node-loader', async function (t) {
await t.test('should expose the public api', async function () {
assert.deepEqual(Object.keys(await import('@mdx-js/node-loader')).sort(), [
'createLoader',
'getFormat',
'load',
'transformSource'
'load'
])
})

View File

@ -2,18 +2,16 @@ import fs from 'node:fs/promises'
import {fileURLToPath} from 'node:url'
import {transform} from 'esbuild'
// To do: remove Node 16 version.
const {getFormat, load, transformSource} = createLoader()
const {load} = createLoader()
export {getFormat, load, transformSource}
export {load}
/**
* A tiny JSX loader.
*/
export function createLoader() {
return {getFormat, load, transformSource}
return {load}
// Node version 17.
/**
* @param {string} href
* URL.
@ -48,58 +46,4 @@ export function createLoader() {
return {format: 'module', shortCircuit: true, source: code}
}
// Pre version 17.
/**
* @param {string} href
* URL.
* @param {unknown} context
* Context.
* @param {Function} defaultGetFormat
* Default `getFormat`.
* @returns
* Result.
*/
function getFormat(href, context, defaultGetFormat) {
const url = new URL(href)
return url.pathname.endsWith('.jsx')
? {format: 'module'}
: defaultGetFormat(href, context, defaultGetFormat)
}
/**
* @param {Buffer} value
* Code.
* @param {{url: string, [x: string]: unknown}} context
* Context.
* @param {Function} defaultTransformSource
* Default `transformSource`.
* @returns
* Result.
*/
async function transformSource(value, context, defaultTransformSource) {
const url = new URL(context.url)
if (!url.pathname.endsWith('.jsx')) {
return defaultTransformSource(value, context, defaultTransformSource)
}
const {code, warnings} = await transform(String(value), {
format: context.format === 'module' ? 'esm' : 'cjs',
loader: 'jsx',
sourcefile: fileURLToPath(url),
sourcemap: 'both',
target: 'esnext'
})
if (warnings) {
for (const warning of warnings) {
console.log(warning.location)
console.log(warning.text)
}
}
return {source: code}
}
}