-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
61 lines (49 loc) · 1.58 KB
/
index.js
File metadata and controls
61 lines (49 loc) · 1.58 KB
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
// through2 is a thin wrapper around node transform streams
var _ = require('lodash');
var path = require('path');
var through = require('through2');
var gutil = require('gulp-util');
var PluginError = gutil.PluginError;
var compiler = require(path.resolve(__dirname, 'src', 'compiler.js'));
var fs = require('fs');
// Consts
const PLUGIN_NAME = 'gulp-angular-compiler';
// Plugin level function(dealing with files)
function gulpCompiler(opts) {
opts = _.extend({}, compiler.defOpts, opts);
if (!opts.config && !!opts.configDir) {
try {
opts.config = JSON.parse(fs.readFileSync(path.resolve(opts.configDir), {
encoding: 'utf8'
}));
} catch (ex) {}
}
if (!opts || !opts.config) {
throw new PluginError(PLUGIN_NAME, 'Missing opts.config!');
}
opts.config = _.extend({}, compiler.defConfig, opts.config);
// Creating a stream through which each file will pass
return through.obj(function(file, enc, cb) {
if (file.isNull()) {
return cb();
}
if (file.isStream()) {
this.emit('error', new PluginError(PLUGIN_NAME, 'Streaming not supported'));
return cb();
}
if (file.isBuffer()) {
var content = file.contents.toString();
if (!compiler.isValid(content)) {
return cb();
}
file.contents = new Buffer(compiler.compile(content, opts.config, opts.injectors, path.relative(path.resolve(), file.path)));
}
return cb(null, file);
});
};
// extend plugin
gulpCompiler.extend = function(obj) {
compiler.extend(obj);
};
// Exporting the plugin main function
module.exports = gulpCompiler;