@nodejs-loaders/media
Advanced tools
+17
-1
| export const exts: Set<string>; | ||
| export type FileURL = import("../types.d.ts").FileURL; | ||
| export type FileExtensionsList = Array<string> | Set<string>; | ||
| export type MediaExtensionAddRemoveConfig = { | ||
| /** | ||
| * A list of file extensions to add to the default list. | ||
| */ | ||
| additions: FileExtensionsList; | ||
| /** | ||
| * A list of file extensions to remove from the default list. | ||
| */ | ||
| deletions: FileExtensionsList; | ||
| }; | ||
| /** | ||
| * A list of file extensions to REPLACE the default list. | ||
| */ | ||
| export type MediaExtensionReplacementConfig = FileExtensionsList; | ||
| declare function resolveMedia(specifier: string, context: import("module").ResolveHookContext, nextResolve: (specifier: string, context?: Partial<import("module").ResolveHookContext>) => import("module").ResolveFnOutput | Promise<import("module").ResolveFnOutput>): import("module").ResolveFnOutput | Promise<import("module").ResolveFnOutput>; | ||
| declare function loadMedia(url: string, context: import("module").LoadHookContext, nextLoad: (url: string, context?: Partial<import("module").LoadHookContext>) => import("module").LoadFnOutput | Promise<import("module").LoadFnOutput>): import("module").LoadFnOutput | Promise<import("module").LoadFnOutput>; | ||
| export { resolveMedia as resolve, loadMedia as load }; | ||
| declare function initialiseMedia(data: any): void | Promise<void>; | ||
| export { resolveMedia as resolve, loadMedia as load, initialiseMedia as initialize }; | ||
| //# sourceMappingURL=media.loader.d.mts.map |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"media.loader.d.mts","sourceRoot":"","sources":["media.loader.mjs"],"names":[],"mappings":"AA8CA,+BAoBG;sBA9DW,OAAO,eAAe,EAAE,OAAO;uIA+Dy0e,CAAC;kHAAo8C,CAAC"} | ||
| {"version":3,"file":"media.loader.d.mts","sourceRoot":"","sources":["media.loader.mjs"],"names":[],"mappings":"AAmFA,+BAmBG;sBAlGW,OAAO,eAAe,EAAE,OAAO;iCAwChC,KAAK,CAAC,MAAM,CAAC,GAAC,GAAG,CAAC,MAAM,CAAC;;;;;eAG5B,kBAAkB;;;;eAClB,kBAAkB;;;;;8CAEf,kBAAkB;uIAqDiyc,CAAC;kHAA89C,CAAC"} |
+39
-3
@@ -21,3 +21,3 @@ import process from 'node:process'; | ||
| ...ctx, | ||
| // @ts-ignore https://github.com/DefinitelyTyped/DefinitelyTyped/pull/71493 | ||
| format: 'media', | ||
@@ -33,3 +33,2 @@ url: nextResult.url, | ||
| async function loadMedia(url, ctx, nextLoad) { | ||
| // @ts-ignore https://github.com/DefinitelyTyped/DefinitelyTyped/pull/71493 | ||
| if (ctx.format !== 'media') return nextLoad(url); | ||
@@ -47,2 +46,40 @@ | ||
| /** | ||
| * @typedef {Array<string>|Set<string>} FileExtensionsList | ||
| * | ||
| * @typedef {object} MediaExtensionAddRemoveConfig | ||
| * @prop {FileExtensionsList} MediaExtensionAddRemoveConfig.additions A list of file extensions to add to the default list. | ||
| * @prop {FileExtensionsList} MediaExtensionAddRemoveConfig.deletions A list of file extensions to remove from the default list. | ||
| * | ||
| * @typedef {FileExtensionsList} MediaExtensionReplacementConfig A list of file extensions to REPLACE the default list. | ||
| */ | ||
| /** | ||
| * @type {import('node:module').InitializeHook} | ||
| * @param {MediaExtensionAddRemoveConfig|MediaExtensionReplacementConfig} config | ||
| */ | ||
| function initialiseMedia(config) { | ||
| if (config == null) return; | ||
| if (isList(config)) { | ||
| exts.clear(); | ||
| for (const r of /** @type {Iterable} */ (config)) exts.add(r); | ||
| } | ||
| if ('additions' in config && isList(config.additions)) { | ||
| for (const a of config.additions) exts.add(a); | ||
| } | ||
| if ('deletions' in config && isList(config.deletions)) { | ||
| for (const d of config.deletions) exts.delete(d); | ||
| } | ||
| } | ||
| export { initialiseMedia as initialize }; | ||
| /** | ||
| * @param {unknown} suspect | ||
| * @returns {boolean} | ||
| */ | ||
| const isList = (suspect) => | ||
| typeof suspect[Symbol.iterator] === 'function' && | ||
| (Array.isArray(suspect) || suspect instanceof Set); | ||
| const cwd = process.cwd(); | ||
@@ -56,3 +93,2 @@ | ||
| '.mp3', | ||
| '.mp3', | ||
| '.mp4', | ||
@@ -59,0 +95,0 @@ '.ogg', |
+1
-1
| { | ||
| "version": "1.1.0", | ||
| "version": "1.2.0", | ||
| "name": "@nodejs-loaders/media", | ||
@@ -4,0 +4,0 @@ "description": "Extend node to support media imports via customization hooks.", |
+53
-1
@@ -5,5 +5,18 @@ # Nodejs Loaders: Media | ||
| [](https://www.npmjs.com/package/nodejs-loaders/media) | ||
| [](https://www.npmjs.com/package/@nodejs-loaders/media) | ||
|  | ||
| [](https://nodejs.org/download) | ||
| ## Usage | ||
| ```console | ||
| $ npm i -D @nodejs-loaders/media | ||
| ``` | ||
| ```console | ||
| $ node --import @nodejs-loaders/media main.js | ||
| ``` | ||
| See `README.md` in the repository's root for more details. | ||
| **Environment**: test | ||
@@ -43,4 +56,43 @@ | ||
| ## Extending supported extensions | ||
| Media loader's default list of file extenions can be modified via `module.register`; either with addition(s) and/or deletion(s) OR replacements: | ||
| ```console | ||
| $ node ./example.mts | ||
| ``` | ||
| ```js | ||
| // ./example.mts | ||
| import module from 'node:module'; | ||
| module.register('@nodejs-loaders/media', import.meta.url, { | ||
| data: { | ||
| additions: ['.ext'], // This will add .ext to the default list. | ||
| deletions: ['.ico'], // This will remove .ico from the default list. | ||
| }, | ||
| }); | ||
| const someFileA = await import('./some.ext'); // someFile = '[…]/some.ext' | ||
| const someFileB = await import('./some.ico'); // 💥 | ||
| ``` | ||
| OR | ||
| ```js | ||
| // ./example.mts | ||
| import module from 'node:module'; | ||
| module.register('@nodejs-loaders/media', import.meta.url, { | ||
| data: ['.ext'], // ⚠️ This will REPLACE the entire list with ONLY the .ext file extension. | ||
| }); | ||
| const someFileA = await import('./some.ext'); // someFile = '[…]/some.ext' | ||
| const someFileB = await import('./some.ico'); // 💥 | ||
| ``` | ||
| ## Alternatives | ||
| * [`esm-loader-images`](https://github.com/brev/esm-loaders/tree/main/packages/esm-loader-images#readme) - This alternative loader just supports images. |
7842
64.13%94
49.21%97
115.56%