-
Notifications
You must be signed in to change notification settings - Fork 1
/
tasks.js
179 lines (159 loc) · 4.76 KB
/
tasks.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
let gulp = require('gulp');
let browserSync = require('browser-sync');
let sass = require('gulp-sass');
let postcss = require('gulp-postcss');
let autoprefixer = require('autoprefixer');
let cssnano = require('cssnano');
let cp = require('child_process');
let reporter = require('postcss-reporter');
let runSequence = require('run-sequence');
let gutil = require('gulp-util');
let webpack = require('webpack');
let path = require('path');
let fs = require('fs-extra');
let messages = {
jekyllBuild: '<span style="color: grey">Running:</span> $ jekyll build'
};
let defaultWebpackDevConfig = require('./defaultConfigs/webpack.config.dev.js');
let defaultWebpackProdConfig = require('./defaultConfigs/webpack.config.prod.js');
module.exports = (gulp, options) => {
options = options || {};
let webpackDevConfig = options.webpackDevConfig || defaultWebpackDevConfig;
let webpackProdConfig = options.webpackProdConfig || defaultWebpackProdConfig;
/*
* Bootstrapping a new project
*/
gulp.task('setup-project', done => {
let setupSrc = path.resolve(__dirname, 'src');
let newSrc = path.resolve('./src');
fs.copy(setupSrc, newSrc, err => {
if(err) {
throw new gutil.PluginError('jekyll-tasks', err.message);
}
gutil.log('Project now setup at ' + newSrc);
done();
});
});
/**
* Build the Jekyll Site
*/
gulp.task('jekyll-build', done => {
browserSync.notify(messages.jekyllBuild);
cp.spawn('jekyll', ['build', '--source', './src'], {
stdio: 'inherit'
}).on('close', (code) => {
gutil.log('Jekyll build finsihed with a code of ' + code);
done();
});
});
gulp.task('jekyll-build:drafts', done => {
browserSync.notify(messages.jekyllBuild);
cp.spawn('jekyll', ['build', '--source', './src', '--drafts'], {
stdio: 'inherit'
}).on('close', (code) => {
gutil.log('Jekyll build finsihed with a code of ' + code);
done();
});
});
/**
* Rebuild Jekyll & do page reload
*/
gulp.task('jekyll-rebuild', ['jekyll-build:drafts'], () => {
browserSync.reload();
});
/**
* Wait for jekyll-build, then launch the Server
*/
gulp.task('browser-sync', ['sass', 'jekyll-build:drafts'], () => {
browserSync({
server: {
baseDir: '_site'
},
open: false
});
});
/**
* Compile files from _scss into both _site/builds/css (for live injecting) and site (for future jekyll builds)
*/
let browsers = ['> 2%', 'last 2 versions', 'ie >= 10', 'iOS >= 7'];
gulp.task('sass', () => {
let processors = [
autoprefixer({browsers}),
reporter({clearMessages: true})
];
return gulp.src('src/_scss/main.scss')
.pipe(sass({
outputStyle: 'expanded',
sourceComments: true,
includePaths: ['scss'],
onError: browserSync.notify
}).on('error', sass.logError))
.pipe(postcss(processors, {
map: true, // inline sourcemaps
diff: 'src/builds/css/postcss.patch'
}))
.pipe(gulp.dest('_site/builds/css'))
.pipe(browserSync.stream())
.pipe(gulp.dest('src/builds/css'));
});
gulp.task('sass:prod', () => {
let processors = [
autoprefixer({browsers}),
reporter({clearMessages: true}),
cssnano() // minify the result
];
return gulp.src('src/_scss/main.scss')
.pipe(sass({
outputStyle: 'compressed',
includePaths: ['scss']
}).on('error', sass.logError))
.pipe(postcss(processors))
.pipe(gulp.dest('src/builds/css'));
});
gulp.task('webpack:dev', done => {
// run webpack
webpack(webpackDevConfig, (err, stats) => {
if(err) {
throw new gutil.PluginError('jekyll-tasks', err);
}
gutil.log('[webpack]', stats.toString({
// output options
}));
done();
});
});
gulp.task('webpack:prod', done => {
// run webpack
webpack(webpackProdConfig, (err, stats) => {
if(err) {
throw new gutil.PluginError('webpack', err);
}
gutil.log('[webpack]', stats.toString({
// output options
}));
done();
});
});
/**
* Watch scss files for changes & recompile
* Watch html/md files, run jekyll & reload BrowserSync
*/
gulp.task('watch', () => {
gulp.watch('src/_scss/**/*.scss', ['sass']);
gulp.watch([
'src/**/*.html',
'src/**/*.md',
'src/_posts/*',
'src/builds/js/**'
], ['jekyll-rebuild']);
gulp.watch(['src/_js/**/*.js'], ['webpack:dev']);
});
/**
* Default task, running just `gulp` will compile the sass,
* compile the jekyll site, launch BrowserSync & watch files.
*/
gulp.task('dev', ['browser-sync', 'webpack:dev', 'watch']);
gulp.task('build', done => {
runSequence('sass:prod', 'webpack:prod', 'jekyll-build', done);
});
};