All files index.ts

90% Statements 18/20
78.94% Branches 15/19
100% Functions 5/5
89.47% Lines 17/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 55 56 57          1x                 4x 4x 4x   4x     9x       9x 45x 5x     4x 4x   4x         4x       4x       4x         1x     4x    
import { readFileSync,  } from 'fs';
import { basename, extname } from 'path';
import { createFilter } from 'rollup-pluginutils';
import { Plugin } from 'rollup'
 
const defaultExtensions = [/\.jpg/, /\.jpeg/, /\.png/, /\.gif/, /\.svg/];
 
interface Options {
	extensions?: Array<string | RegExp>,
	include?: Array<string | RegExp> | string | RegExp | null,
	exclude?: Array<string | RegExp> | string | RegExp | null,
}
 
export default function image(options: Options = {}) : Plugin {
	const extensions = options.extensions || defaultExtensions;
	const filter = createFilter(options.include, options.exclude);
	let images: string[] = [];
 
	return {
		name: 'image-file',
		load(id: string) {
			Iif ('string' !== typeof id || !filter(id)) {
				return null;
			}
 
			const ext = extname(id)
			if (!extensions.some(item => typeof item === 'string' ? (item === ext) : item.test(ext))) {
				return null
			}
 
			Eif (images.indexOf(id) < 0) {
				images.push(id);
 
				const referenceId = this.emitFile({
          type: 'asset',
          name: basename(id),
          source: readFileSync(id)
        });
				return `export default import.meta.ROLLUP_FILE_URL_${referenceId};`;
			}
		},
		resolveFileUrl ({ fileName, relativePath }) {
			Iif ('string' !== typeof fileName || !filter(fileName)) {
				return null;
			}
 
			return `require('${isRelativePath(relativePath) ? relativePath : `./${relativePath}`}')`
		},
	};
}
 
const matchRelativePath = /^\.\.?[/\\]/;
 
function isRelativePath(str: string) {
  return matchRelativePath.test(str);
}