-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (47 loc) · 1.35 KB
/
index.js
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
58
59
60
61
62
'use strict';
// load dependencies
var fs = require('fs'),
util = require('gulp-util'),
through = require('through2'),
requirejs = require('requirejs');
// Constants
var PLUGIN_NAME = 'gulp-afom-requirejs';
module.exports = function(opt) {
var compile = function(file, enc, cb) {
// check for file
if (file.isNull()) {
return cb(null, file);
}
// check if file is a stream
if (file.isStream()) {
return cb(new util.PluginError(PLUGIN_NAME, 'Streaming not supported'));
}
var successCallback = function() {
// empty file content
file.contents = null;
// try to read output file
fs.readFile(opt.out, function (err, contents) {
// check if file exists/can be read
if (err) {
return cb(new util.PluginError(PLUGIN_NAME, 'Failure reading in the JS output file'));
}
// if file content is not a buffer object
// then create one
if (!(contents instanceof Buffer)) {
contents = new Buffer(contents);
}
// update file content
file.contents = contents;
// return callback with out file
return cb(null, file);
});
};
var errorCallback = function(err){
// trigger requirejs error
return cb(new util.PluginError(PLUGIN_NAME, err));
};
// run requirejs optimize
requirejs.optimize(opt, successCallback, errorCallback);
};
return through.obj(compile);
};