1 | import crypto from 'crypto';
|
2 | import npath from 'path';
|
3 |
|
4 | import setExt from './set-ext.js';
|
5 |
|
6 | const getHash = buffer => {
|
7 | const hash = crypto.createHash('md5');
|
8 | hash.update(buffer);
|
9 | return hash.digest('hex').slice(0, 8);
|
10 | };
|
11 |
|
12 | export 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 | };
|