forked from sibartlett/colonizers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulptools.js
140 lines (118 loc) · 3.35 KB
/
gulptools.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
'use strict';
var _ = require('underscore'),
gulp = require('gulp'),
source = require('vinyl-source-stream'),
browserify = require('browserify'),
stringify = require('stringify'),
watchify = require('watchify'),
fs = require('fs'),
path = require('path'),
concat = require('gulp-concat'),
insert = require('gulp-insert'),
replace = require('gulp-replace'),
mold = require('mold-source-map');
function readPackageFile() {
var file = __dirname + '/package.json',
data = fs.readFileSync(file, 'utf8');
return JSON.parse(data);
}
function getBrowserDependencies() {
var pkgFile = readPackageFile(),
dependencies = pkgFile.dependencies,
result = [],
dep,
value;
for (dep in pkgFile.browser) {
value = pkgFile.browser[dep];
if (!value) {
dependencies[dep] = false;
}
}
result = [];
for (dep in dependencies) {
value = dependencies[dep];
if (value) {
result.push(dep);
}
}
result.sort();
return result;
}
function bundleJqueryPlugins(paths) {
var head = 'var jQuery = require(\'jquery\'),\n $ = jQuery;\n\n',
foot = '\n\nmodule.exports = jQuery;\n';
paths = paths.map(function(p) { return path.join(__dirname, p); });
return gulp.src(paths)
.pipe(replace('window.jQuery', 'jQuery'))
.pipe(replace('window.$', '$'))
.pipe(concat('jquery-plugins.js'))
.pipe(insert.wrap(head, foot));
}
function bundle(opts) {
var debug = opts.options && opts.options.debug,
destIndex = opts.dest.lastIndexOf('/'),
dir = opts.dest.substring(0, destIndex),
file = opts.dest.substring(destIndex + 1),
mapFilePath = opts.dest + '.map.json',
bundler,
options,
mapFileUrlComment,
bundleFunc;
if (opts.watch) {
options = _.extend(opts.options || {}, watchify.args);
bundler = browserify(opts.file || opts.files, options);
bundler = watchify(bundler);
} else {
bundler = browserify(opts.file || opts.files, opts.options);
}
mapFileUrlComment = function(sourcemap, cb) {
sourcemap.sourceRoot('file://');
sourcemap.mapSources(mold.mapPathRelativeTo(__dirname + '/'));
// write map file and return a sourceMappingUrl that points to it
fs.writeFile(mapFilePath, sourcemap.toJSON(2), 'utf-8', function(err) {
if (err) return console.error(err);
cb('//@ sourceMappingURL=' + path.basename(mapFilePath));
});
};
bundleFunc = function() {
if (debug) {
return bundler
.bundle()
.pipe(mold.transform(mapFileUrlComment))
.pipe(source(file))
.pipe(gulp.dest(dir));
} else {
return bundler
.bundle()
.pipe(source(file))
.pipe(gulp.dest(dir));
}
};
if (opts.watch) {
bundler.on('update', bundleFunc);
}
bundler.transform(stringify(['.html']));
if (opts.require) {
opts.require.forEach(function(dependency) {
bundler.require(dependency);
});
}
if (opts.jquery) {
bundler.require(
opts.jquery,
{ expose: 'jquery-plugins', basedir: '' }
);
}
if (opts.exclude) {
opts.exclude.forEach(function(dependency) {
bundler.exclude(dependency);
});
}
return bundleFunc();
}
module.exports = {
readPackageFile: readPackageFile,
getBrowserDependencies: getBrowserDependencies,
bundleJqueryPlugins: bundleJqueryPlugins,
bundle: bundle
};