-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGulpfile.js
148 lines (122 loc) · 3.55 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
138
139
140
141
142
143
144
145
146
var path = require("path"),
_ = require("lodash"),
webserver = require("gulp-webserver"),
sequence = require("run-sequence"),
fileinclude = require("gulp-file-include"),
gulp = require("gulp"),
sass = require("gulp-sass"),
clean = require("gulp-clean"),
source = require("vinyl-source-stream"),
gutil = require("gulp-util"),
sourcemaps = require("gulp-sourcemaps"),
watchify = require("watchify"),
browserify = require("browserify"),
livereload = require("gulp-livereload"),
mappedPaths = {
'styles': 'styles',
'dist': 'dist'
},
options = {
env: process.env.NODE_ENV,
sourcemaps: false,
minify: false,
watch: false,
debug: false
};
gulp.task("dist", function(done) {
options.env = options.env || "production";
options.minify = true;
return sequence(
"clean",
["styles", "scripts", "html", "images"],
done
);
});
gulp.task("build", function(done) {
return sequence(
"clean",
["styles", "scripts", "html", "images"],
"watch",
done
);
});
gulp.task("server", ["build"], function(done) {
return gulp.src(dir("dist")).pipe(webserver({
livereload: true,
port: 3000
}));
});
gulp.task("watch", function() {
// livereload.listen();
gulp.watch(dir("styles", "**/*.{sass,scss}"), ["styles"]);
gulp.watch(dir("html", "**/*.html"), ["html"]);
//gulp.watch(dir("dist", "**/*")).on("change", livereload.changed);
});
gulp.task("clean", function() {
return gulp.src(dir("dist"), { read: false }).pipe(clean());
});
gulp.task("images", function() {
return gulp.src(dir("styles/images/**/*")).pipe(gulp.dest(dir("dist", "styles/images")));
});
gulp.task('styles', buildStyles);
gulp.task("scripts", buildScripts);
gulp.task("html", function() {
return gulp.src("./html/**/*.html")
.pipe(fileinclude({
prefix: "@@"
}))
.pipe(gulp.dest(dir("dist")));
});
function buildScripts() {
var bundler,
entryPoint = "./scripts/cnto.js",
bundlerOptions = {
basedir: __dirname,
debug: options.debug,
extensions: [".js"],
// needed for watchify
cache: {},
packageCache: {},
fullPaths: true
};
bundler = browserify(entryPoint, bundlerOptions);
if (options.watch) {
bundler = watchify(bundler);
bundler.on("update", makeBundle);
}
function makeBundle() {
var bundle = bundler.bundle();
return bundle.on("error", handleError("browserify"))
.pipe(source("scripts/cnto.js"))
.pipe(gulp.dest(dir("dist")));
}
return makeBundle();
}
function buildStyles() {
var srcDir = dir('styles', 'cnto.scss'),
destDir = dir('dist', 'styles'),
sassOptions = {
errLogToConsole: true
},
sourcemapsInit = options.sourcemaps ? sourcemaps.init() : gutil.noop();
sourcemapsWrite = options.sourcemaps ? sourcemaps.write() : gutil.noop();
sassOptions.outputStyle = options.minify ? "compressed" : "nested";
sassOptions.errLogToConsole = !!options.watch;
return gulp.src(srcDir)
.pipe(sourcemapsInit)
.pipe(sass(sassOptions))
.on("error", handleError("sass"))
.pipe(sourcemapsWrite)
.pipe(gulp.dest(destDir));
}
function dir(root) {
var args = _.toArray(arguments);
args[0] = mappedPaths[root] || root;
return path.join.apply(path, args);
}
function handleError(errSection) {
return function(err) {
gutil.log(gutil.colors.bgRed(errSection), err);
this.emit("end");
};
}