This repository has been archived by the owner on Nov 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
/
gulpfile.js
277 lines (257 loc) · 7.84 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/* global process */
'use strict';
const gulp = require('gulp');
const gutil = require('gulp-util');
const gulpif = require('gulp-if');
const webpack = require('webpack-stream');
const size = require('gulp-size');
const uglify = require('gulp-uglify');
const rename = require('gulp-rename');
const minifyCss = require('gulp-minify-css');
const autoprefixer = require('gulp-autoprefixer');
const browserSync = require('browser-sync');
const jshint = require('gulp-jshint');
const concat = require('gulp-concat');
const templateCache = require('gulp-angular-templatecache');
const ghPages = require('gulp-gh-pages');
const sourcemaps = require('gulp-sourcemaps');
const markdown = require('gulp-markdown');
const config = {
dragular: {
scripts: './src/*.js',
styles: './src/*.css',
dest: './dist'
},
docs: {
src: './docs/src/examples',
index: './docs/index.html',
scripts: './docs/src/examples/**/*.js',
styles: './docs/src/**/*.css',
templates: './docs/src/examples/**/*.html',
dest: './docs/dist'
},
browserSync: {
port: '3000',
server: './docs'
},
// Predefined scripts & styles configs to keep tasks DRY
scripts: {
libraryTarget: 'umd',
dragular: {
type: 'dragular',
entryPoint: './src/dragularModule.js',
bundleName: 'dragular.js',
dest: './dist'
},
docs: {
type: 'docs',
entryPoint: './docs/src/examples/examplesApp.js',
bundleName: 'examples.js',
dest: './docs/dist'
}
},
styles: {
dragular: {
entryPoint: './src/*.css',
bundleName: 'dragular.css',
dest: './dist'
},
docs: {
entryPoint: [
'./src/*.css',
'./docs/src/**/*.css'
],
bundleName: 'examples.css',
dest: './docs/dist'
}
},
// A flag attribute to switch modes
isProd: false
};
const tasks = {};
/*
* selectedConfigs stores temporary scripts & styles settings (like entry point or
* output directory). This metadata is used to configure the webpack compilation
* process.
*/
let selectedConfigs = {
scripts: config.scripts.dragular,
styles: config.styles.dragular
}
function handleErrors(err) {
gutil.log(err.toString());
this.emit('end');
}
tasks.lint = gulp.series(function lint() {
return gulp.src([config.dragular.scripts])
.pipe(jshint())
.pipe(jshint.reporter('jshint-stylish'));
});
tasks.lint_docs = gulp.series(function lintDocs() {
return gulp.src([config.docs.scripts, '!./docs/src/examples/templates.js'])
.pipe(jshint())
.pipe(jshint.reporter('jshint-stylish'));
});
tasks.scripts = gulp.series(selectedConfigs.scripts.type === 'dragular' ? tasks.lint : tasks.lint_docs, function scriptsTask(done) {
console.log('selected configs: ', selectedConfigs);
return gulp.src(selectedConfigs.scripts.entryPoint)
.on('error', handleErrors)
.pipe(webpack({
watch: config.isProd ? false : true,
output: {
libraryTarget: config.scripts.libraryTarget,
filename: selectedConfigs.scripts.bundleName
}
}, null, function(err, stats) {
if ( err ) {
throw err;
}
console.log('webpack build finished in ', stats.endTime - stats.startTime, 'ms');
done();
}))
.pipe(gulpif(config.isProd, gulp.dest(selectedConfigs.scripts.dest)))
.pipe(gulpif(config.isProd, size({
title: 'Non-minified scripts: '
})))
.pipe(gulpif(config.isProd, sourcemaps.init({loadMaps: true})))
.pipe(gulpif(config.isProd, uglify({
compress: { drop_console: true }
})))
.pipe(gulpif(config.isProd, rename({
suffix: '.min'
})))
.pipe(size({
title: 'Scripts: '
}))
.pipe(gulpif(config.isProd, sourcemaps.write('./')))
.pipe(gulp.dest(selectedConfigs.scripts.dest))
.pipe(gulpif(browserSync.active, browserSync.stream()));
}
);
tasks.styles = gulp.series(function stylesTask() {
return gulp.src(selectedConfigs.styles.entryPoint)
.pipe(autoprefixer({
browsers: [ 'last 15 versions', '> 1%', 'ie 8', 'ie 7' ],
cascade: false
}))
.pipe(concat(selectedConfigs.styles.bundleName))
.pipe(gulpif(config.isProd, gulp.dest(selectedConfigs.styles.dest)))
.pipe(gulpif(config.isProd, size({
title: 'Non-minified styles: '
})))
.pipe(gulpif(config.isProd, minifyCss()))
.pipe(gulpif(config.isProd, rename({
suffix: '.min'
})))
.pipe(size({
title: 'Styles: '
}))
.pipe(gulp.dest(selectedConfigs.styles.dest))
.pipe(gulpif(browserSync.active, browserSync.stream()));
}
);
tasks.serve = gulp.series(function serve(done) {
browserSync({
ui:{
port: '8081'
},
port: (typeof process !== 'undefined' && process.env.PORT) || config.browserSync.port, // cloud9 improvement
host: typeof process !== 'undefined' && process.env.IP, // cloud9 improvement
server: {
baseDir: config.browserSync.server
},
logConnections: true,
logFileChanges: true,
notify: true
});
done();
});
/*
* Concatenate and register templates in the $templateCache. The resulting file
* (templates.js) is placed inside the directory specified in config.docs.src.
*/
tasks.templates_docs = gulp.series(function templatesDocs() {
return gulp.src(config.docs.templates)
.pipe(templateCache({
moduleSystem: 'Browserify',
standalone: true
}))
.pipe(gulp.dest(config.docs.src));
});
/*
* Watch files for changes
*/
tasks.watch = gulp.series(tasks.serve, function watch(done) {
gulp.watch(config.dragular.styles, tasks.styles);
gulp.watch(config.dragular.scripts, tasks.scripts);
done();
});
tasks.watch_docs = gulp.series(tasks.serve, function watchDocs(done) {
console.log('watching docs');
gulp.watch(config.dragular.styles, tasks.styles);
gulp.watch(config.dragular.scripts, tasks.scripts);
gulp.watch(config.docs.styles, tasks.styles);
gulp.watch(config.docs.scripts, tasks.scripts);
gulp.watch(config.docs.templates, tasks.templates_docs);
gulp.watch(config.docs.index, browserSync.reload);
gulp.watch('./CONTRIBUTING.md', tasks.markdown);
done();
});
/*
* Launch browserSync server, watch & automatically refresh connected browsers
* on changes, generate non-minified but concatenated output.
*/
tasks.dev = gulp.series(function dev(done) {
config.isProd = true;
selectedConfigs = {
scripts: config.scripts.dragular,
styles: config.styles.dragular
};
done();
}, gulp.parallel(tasks.scripts, tasks.styles), tasks.watch);
/*
* Compiles markdown to html.
*/
tasks.markdown = gulp.series(function markdownTask() {
return gulp.src('./CONTRIBUTING.md')
.pipe(markdown())
.pipe(gulpif('**/CONTRIBUTING.html', rename({basename: 'contribute'})))
.pipe(gulp.dest(config.docs.src + '/partials/autogenerated'));
});
tasks.dev_docs = gulp.series(function devDocs(done) {
config.isProd = false;
selectedConfigs = {
scripts: config.scripts.docs,
styles: config.styles.docs
};
done();
}, tasks.markdown, tasks.templates_docs, gulp.parallel(tasks.scripts, tasks.styles), tasks.watch_docs);
tasks.build_docs = gulp.series(function buildDocs(done) {
config.isProd = true;
selectedConfigs = {
scripts: config.scripts.docs,
styles: config.styles.docs
};
done();
}, tasks.markdown, tasks.templates_docs, gulp.parallel(tasks.scripts, tasks.styles));
/*
* Generate production ready minified and concantenated output.
*/
tasks.build = gulp.series(function build(done) {
config.isProd = true;
selectedConfigs = {
scripts: config.scripts.dragular,
styles: config.styles.dragular
};
done();
}, gulp.parallel(tasks.scripts, tasks.styles), tasks.build_docs);
/*
* Publish code to GitHub pages.
*/
tasks.deploy_docs = gulp.series(function deployDocs() {
return gulp.src('./docs/**/*')
.pipe(ghPages());
});
exports.build = tasks.build;
gulp.task('dev:docs', tasks.dev_docs);
gulp.task('deploy:docs', tasks.deploy_docs);