-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgulpfile.babel.js
218 lines (187 loc) · 5.81 KB
/
gulpfile.babel.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
'use strict';
import gulp from 'gulp';
import sequence from 'run-sequence';
import gulpLoadPlugins from 'gulp-load-plugins';
import browserSync from 'browser-sync';
import gulpif from 'gulp-if';
import source from 'vinyl-source-stream';
import sourcemaps from 'gulp-sourcemaps';
import buffer from 'vinyl-buffer';
import streamify from 'gulp-streamify';
import watchify from 'watchify';
import browserify from 'browserify';
import babelify from 'babelify';
import debowerify from 'debowerify';
// import ngAnnotate from 'browserify-ngannotate';
let isProd = false;
let plugins = gulpLoadPlugins();
const DIRS = {
src: 'app',
dest: 'build',
js: { app: 'js/app.js', libs: 'js/libs.js' }
};
const JS_DEPENDENCIES = [
'node_modules/angular/angular.js',
'node_modules/angular-ui-router/release/angular-ui-router.js',
'node_modules/angular-animate/angular-animate.js',
'node_modules/angular-aria/angular-aria.js',
'node_modules/angular-material/angular-material.js'
];
const CSS_DEPENDENCIES = [
'node_modules/angular-material/angular-material.css'
];
const PATHS = {
sass: [`${DIRS.src}/scss/*.scss`, `${DIRS.src}/scss/**/*.scss`],
js: [`${DIRS.src}/js/*.js`, `${DIRS.src}/js/**/*.js`],
html: [`${DIRS.src}/templates/*.html`, `${DIRS.src}/templates/**/*.html`]
};
// Handle sass changes.
gulp.task('sass', () => {
return gulp.src(PATHS.sass)
.pipe(plugins.sourcemaps.init())
.pipe(plugins.sass())
.pipe(plugins.concat('css/main.min.css'))
.pipe(plugins.cleanCss())
.pipe(plugins.sourcemaps.write(`../${DIRS.dest}`))
.pipe(gulp.dest(DIRS.dest))
// .pipe(browserSync.stream({match: '**/*.css'}))
.on('end', () => {
gulp
.src(CSS_DEPENDENCIES)
.pipe(plugins.sourcemaps.init())
.pipe(plugins.cleanCss())
.pipe(plugins.concat('css/libs.min.css'))
.pipe(plugins.sourcemaps.write(`../${DIRS.dest}`))
.pipe(gulp.dest(DIRS.dest));
// .pipe(browserSync.stream({match: '**/*.css'}));
});
});
// Handle html changes.
gulp.task('html', () => {
return gulp.src(PATHS.html)
.pipe(gulp.dest(`${DIRS.dest}/templates`))
.pipe(browserSync.stream({match: '**/*.html'}));
});
// Handle assets changes.
gulp.task('assets', () => {
return gulp.src(`${DIRS.src}/assets/*`)
.pipe(gulp.dest(`${DIRS.dest}/assets`));
});
// Handle assets changes.
gulp.task('assets', () => {
return gulp.src(`${DIRS.src}/assets/vendor/*/**`)
.pipe(gulp.dest(`${DIRS.dest}/assets/vendor`));
});
// Minify js
gulp.task('minifyJS', () => {
return gulp
.src(PATHS.js)
.pipe(plugins.concat(DIRS.js.app))
.pipe(plugins.babel())
// .pipe(plugins.uglify()).on('error', function(e){
// console.log(e);
// })
.pipe(gulp.dest(DIRS.dest))
.on('end', () => {
gulp
.src(JS_DEPENDENCIES)
.pipe(plugins.concat(DIRS.js.libs))
// .pipe(plugins.uglify()).on('error', function(e){
// console.log(e);
// })
.pipe(gulp.dest(DIRS.dest));
});
});
// Start the development sever task.
gulp.task('server', () => {
browserSync({
ghostMode: false,
notify: false,
server: DIRS.dest,
// tunnel: 'angularseedes6',
browser: 'google chrome',
port: 8000
});
});
// Task for watching file changes and livereloading the development server.
gulp.task('watch', cb => {
sequence(['sass', 'browserify', 'html', 'assets'], 'revision', ['server', 'watching'], cb);
});
gulp.task('watching', () => {
gulp.watch(PATHS.sass, ['sass']);
gulp.watch(PATHS.html, ['html']);
});
// Gulp default task.
gulp.task('default', ['watch']);
gulp.task('revision', () => {
return gulp.src(`${DIRS.src}/index.html`)
.pipe(gulp.dest(DIRS.dest))
.pipe(plugins.revAppend())
.pipe(gulp.dest(DIRS.dest));
});
gulp.task('minifyHTML', function() {
return gulp.src(PATHS.html)
.pipe(plugins.htmlmin({collapseWhitespace: true}))
.pipe(gulp.dest(`${DIRS.dest}/templates`));
});
gulp.task('eslint', () => {
return gulp.src(PATHS.js)
.pipe(plugins.eslint())
.pipe(plugins.eslint.format())
.pipe(plugins.eslint.failAfterError());
});
gulp.task('test', () => {
console.log('TODO test');
});
// Build command
gulp.task('build', cb => {
isProd = true;
sequence(['sass', 'browserify', 'minifyHTML', 'assets'], 'revision', cb);
});
// Based on: http://blog.avisi.nl/2014/04/25/how-to-keep-a-fast-build-with-browserify-and-reactjs/
function buildScript(file) {
const shouldCreateSourcemap = true;
let bundler = browserify({
entries: ['./app/js/' + file],
debug: shouldCreateSourcemap,
cache: {},
packageCache: {},
fullPaths: true
});
if (!isProd) {
bundler = watchify(bundler);
bundler.on('update', rebundle);
}
const transforms = [
{ name: babelify, options: {}},
{ name: debowerify, options: {}},
// { name: ngAnnotate, options: {}},
{ name: 'brfs', options: {}},
{ name: 'bulkify', options: {}}
];
transforms.forEach(function(transform) {
bundler.transform(transform.name, transform.options);
});
function rebundle() {
const stream = bundler.bundle();
const sourceMapLocation = '';
return stream
// .on('error', handleErrors)
// .on('end', bundleLogger.end)
.pipe(source(file))
.pipe(gulpif(shouldCreateSourcemap, buffer()))
.pipe(gulpif(shouldCreateSourcemap, sourcemaps.init({ loadMaps: true })))
// .pipe(gulpif(isProd, streamify(plugins.uglify({
// compress: { drop_console: true } // eslint-disable-line camelcase
// })))).on('error', function(e){
// console.log(e);
// })
.pipe(gulpif(shouldCreateSourcemap, sourcemaps.write(sourceMapLocation)))
.pipe(gulp.dest(DIRS.dest));
// .pipe(browserSync.stream());
}
return rebundle();
}
gulp.task('browserify', function() {
return buildScript('app.js');
});