Skip to content

Commit

Permalink
node-loader: remove support for Node 14
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wooorm committed Oct 18, 2023
1 parent 0f62bce commit 5afa48e
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 117 deletions.
4 changes: 2 additions & 2 deletions packages/node-loader/index.js
Expand Up @@ -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
52 changes: 2 additions & 50 deletions packages/node-loader/lib/index.js
Expand Up @@ -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).
Expand All @@ -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.
Expand All @@ -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 */
}
5 changes: 2 additions & 3 deletions packages/node-loader/readme.md
Expand Up @@ -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`.
Expand Down
4 changes: 1 addition & 3 deletions packages/node-loader/test/index.js
Expand Up @@ -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'
])
})

Expand Down
62 changes: 3 additions & 59 deletions script/jsx-loader.js
Expand Up @@ -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.
Expand Down Expand Up @@ -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}
}
}

0 comments on commit 5afa48e

Please sign in to comment.