forked from fbrctr/fabricator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
183 lines (150 loc) · 4.25 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
'use strict';
// modules
var assemble = require('fabricator-assemble');
var browserSync = require('browser-sync');
var csso = require('gulp-csso');
var del = require('del');
var gulp = require('gulp');
var gutil = require('gulp-util');
var gulpif = require('gulp-if');
var imagemin = require('gulp-imagemin');
var prefix = require('gulp-autoprefixer');
var rename = require('gulp-rename');
var reload = browserSync.reload;
var runSequence = require('run-sequence');
var sass = require('gulp-sass');
var sourcemaps = require('gulp-sourcemaps');
var webpack = require('webpack');
// configuration
var config = {
dev: gutil.env.dev,
src: {
scripts: {
fabricator: './src/assets/fabricator/scripts/fabricator.js',
toolkit: './src/assets/toolkit/scripts/toolkit.js'
},
styles: {
fabricator: 'src/assets/fabricator/styles/fabricator.scss',
toolkit: 'src/assets/toolkit/styles/toolkit.scss'
},
images: 'src/assets/toolkit/images/**/*',
views: 'src/toolkit/views/*.html'
},
dest: 'dist'
};
// webpack
var webpackConfig = require('./webpack.config')(config);
var webpackCompiler = webpack(webpackConfig);
// clean
gulp.task('clean', function () {
return del([config.dest]);
});
// styles
gulp.task('styles:fabricator', function () {
gulp.src(config.src.styles.fabricator)
.pipe(sourcemaps.init())
.pipe(sass().on('error', sass.logError))
.pipe(prefix('last 1 version'))
.pipe(gulpif(!config.dev, csso()))
.pipe(rename('f.css'))
.pipe(sourcemaps.write())
.pipe(gulp.dest(config.dest + '/assets/fabricator/styles'))
.pipe(gulpif(config.dev, reload({stream:true})));
});
gulp.task('styles:toolkit', function () {
gulp.src(config.src.styles.toolkit)
.pipe(gulpif(config.dev, sourcemaps.init()))
.pipe(sass().on('error', sass.logError))
.pipe(prefix('last 1 version'))
.pipe(gulpif(!config.dev, csso()))
.pipe(gulpif(config.dev, sourcemaps.write()))
.pipe(gulp.dest(config.dest + '/assets/toolkit/styles'))
.pipe(gulpif(config.dev, reload({stream:true})));
});
gulp.task('styles', ['styles:fabricator', 'styles:toolkit']);
// scripts
gulp.task('scripts', function (done) {
webpackCompiler.run(function (error, result) {
if (error) {
gutil.log(gutil.colors.red(error));
}
result = result.toJson();
if (result.errors.length) {
result.errors.forEach(function (error) {
gutil.log(gutil.colors.red(error));
});
}
done();
});
});
// images
gulp.task('images', ['favicon'], function () {
return gulp.src(config.src.images)
.pipe(imagemin())
.pipe(gulp.dest(config.dest + '/assets/toolkit/images'));
});
gulp.task('favicon', function () {
return gulp.src('./src/favicon.ico')
.pipe(gulp.dest(config.dest));
});
// assemble
gulp.task('assemble', function (done) {
assemble({
logErrors: config.dev
});
done();
});
// server
gulp.task('serve', function () {
browserSync({
server: {
baseDir: config.dest
},
notify: false,
logPrefix: 'FABRICATOR'
});
/**
* Because webpackCompiler.watch() isn't being used
* manually remove the changed file path from the cache
*/
function webpackCache(e) {
var keys = Object.keys(webpackConfig.cache);
var key, matchedKey;
for (var keyIndex = 0; keyIndex < keys.length; keyIndex++) {
key = keys[keyIndex];
if (key.indexOf(e.path) !== -1) {
matchedKey = key;
break;
}
}
if (matchedKey) {
delete webpackConfig.cache[matchedKey];
}
}
gulp.task('assemble:watch', ['assemble'], reload);
gulp.watch('src/**/*.{html,md,json,yml}', ['assemble:watch']);
gulp.task('styles:fabricator:watch', ['styles:fabricator']);
gulp.watch('src/assets/fabricator/styles/**/*.scss', ['styles:fabricator:watch']);
gulp.task('styles:toolkit:watch', ['styles:toolkit']);
gulp.watch('src/assets/toolkit/styles/**/*.scss', ['styles:toolkit:watch']);
gulp.task('scripts:watch', ['scripts'], reload);
gulp.watch('src/assets/{fabricator,toolkit}/scripts/**/*.js', ['scripts:watch']).on('change', webpackCache);
gulp.task('images:watch', ['images'], reload);
gulp.watch(config.src.images, ['images:watch']);
});
// default build task
gulp.task('default', ['clean'], function () {
// define build tasks
var tasks = [
'styles',
'scripts',
'images',
'assemble'
];
// run build
runSequence(tasks, function () {
if (config.dev) {
gulp.start('serve');
}
});
});