1 | var _ = require('lodash');
|
2 | var async = require('async');
|
3 | var mutil = require('miaow-util');
|
4 | var path = require('path');
|
5 | var url = require('url');
|
6 |
|
7 | var pkg = require('../../package.json');
|
8 |
|
9 | module.exports = function(options, callback) {
|
10 | var context = this;
|
11 |
|
12 |
|
13 | context.destHash = mutil.hash(context.contents);
|
14 |
|
15 |
|
16 | var filename = path.basename(context.src).replace(/\.[^\.]+$/, context.ext || path.extname(context.src));
|
17 |
|
18 |
|
19 | var hashLength = context.hashLength;
|
20 | var hashConnector = context.hashConnector;
|
21 | var filenameWithHash = filename.replace(
|
22 | /\.[^\.]+$/,
|
23 | (hashLength ? (hashConnector + context.destHash.slice(0, hashLength)) : '') + path.extname(filename)
|
24 | );
|
25 |
|
26 |
|
27 | var destPathWithoutHash = mutil.relative('', path.join(context.destDir, filename));
|
28 | context.dest = mutil.relative('', path.join(context.destDir, filenameWithHash));
|
29 |
|
30 |
|
31 | context.url = url.resolve(
|
32 | (context.domain || '').replace(/\/?$/, '/'),
|
33 | [context.url.replace(/(^\/|\/$)/, ''), filenameWithHash].join('/')
|
34 | );
|
35 |
|
36 |
|
37 | context.emitFiles = _.uniq([destPathWithoutHash, context.dest]);
|
38 |
|
39 | function runHook(callback) {
|
40 | async.each(
|
41 | context.hooks,
|
42 | function(hook, callback) {
|
43 | hook(context, callback);
|
44 | },
|
45 |
|
46 | callback);
|
47 | }
|
48 |
|
49 | function emitFile(callback) {
|
50 | async.each(
|
51 | context.emitFiles,
|
52 | _.partial(context.emitFile, _, context.contents, context.charset || 'utf-8'),
|
53 | callback);
|
54 | }
|
55 |
|
56 | async.series([
|
57 | runHook,
|
58 | emitFile
|
59 | ], callback);
|
60 | };
|
61 |
|
62 | module.exports.toString = function() {
|
63 | return ['dest', pkg.version].join('@');
|
64 | };
|