'use strict';
var PluginError = require('gulp-util').PluginError,
Transform = require('stream').Transform,
fs = require('fs'),
PLUGIN_NAME = 'gulp-pre-link';
module.exports = function(opts) {
opts = opts || {};
var stream = new Transform({
objectMode: true
});
stream._transform = function(file, encoding, cb) {
if (file.isNull()) {
return cb(null, file);
}
if (file.isStream()) {
var error = 'Streaming not supported';
return cb(new PluginError(PLUGIN_NAME, error));
} else if (file.isBuffer()) {
var content = String(file.contents);
var reg = /<link\s+[\s\S]*?["'\s\w\/\-](?:>|$)/gi;
var arrs = content.match(reg);
var reg2 = /\srel\s*=\s*('[^']+'|"[^"]+"|[^\s\/>]+)\s*href\s*=\s*('[^']+'|"[^"]+"|[^\s\/>]+)/i;
if (arrs != null) {
for (var i = 0; i < arrs.length; i++) {
var result = arrs[i].match(reg2);
if (result && result[1]) {
var rel = result[1].replace(/^['"]|['"]$/g, '').toLowerCase();
if (rel === 'import') {
var addr = result[2].replace(/^['"]|['"]$/g, '').toLowerCase();
var _this = this;
fs.readFile(addr, function(err, data) {
if (err) throw err;
content = content.replace(result.input, String(data));
file.contents = new Buffer(content);
_this.push(file);
});
} else {
file.contents = new Buffer(content);
this.push(file);
}
} else {
file.contents = new Buffer(content);
this.push(file);
}
}
cb();
}
}
};
return stream;
};