-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
71 lines (60 loc) · 2.03 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
63
64
65
66
67
68
69
70
71
'use strict';
var through = require('through2');
var lasso = require('lasso');
var injector = require('./lib/html-injector');
var gutil = require('gulp-util');
var fs = require('fs');
var extend = require('raptor-util/extend');
var PluginError = gutil.PluginError;
module.exports = function (options) {
//Path to a JSON lasso configuration file
var configFile = options.configFile;
var name = options.name || 'default';
var dependencies = options.dependencies || [];
var mode = options.mode || null;
var config = {};
if (mode) {
config.minify = (mode === "production") ? true : false;
config.bundlingEnabled = (mode === "production") ? true : false;
config.fingerprintsEnabled = (mode === "production") ? true : false;
}
extend(config, options);
delete config.dependencies;
delete config.mode;
delete config.configFile;
delete config.name;
if (configFile) {
try {
extend(config, JSON.parse(fs.readFileSync(configFile, {encoding: 'utf8'})));
} catch(e) {
console.error('Unable to parse JSON config file at path "' + config + '". Exception: ' + e);
process.exit(1);
}
}
return through.obj(function (file, enc, cb) {
var stream = this;
if (file.isNull()) {
return cb(null, file);
}
if (file.isStream()) {
this.emit('error', new PluginError('gulp-lasso', 'Streaming not supported'));
return;
}
var str = file.contents.toString();
lasso.configure(config);
lasso.lassoPage({
dependencies: dependencies,
name: name
},
function(err, lassoPageResult) {
// console.log('ERROR: ', err);
if (err) {
stream.emit('error', err);
return;
}
file.contents = new Buffer(injector.inject(str, lassoPageResult));
cb(null, file);
}
);
});
};