-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathgulp-twoside.js
50 lines (48 loc) · 2.19 KB
/
gulp-twoside.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
/* jshint node: true */
'use strict';
var path = require('path');
var es = require('event-stream');
var gutil = require('gulp-util');
console.log(__dirname)
var removeExtname = function(path) {
var length = path.length;
if (path.slice(length - 3) === '.js') {
return path.slice(0, length - 3);
} else if (path.slice(length - 7) === '.coffee') {
return path.slice(0, length - 7);
} else { return path; }
};
module.exports = function(basepath, packageName, pathMap) {
basepath = path.join(__dirname,basepath)
if (pathMap===undefined) pathMap = {};
if (packageName===undefined) throw new Error("gulp-twoside: packageName is not provided.");
return es.through(function(file){
if (file.isStream()) return this.emit('error', new Error("gulp-twoside: Streaming not supported"));
var padpath = path.join(packageName, file.path.slice(basepath.length)).replace(/\\/g, '/');
var slashLastIndex = padpath.lastIndexOf("/");
var filename = padpath.slice(slashLastIndex+1);
if (filename==='twoside.js') { this.emit('data', file); return;}
var mappath = removeExtname(padpath).slice(packageName.length+1);
//console.log(mappath);
if (pathMap[mappath]!==undefined)
if (pathMap[mappath]!=='') padpath = packageName+'/'+pathMap[mappath];
else padpath = packageName;
//console.log(padpath);
var head, foot;
if (pathMap['only_wrap_for_browser']) {
head = "(function() {var ts = twoside('" + padpath + "'), require = ts.require, exports = ts.exports, module = ts.module; // wrap line by gulp-twoside for providing twoside module\n\n";
foot = "\n\n})();// wrap line by gulp-twoside"
} else {
head = "var exports, module, require; \n(function(require, exports, module) {var ts;if (typeof window === 'object') { ts = twoside('" + padpath + "'), require = ts.require, exports = ts.exports, module = ts.module;} // wrap line by gulp-twoside for providing twoside module; \n\n";
foot = "\n\n})(require, exports, module);// wrap line by gulp-twoside"
}
//console.log(file);
file.contents = Buffer.concat([
new Buffer(head),
file.contents,
new Buffer(foot)
]);
//console.log(file.path);
this.emit('data', file);
});
};