1 | var _ = require('lodash');
|
2 | var mutil = require('miaow-util');
|
3 | var path = require('path');
|
4 | var pathIsAbsolute = require('path-is-absolute');
|
5 |
|
6 | function TaskContext(compilation, module) {
|
7 | var context = this;
|
8 |
|
9 | _.assign(context, _.pick(
|
10 | module,
|
11 | [
|
12 | 'context', 'output', 'src', 'srcDir', 'srcHash', 'destDir', 'ext', 'charset',
|
13 | 'hashLength', 'hashConnector', 'domain', 'contents', 'url', 'debug'
|
14 | ]
|
15 | ));
|
16 | _.assign(context, _.pick(compilation, ['startTime', 'emitFile']));
|
17 |
|
18 | context.extra = {};
|
19 | context.hooks = [];
|
20 | context.dependencies = [];
|
21 | context.emitModules = [];
|
22 | context.emitFiles = [];
|
23 |
|
24 | context.__cacheable__ = true;
|
25 | this.__compilation__ = compilation;
|
26 | }
|
27 |
|
28 | TaskContext.prototype.emitModule = function(file, contents, callback) {
|
29 | this.__compilation__.emitModule(file, contents, function(err, module) {
|
30 | if (!err && this.emitModules.indexOf(module.src) === -1) {
|
31 | this.emitModules.push(module.src);
|
32 | }
|
33 |
|
34 | callback(err, module);
|
35 | }.bind(this));
|
36 | };
|
37 |
|
38 | TaskContext.prototype.resolveModule = function(request, options, callback) {
|
39 | if (_.isFunction(options)) {
|
40 | callback = options;
|
41 | options = {};
|
42 | }
|
43 |
|
44 | this.__compilation__.resolveModule(
|
45 | path.resolve(this.context, this.srcDir),
|
46 | request,
|
47 | options,
|
48 | function(err, module) {
|
49 | if (!err) {
|
50 | this.addDependency(module.src);
|
51 | }
|
52 |
|
53 | callback(err, module);
|
54 | }.bind(this)
|
55 | );
|
56 | };
|
57 |
|
58 | TaskContext.prototype.addDependency = function(file) {
|
59 | file = mutil.relative(pathIsAbsolute(file) ? this.context : '', file);
|
60 |
|
61 | if (this.dependencies.indexOf(file) === -1) {
|
62 | this.dependencies.push(file);
|
63 | }
|
64 | };
|
65 |
|
66 | TaskContext.prototype.addHook = function(hook) {
|
67 | this.hooks.push(hook);
|
68 | };
|
69 |
|
70 | Object.defineProperty(TaskContext.prototype, 'cacheable', {
|
71 | set: function(value) {
|
72 | if (value === false) {
|
73 | this.__cacheable__ = value;
|
74 | }
|
75 | },
|
76 |
|
77 | get: function() {
|
78 | return this.__cacheable__;
|
79 | }
|
80 | });
|
81 |
|
82 | module.exports = TaskContext;
|