-
Notifications
You must be signed in to change notification settings - Fork 2
/
media.mjs
54 lines (44 loc) · 1.03 KB
/
media.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import process from 'node:process';
import { getFilenameExt } from './parse-filename.mjs';
export async function resolve(specifier, ctx, nextResolve) {
const nextResult = await nextResolve(specifier);
// Check against the fully resolved URL, not just the specifier, in case another loader has
// something to contribute to the resolution.
if (!exts.has(getFilenameExt(nextResult.url))) return nextResult;
return {
...ctx,
format: 'media',
url: nextResult.url,
}
}
export async function load(url, ctx, nextLoad) {
if (ctx.format !== 'media') return nextLoad(url);
const source = `export default '${url.replace(cwd, '[…]')}';`;
return {
format: 'module',
shortCircuit: true, // There's nothing else for another loader to do, so signal to stop.
source,
};
}
const cwd = process.cwd();
export const exts = new Set([
/**
* A/V
*/
'.av1',
'.mp3',
'.mp3',
'.mp4',
'.ogg',
'.webm',
/**
* images
*/
'.avif',
'.gif',
'.ico',
'.jpeg',
'.jpg',
'.png',
'.webp',
]);