-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
179 lines (157 loc) · 6.14 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
// Include gulp
let gulp = require('gulp'),
plumber = require('gulp-plumber'),
nodemon = require('gulp-nodemon'),
browserSync = require('browser-sync'),
stylus = require('stylus'),
gulpStylus = require('gulp-stylus'),
fs = require('fs'),
gulpFn = require('gulp-fn'),
cssnano = require('cssnano'),
CleanCSS = require('clean-css'),
webpack = require('webpack'),
webpackStream = require('webpack-stream'),
named = require('vinyl-named'),
favicons = require('favicons'),
config = require('./gulpconfig');
// Assign gulpstylus to the most recent version
gulpStylus.stylus = stylus;
// Creates a proxy to the default server to inject css files and reload on js changes
gulp.task('connect', () => {
browserSync.init({
proxy: '127.0.0.1:3000',
open: false,
port: 3010,
injectChanges: true,
codeSync: true,
ui: false
});
});
gulp.task('nodemon', (done) => {
let called = false;
let stream = nodemon({
nodemon: require('nodemon'),
script: 'index.js',
watch: ['src', 'public/js', 'public/css/app.css', 'public/images']
});
stream.on('start', () => {
if (!called) {
done();
}
called = true;
});
stream.on('restart', () => setTimeout(() => browserSync.reload({stream: false}), 400));
stream.on('crash', () => stream.emit('restart', 10));
return stream;
});
gulp.task('stylus', () => {
return gulp.src(__dirname + '/public/css/app.styl')
.pipe(plumber())
.pipe(gulpStylus({ compress: false, sourcemap: true, 'include css': true }))
.pipe(gulp.dest(__dirname + '/public/css/'))
.pipe(browserSync.stream({ match: '**/*.css' }));
});
// Bundle the main app.js and the sw.js service worker
gulp.task("webpack", () => {
return gulp.src([__dirname + '/public/src/app.js', __dirname + '/public/src/sw.js'])
.pipe(plumber())
.pipe(named())
.pipe(webpackStream(config.webpackOptions, webpack, (err, stats) => {
if (err) {
throw error;
}
console.log(stats.toString({chunks: false, colors: true}));
}))
.pipe(gulp.dest(__dirname + '/public/js/'))
.pipe(browserSync.stream({match: '**/*.js'}));
});
gulp.task('build-stylus', () => {
return gulp.src(__dirname + '/public/css/app.styl')
.pipe(plumber())
.pipe(gulpStylus({ compress: false, sourcemap: false, 'include css': true }))
.pipe(gulpFn(file => {
file.contents = new Buffer(new CleanCSS(config.CleanCSSOptions).minify(file.contents.toString()).styles, 'utf-8');
return file;
}))
.pipe(gulpFn(file => {
return cssnano.process(file.contents.toString()).then(result => {
file.contents = new Buffer(result.css, 'utf-8');
return file;
});
}))
.pipe(gulp.dest(__dirname + '/public/css/'));
});
gulp.task("build-webpack", () => {
return gulp.src([__dirname + '/public/src/app.js', __dirname + '/public/src/sw.js'])
.pipe(plumber())
.pipe(named())
.pipe(webpackStream(Object.assign({}, config.webpackOptions, {
plugins: [
new webpack.optimize.UglifyJsPlugin({
mangle: true,
compress: {
warnings: false, // Suppress uglification warnings
pure_getters: true,
unsafe: true,
screw_ie8: true
},
output: {
comments: false
},
exclude: [/\.min\.js$/gi] // skip pre-minified libs
})
]
}), webpack, (err, stats) => {
if (err) {
throw error;
}
console.log(stats.toString({chunks: true, colors: true}));
}))
.pipe(gulp.dest(__dirname + '/public/js/'))
.pipe(browserSync.stream({match: '**/*.js'}));
});
gulp.task('build-icons', (done) => {
favicons(config.favicons.logo, config.favicons, (error, response) => {
if (error) {
console.log(error.status); // HTTP error code (e.g. `200`) or `null`
console.log(error.name); // Error name e.g. "API Error"
console.log(error.message); // Error description e.g. "An unknown error has occurred"
return;
}
let errorHandler = error => error && console.log(error);
for (let i in response.images) {
fs.writeFile(config.favicons.iconsPath + response.images[i].name, response.images[i].contents, errorHandler);
}
for (let i in response.files) {
let name = response.files[i].name;
if (name === 'manifest.json') {
try {
let jsonObj = JSON.parse(response.files[i].contents);
jsonObj.display = config.favicons.display;
jsonObj.theme_color = config.favicons.background;
jsonObj.short_name = config.favicons.shortName;
response.files[i].contents = JSON.stringify(jsonObj);
} catch (e) {
console.log(e);
}
}
fs.writeFile(config.favicons.iconsPath + response.files[i].name, response.files[i].contents, errorHandler);
}
let html = '';
for (let i in response.html) {
html += response.html[i];
}
html += '<link rel="shortcut icon" sizes="';
html += config.favicons.defaultFavicon.size + 'x' + config.favicons.defaultFavicon.size + '"';
html += ' href="' + config.favicons.defaultFavicon.fileName + '">';
fs.writeFile(config.favicons.linksViewPath + 'links.html', html, errorHandler);
done();
});
});
gulp.task('dev', ['build-icons','stylus', 'webpack', 'connect', 'nodemon'], () => {
gulp.watch(__dirname + '/public/css/**/*.styl', ['stylus']);
gulp.watch(__dirname + '/public/src/**/*.js', ['webpack']);
});
gulp.task('build', ['build-webpack', 'build-stylus', 'build-icons'], () => {
console.log('Done');
});