-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
145 lines (120 loc) · 3.37 KB
/
gulpfile.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
var gulp = require('gulp');
var browserify = require('browserify');
var babel = require('babelify');
var react = require('react');
var concat = require("concat");
var fs = require("fs");
var source = require('vinyl-source-stream');
var uglify = require("gulp-uglify");
var jshint = require('jshint');
var jshintStylish = require('jshint-stylish');
var inject = require('gulp-inject');
var path = require("path");
var rename = require('gulp-rename');
var using = require('gulp-using');
var del = require("del");
var flatten = require('gulp-flatten');
// var sourcemaps = require('gulp-sourcemaps');
// var source = require('vinyl-source-stream');
// var buffer = require('vinyl-buffer');
// var watchify = require('watchify');
var p = {
dest: "./build",
ownJs: ["./src/**/*.js", "./src/**/*.jsx"],
ownStylus: ["./src/**/*.styl"],
indexHtml: './src/index.html',
libsDir: "./node_modules",
libsDestSubDir: "resources/",
libs: {
"bootstrap": [
"dist/css/bootstrap.min.css",
"dist/css/bootstrap-theme.min.css",
"dist/js/bootstrap.min.js"
]
// ,
// "bootstrap": "**/*"
},
libsCss: [
"bootstrap/dist/css/bootstrap.min.css"
]
};
/**
* Adapts the lib paths for moving and injecting by
* adding a basepath.
* @param {Object} libs The libs in original key-value format
* @param {Boolean} inject Get paths adapted for inject (if false, paths point to the lib source)
* @return {Array} Array of libs
*/
function getLibPaths(libs, inject) {
inject = inject || false;
var libName,
libSrcs = [],
outSrcs = [];
for (libName in libs) {
libSrcs = libs[libName];
if (typeof libSrcs === "string") {
libSrcs = [libSrcs];
}
var basePath;
if (inject === true) {
// Adapt basepath for inject
basePath = path.join(p.dest, p.libsDestSubDir, libName);
}
else {
// Adapt basepath for libs' origin
basePath = path.join(p.libsDir, libName);
}
libSrcs = libSrcs.map(function(src) {
return path.join(basePath, src);
});
outSrcs = outSrcs.concat(libSrcs);
}
return outSrcs;
}
gulp.task("clean:total", function() {
del("./build");
});
gulp.task("html", function() {
return gulp
.src([p.indexHtml])
.pipe(gulp.dest(p.dest));
});
gulp.task("libs:move", function() {
var sourcePaths = getLibPaths(p.libs, false);
var destDir = path.join(p.dest, p.libsDestSubDir);
console.log(sourcePaths);
gulp.src(sourcePaths, {base: p.libsDir})
// .pipe(flatten())
.pipe(using())
.pipe(gulp.dest(destDir));
});
gulp.task("libs:inject", function() {
// Find out which files to inject
var libInjectPaths = getLibPaths(p.libs, true);
console.log(libInjectPaths);
gulp.src(p.indexHtml)
.pipe(inject(gulp.src(libInjectPaths, {read: true, base: p.dest}), {addRootSlash: false, relative: false, ignorePath: "build"}))
.pipe(rename("index.html"))
.pipe(gulp.dest("./build"));
});
gulp.task("libs", ["libs:move"], function() {
return gulp.start("libs:inject");
});
gulp.task("js", function() {
return browserify({
entries: "./src/index.jsx",
debug: true
})
.transform("babelify", {presets: ["es2015", "react"]}).bundle()
// .pipe(jshint())
// .pipe(jshint.reporter(jshintStylish))
.pipe(source("bundle.js"))
// .pipe(uglify())
// .pipe(concat("bundle.js"))
.pipe(gulp.dest(p.dest))
// .pipe(fs.createWriteStream("bundle.js"));
});
gulp.task("watch", function() {
gulp.watch(p.js, ["html", "js"])
});
gulp.task("default", ["html", "js", "watch"]);