-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
192 lines (156 loc) · 5.89 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
var gulp = require('gulp');
var gutil = require('gulp-util');
var browserSync = require('browser-sync');
var browserify = require('browserify');
var watchify = require('watchify');
var minimist = require('minimist');
var sass = require('gulp-ruby-sass');
var vss = require('vinyl-source-stream');
var path = require('path');
var open = require('open');
var swig = require('gulp-swig'); //render swig templates
var gdata = require('gulp-data'); //pass data via streams
var rename = require('gulp-rename');
var hexofs = require('hexo-fs'); //promise friendly fs functions
var autoprefixer = require('gulp-autoprefixer');
//parse command line options
var knownOptions = {
string: ['name', 'batch'], //ex.: --batch svg-shapes --name svg-001-test --port 4263
//for each item, if theyh didn't pass an option, we'll default to one of these
default: {
name: 'ct-' + (new Date()).getTime(), //a unique name
batch: 'batch-001', //the default batch
port: 4266
}
};
var options = minimist(process.argv.slice(2), knownOptions);
//conveniences
var labPath = './lab/'+ options.batch + '/'+ options.name; //the lab is were we code
var distPath = './dist/'+ options.batch + '/'+ options.name; //the dist is the processed results
// ex: $ gulp init --batch svg-shapes --name svg-001-test
gulp.task('init', function(){
console.log('initializing project %s ', options.name);
hexofs.mkdirs( distPath + '/img' ); //no need to wait on this so no promise wrapping
var swigopts = {
defaults: {
cache: false
}
};
var context = {
name: options.name,
batch: options.batch
};
gulp.src('./templates/index.swig')
.pipe(gdata(context))
.pipe(swig(swigopts))
.pipe(rename('index.swig')) //it will still be a swig template
.pipe(gulp.dest( labPath + '/' ));
gulp.src('./templates/scss/styles.scss.swig')
.pipe(gdata(context))
.pipe(swig(swigopts))
.pipe(rename('styles.scss')) //when not generating html, need to rename output
.pipe(gulp.dest( labPath + '/sass/'));
gulp.src('./templates/js/source.js.swig')
.pipe(gdata(context))
.pipe(swig(swigopts))
.pipe(rename('source.js'))
.pipe(gulp.dest( labPath + '/js/'));
console.log('initializing done.\n To monitor, run command "%s"\nTo change, edit the files files at %s. ', 'gulp browserSync --batch ' + options.batch + ' --name ' + options.name, labPath);
});
//
// });
//gulp bs-watchify --name svg-001-test
// watch the html file with browsersync and if it changes refresh browser
// watch the sass with gulp.watch and if it changes recompile and refresh browser
// watch the js with watchify and if it changes rebuild, refresh browser
// browserify bundle js (and watch for future changes to trigger it again)
gulp.task('watchify', function(){
//mostly similary to the watchify task right above with one addition
var bundleShare = function(b) {
return b.bundle() //recall b (the watchify/browserify object alreadyknows the source files). carry out the bundling
.on("error", function(err) {
console.log("Browserify error:", err);
})
.pipe(vss( distPath + '/js/source.js'))
.pipe(gulp.dest('./'))
//after you're done bundling, inform browserSync to reload the page
.pipe(browserSync.reload({stream:true, once: true}));
};
var b = browserify({
cache: {},
packageCache: {},
fullPaths: true
});
//files we'll bundle and watch for changes to trigger bundling
b.add(labPath + '/js/source.js');
// b.add(labPath + '/index.nunj');
//wrap
b = watchify(b);
//whenever a file we're bundling is updated
b.on('update', function(paths){
//give some sort of gulp indication that a save occured on one of the watched files
console.log('watchify rebundling: ', paths);
bundleShare(b); //browserify away
});
// b.on('error', function (error) { // Catch any js errors and prevent them from crashing gulp
// console.error(error);
// this.emit('end');
// })
//while we're here let's do a one time browserify bundling
bundleShare(b);
});
//compile sass -> css
gulp.task('sass', function() {
return gulp.src(labPath + '/sass/styles.scss')
.pipe(sass({
//disabling sourmaps for now fir gulp-ruby-sass work with gulp-autoprefixer
//see http://stackoverflow.com/questions/26979433/gulp-with-gulp-ruby-sass-error-style-css-map31-unknown-word
"sourcemap=none": true,
//have some more stylesheets you may want to use? Add them here
"loadPath" : ['assets/scss']
}))
.on('error', function (error) { // Catch any SCSS errors and prevent them from crashing gulp
console.error(error);
this.emit('end');
})
.pipe(autoprefixer({
browsers: ['last 2 versions'],
cascade: false
}))
.pipe(gulp.dest(distPath + '/css'))
.pipe(browserSync.reload({ stream:true, once: true }));
});
//generate the html from the swig templates
gulp.task('gen-html', function() {
console.log('gen-html: generating html to %s', distPath);
var swigopts = {
defaults: {
cache: false
}
};
return gulp.src(labPath + '/index.swig')
.pipe(swig(swigopts))
.pipe(gulp.dest(distPath));
});
//watching non-specialized files (like sas changes)
gulp.task('watch', function(){
//when the scss changes, run gulp-sass task
gulp.watch(labPath + '/sass/*.scss', ['sass']);
//when the html (swig template) changes
gulp.watch(labPath + '/**/*.swig', ['gen-html']);
})
//we'll kick off watchify which will take care of the bundling and inform us
// ex: $ gulp browserSync --batch svg-pocket-guide --name svg-001-test
gulp.task('browserSync', ['watchify', 'watch'], function() {
browserSync(
{
server: { //have browser-synce be the static site
baseDir: "./", //the root /
directory: true //alternatly the root can just be the directory and you click the file
},
port: options.port,
// browserSync will have some watching duties as well. whenever the
// generated html changes we'll have refresh
files: [ distPath + '/index.html' ]
});
});