diff --git a/packages/node-loader/index.js b/packages/node-loader/index.js index 67d01f4be..877ce08f5 100644 --- a/packages/node-loader/index.js +++ b/packages/node-loader/index.js @@ -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 diff --git a/packages/node-loader/lib/index.js b/packages/node-loader/lib/index.js index 3530fb42a..860f57c18 100644 --- a/packages/node-loader/lib/index.js +++ b/packages/node-loader/lib/index.js @@ -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 | 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 */ } diff --git a/packages/node-loader/readme.md b/packages/node-loader/readme.md index 0fd67555c..84674640b 100644 --- a/packages/node-loader/readme.md +++ b/packages/node-loader/readme.md @@ -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`. diff --git a/packages/node-loader/test/index.js b/packages/node-loader/test/index.js index e01b11b17..52fb2e24f 100644 --- a/packages/node-loader/test/index.js +++ b/packages/node-loader/test/index.js @@ -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' ]) }) diff --git a/script/jsx-loader.js b/script/jsx-loader.js index 877356416..47ad40c08 100644 --- a/script/jsx-loader.js +++ b/script/jsx-loader.js @@ -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} - } }