2014-12-05 19:26:30 -05:00
|
|
|
var util = require('./util');
|
|
|
|
var file2moduleName = require('./file2modulename');
|
|
|
|
var through2 = require('through2');
|
|
|
|
var path = require('path');
|
2015-03-02 18:15:36 -05:00
|
|
|
var fs = require('fs');
|
|
|
|
var VinylFile = require('vinyl');
|
2014-12-05 19:26:30 -05:00
|
|
|
|
|
|
|
module.exports = function(gulp, plugins, config) {
|
|
|
|
return function() {
|
|
|
|
return gulp.src(config.src)
|
2015-02-02 19:25:34 -05:00
|
|
|
.pipe(util.insertSrcFolder(plugins, config.srcFolderInsertion, config.modulesFolder))
|
2014-12-05 19:26:30 -05:00
|
|
|
.pipe(through2.obj(function(file, enc, done) {
|
|
|
|
var fileName = file.relative;
|
|
|
|
var moduleName = file2moduleName(fileName);
|
|
|
|
var moduleNameWithoutPath = path.basename(moduleName);
|
2015-03-02 18:15:36 -05:00
|
|
|
var self = this;
|
2014-12-05 19:26:30 -05:00
|
|
|
var scripts = util.filterByFile(config.scriptsPerFolder, fileName).map(function(script) {
|
|
|
|
var scriptTag;
|
2015-03-02 18:15:36 -05:00
|
|
|
var scriptSrc = script.src;
|
|
|
|
if (script.copy) {
|
|
|
|
scriptSrc = path.basename(script.src);
|
|
|
|
self.push(new VinylFile({
|
|
|
|
cwd: file.cwd,
|
|
|
|
base: file.base,
|
|
|
|
path: path.join(path.dirname(file.path), scriptSrc),
|
|
|
|
contents: fs.readFileSync(script.src)
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
if (scriptSrc) {
|
|
|
|
scriptTag = '<script src="'+scriptSrc+'" type="'+script.mimeType+'"></script>';
|
2014-12-05 19:26:30 -05:00
|
|
|
} else {
|
|
|
|
scriptTag = '<script type="'+script.mimeType+'">'+script.inline+'</script>';
|
|
|
|
}
|
|
|
|
return scriptTag
|
|
|
|
.replace('$MODULENAME_WITHOUT_PATH$', moduleNameWithoutPath)
|
|
|
|
.replace('$MODULENAME$', moduleName)
|
|
|
|
}).join('\n');
|
|
|
|
file.contents = new Buffer(file.contents.toString().replace('$SCRIPTS$', scripts));
|
|
|
|
this.push(file);
|
|
|
|
done();
|
|
|
|
}))
|
|
|
|
.pipe(gulp.dest(config.dest));
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|