UNPKG

553 BJavaScriptView Raw
1import crypto from 'crypto';
2import npath from 'path';
3
4import setExt from './set-ext.js';
5
6const getHash = buffer => {
7 const hash = crypto.createHash('md5');
8 hash.update(buffer);
9 return hash.digest('hex').slice(0, 8);
10};
11
12export default ({
13 buffer,
14 path,
15 target: { base = '.', dir = '.', ext = {}, fingerprint = false } = {}
16}) => {
17 const oldExt = npath.extname(path);
18 return setExt(
19 npath.join(dir, npath.relative(base, path).replace(/\.\./g, '__')),
20 (fingerprint ? `~${getHash(buffer)}` : '') + (ext[oldExt] || oldExt)
21 );
22};