1.3.0 • Published 7 months ago
@nodejs-loaders/media v1.3.0
Nodejs Loaders: Media
Usage
$ npm i -D @nodejs-loaders/media
$ node --import @nodejs-loaders/media main.js
See README.md
in the repository's root for more details.
Environment: test
This loader returns the specifier (truncated from project root / current working directory) as the default export:
Compatible APIs: module.register
import photo from './team.jpg'; // photo = '[…]/team.jpg'
This ensures snapshots are unaffected by the file system on which the test is run.
Audio/Video:
.av1
.mp3
.mp3
.mp4
.ogg
.webm
Documents:
.epub
.pdf
Images:
.avif
.gif
.ico
.jpeg
.jpg
.png
.webp
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:
$ node ./example.mts
// ./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
// ./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
- This alternative loader just supports images.