Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5afa48e

Browse files
committed
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.
1 parent 0f62bce commit 5afa48e

File tree

5 files changed

+10
-117
lines changed

5 files changed

+10
-117
lines changed

packages/node-loader/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

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

7-
const {getFormat, load, transformSource} = createLoader()
7+
const defaultLoader = createLoader()
88

9-
export {getFormat, load, transformSource}
109
export {createLoader} from './lib/index.js'
10+
export const load = defaultLoader.load

packages/node-loader/lib/index.js

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {extnamesToRegex} from '@mdx-js/mdx/lib/util/extnames-to-regex.js'
88
import {VFile} from 'vfile'
99

1010
/**
11-
* Create smart processors to handle different formats.
11+
* Create a loader to handle markdown and MDX.
1212
*
1313
* @param {Readonly<Options> | null | undefined} [options]
1414
* Configuration (optional).
@@ -20,9 +20,8 @@ export function createLoader(options) {
2020
const {extnames, process} = createFormatAwareProcessors(options_)
2121
const regex = extnamesToRegex(extnames)
2222

23-
return {load, getFormat, transformSource}
23+
return {load}
2424

25-
// Node version 17.
2625
/**
2726
* @param {string} href
2827
* URL.
@@ -45,51 +44,4 @@ export function createLoader(options) {
4544

4645
return defaultLoad(href, context, defaultLoad)
4746
}
48-
49-
// To do: remove.
50-
/* c8 ignore start */
51-
// Pre version 17.
52-
/**
53-
* @param {string} href
54-
* URL.
55-
* @param {unknown} context
56-
* Context.
57-
* @param {Function} defaultGetFormat
58-
* Default `getFormat` function.
59-
* @deprecated
60-
* This is an obsolete legacy function that no longer works in Node 17.
61-
* @returns
62-
* Result.
63-
*/
64-
function getFormat(href, context, defaultGetFormat) {
65-
const url = new URL(href)
66-
67-
return url.protocol === 'file:' && regex.test(url.pathname)
68-
? {format: 'module'}
69-
: defaultGetFormat(href, context, defaultGetFormat)
70-
}
71-
72-
/**
73-
* @param {string} value
74-
* Code.
75-
* @param {{url: string, [x: string]: unknown}} context
76-
* Context.
77-
* @param {Function} defaultTransformSource
78-
* Default `transformSource` function.
79-
* @deprecated
80-
* This is an obsolete legacy function that no longer works in Node 17.
81-
* @returns
82-
* Result.
83-
*/
84-
async function transformSource(value, context, defaultTransformSource) {
85-
const url = new URL(context.url)
86-
87-
if (url.protocol === 'file:' && regex.test(url.pathname)) {
88-
const file = await process(new VFile({path: new URL(context.url), value}))
89-
return {source: String(file)}
90-
}
91-
92-
return defaultTransformSource(value, context, defaultTransformSource)
93-
}
94-
/* c8 ignore end */
9547
}

packages/node-loader/readme.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,9 @@ One extra field is supported:
119119
```tsx
120120
import {createLoader} from '@mdx-js/node-loader'
121121

122-
// Load is for Node 17+, the rest for 12-16.
123-
const {getFormat, load, transformSource} = createLoader(/* Options… */)
122+
const {load} = createLoader(/* Options… */)
124123

125-
export {getFormat, load, transformSource}
124+
export {load}
126125
```
127126

128127
This example can then be used with `node --experimental-loader=./my-loader.js`.

packages/node-loader/test/index.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ test('@mdx-js/node-loader', async function (t) {
1212
await t.test('should expose the public api', async function () {
1313
assert.deepEqual(Object.keys(await import('@mdx-js/node-loader')).sort(), [
1414
'createLoader',
15-
'getFormat',
16-
'load',
17-
'transformSource'
15+
'load'
1816
])
1917
})
2018

script/jsx-loader.js

Lines changed: 3 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@ import fs from 'node:fs/promises'
22
import {fileURLToPath} from 'node:url'
33
import {transform} from 'esbuild'
44

5-
// To do: remove Node 16 version.
6-
const {getFormat, load, transformSource} = createLoader()
5+
const {load} = createLoader()
76

8-
export {getFormat, load, transformSource}
7+
export {load}
98

109
/**
1110
* A tiny JSX loader.
1211
*/
1312
export function createLoader() {
14-
return {getFormat, load, transformSource}
13+
return {load}
1514

16-
// Node version 17.
1715
/**
1816
* @param {string} href
1917
* URL.
@@ -48,58 +46,4 @@ export function createLoader() {
4846

4947
return {format: 'module', shortCircuit: true, source: code}
5048
}
51-
52-
// Pre version 17.
53-
/**
54-
* @param {string} href
55-
* URL.
56-
* @param {unknown} context
57-
* Context.
58-
* @param {Function} defaultGetFormat
59-
* Default `getFormat`.
60-
* @returns
61-
* Result.
62-
*/
63-
function getFormat(href, context, defaultGetFormat) {
64-
const url = new URL(href)
65-
66-
return url.pathname.endsWith('.jsx')
67-
? {format: 'module'}
68-
: defaultGetFormat(href, context, defaultGetFormat)
69-
}
70-
71-
/**
72-
* @param {Buffer} value
73-
* Code.
74-
* @param {{url: string, [x: string]: unknown}} context
75-
* Context.
76-
* @param {Function} defaultTransformSource
77-
* Default `transformSource`.
78-
* @returns
79-
* Result.
80-
*/
81-
async function transformSource(value, context, defaultTransformSource) {
82-
const url = new URL(context.url)
83-
84-
if (!url.pathname.endsWith('.jsx')) {
85-
return defaultTransformSource(value, context, defaultTransformSource)
86-
}
87-
88-
const {code, warnings} = await transform(String(value), {
89-
format: context.format === 'module' ? 'esm' : 'cjs',
90-
loader: 'jsx',
91-
sourcefile: fileURLToPath(url),
92-
sourcemap: 'both',
93-
target: 'esnext'
94-
})
95-
96-
if (warnings) {
97-
for (const warning of warnings) {
98-
console.log(warning.location)
99-
console.log(warning.text)
100-
}
101-
}
102-
103-
return {source: code}
104-
}
10549
}

0 commit comments

Comments
 (0)