forked from wagtail/wagtail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
160 lines (139 loc) · 4.41 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
var gulp = require('gulp');
var gutil = require('gulp-util');
var rename = require('gulp-rename');
var path = require('path');
gulp.task('default', ['build', 'watch']);
gulp.task('build', ['styles', 'javascript', 'images', 'fonts']);
var sourceDirName = 'static_src';
var destDirName = 'static';
var renameSrcToDest = function() {
return rename(function(filePath) {
filePath.dirname = filePath.dirname.replace(
'/' + sourceDirName + '/',
'/' + destDirName + '/');
});
};
var flatten = function(arrOfArr) {
return arrOfArr.reduce(function(flat, more) {
return flat.concat(more);
}, []);
};
// A Wagtail app that contains static files
var App = function(dir, options) {
this.dir = dir;
this.options = options || {};
this.appName = this.options.appName || path.basename(dir);
this.sourceFiles = path.join('.', this.dir, sourceDirName);
};
App.prototype = Object.create(null);
App.prototype.scssIncludePaths = function() {
return [this.sourceFiles];
};
App.prototype.scssSources = function() {
if (!this.options.scss) return [];
return this.options.scss.map(function(file) {
return path.join(this.sourceFiles, file);
}, this);
};
// All the Wagtail apps that contain static files
var apps = [
new App('wagtail/wagtailadmin', {
'scss': [
'wagtailadmin/scss/core.scss',
'wagtailadmin/scss/layouts/login.scss',
'wagtailadmin/scss/layouts/home.scss',
'wagtailadmin/scss/layouts/page-editor.scss',
'wagtailadmin/scss/layouts/preview.scss',
'wagtailadmin/scss/panels/rich-text.scss',
'wagtailadmin/scss/userbar.scss',
'wagtailadmin/scss/userbar_embed.scss',
],
}),
new App('wagtail/wagtaildocs'),
new App('wagtail/wagtailembeds'),
new App('wagtail/wagtailforms'),
new App('wagtail/wagtailimages', {
'scss': [
'wagtailimages/scss/add-multiple.scss',
'wagtailimages/scss/focal-point-chooser.scss',
],
}),
new App('wagtail/wagtailsnippets'),
new App('wagtail/wagtailusers', {
'scss': [
'wagtailusers/scss/groups_edit.scss',
],
}),
new App('wagtail/contrib/wagtailstyleguide', {
'scss': [
'wagtailstyleguide/scss/styleguide.scss'
],
}),
];
/*
* Watch - Watch files, trigger tasks when they are modified
*/
gulp.task('watch', ['build'], function () {
apps.forEach(function(app) {
gulp.watch(path.join(app.sourceFiles, '*/scss/**'), ['styles:sass']);
gulp.watch(path.join(app.sourceFiles, '*/css/**'), ['styles:css']);
gulp.watch(path.join(app.sourceFiles, '*/js/**'), ['javascript']);
gulp.watch(path.join(app.sourceFiles, '*/images/**'), ['images']);
gulp.watch(path.join(app.sourceFiles, '*/fonts/**'), ['fonts']);
});
});
/*
* Styles
**/
gulp.task('styles', ['styles:sass', 'styles:css']);
// SASS - Compile and move sass
var sass = require('gulp-sass');
var autoprefixer = require('gulp-autoprefixer');
gulp.task('styles:sass', function () {
// Wagtail Sass files include each other across applications,
// e.g. wagtailimages Sass files will include wagtailadmin/sass/mixins.scss
// Thus, each app is used as an includePath.
var includePaths = flatten(apps.map(function(app) { return app.scssIncludePaths(); }));
// Not all files in a directory need to be compiled, so each app defines
// its own Sass files that need to be compiled.
var sources = flatten(apps.map(function(app) { return app.scssSources(); }));
return gulp.src(sources)
.pipe(sass({
errLogToConsole: true,
includePaths: includePaths,
outputStyle: 'expanded'
}))
.pipe(autoprefixer({
browsers: ['last 2 versions'],
cascade: false
}))
.pipe(gulp.dest(function(file) {
// e.g. wagtailadmin/scss/core.scss -> wagtailadmin/css/core.css
// Changing the suffix is done by Sass automatically
return file.base
.replace('/static_src/', '/static/')
.replace('/scss/', '/css/');
}))
.on('error', gutil.log);
});
/*
* Simple copy tasks - these just copy files from the source to the destination,
* with no compilation, minification, or other intelligence
*
**/
var rename = require('gulp-rename');
var simpleCopyTask = function(glob) {
return function() {
var sources = apps.map(function(app) {
return path.join(app.sourceFiles, app.appName, glob);
});
return gulp.src(sources, {base: '.'})
.pipe(renameSrcToDest())
.pipe(gulp.dest('.'))
.on('error', gutil.log);
};
};
gulp.task('styles:css', simpleCopyTask('css/**/*'));
gulp.task('javascript', simpleCopyTask('js/**/*'));
gulp.task('images', simpleCopyTask('images/**/*'));
gulp.task('fonts', simpleCopyTask('fonts/**/*'));