forked from zestedesavoir/zds-site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gulpfile.js
170 lines (151 loc) · 5.21 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
const path = require('path');
const livereload = require('gulp-livereload');
const concat = require('gulp-concat');
const del = require('del');
const gulp = require('gulp');
const imagemin = require('gulp-imagemin');
const postcss = require('gulp-postcss');
const sass = require('gulp-sass');
const sourcemaps = require('gulp-sourcemaps');
const spritesmith = require('gulp.spritesmith');
const uglify = require('gulp-uglify');
const jshint = require('gulp-jshint');
const autoprefixer = require('autoprefixer');
const cssnano = require('cssnano');
// PostCSS plugins used
const postcssPlugins = [
autoprefixer({ browsers: ['last 2 versions', '> 1%', 'ie >= 9'] }),
cssnano(),
];
const customSass = () => sass({
sourceMapContents: true,
includePaths: [
path.join(__dirname, 'node_modules'),
path.join(__dirname, 'dist', 'scss'),
],
});
// Deletes the generated files
gulp.task('clean', () => del([
'dist/',
]));
// Lint the js source files
gulp.task('js:lint', () =>
gulp.src([
'assets/js/*.js',
'!assets/js/editor.js', // We'll fix that later
])
.pipe(jshint())
.pipe(jshint.reporter('jshint-stylish'))
.pipe(jshint.reporter('fail')));
// Concat and minify all the js files
gulp.task('js', () =>
gulp.src([
require.resolve('jquery'),
require.resolve('cookies-eu-banner'),
// Used by other scripts, must be first
'assets/js/modal.js',
'assets/js/tooltips.js',
'assets/js/accessibility-links.js',
'assets/js/accordeon.js',
'assets/js/ajax-actions.js',
'assets/js/autocompletion.js',
'assets/js/close-alert-box.js',
'assets/js/compare-commits.js',
'assets/js/dropdown-menu.js',
'assets/js/editor.js',
'assets/js/featured-resource-preview.js',
'assets/js/form-email-username.js',
'assets/js/gallery.js',
'assets/js/index.js',
'assets/js/jquery-tabbable.js',
'assets/js/karma.js',
'assets/js/keyboard-navigation.js',
'assets/js/markdown-help.js',
'assets/js/message-hidden.js',
'assets/js/message-signature.js',
'assets/js/mobile-menu.js',
'assets/js/select-autosubmit.js',
'assets/js/snow.js',
'assets/js/spoiler.js',
'assets/js/submit-dbclick.js',
'assets/js/tab-modalize.js',
'assets/js/topic-suggest.js',
'assets/js/tribune-pick.js',
'assets/js/zen-mode.js',
], { base: '.' })
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(concat('script.js', { newline: ';\r\n' }))
.pipe(uglify())
.on('error', function (err) {
// gulp-uglify sucks
console.log(err.toString());
})
.pipe(sourcemaps.write('.', { includeContent: true, sourceRoot: '../../' }))
.pipe(gulp.dest('dist/js/')));
// Compiles the SCSS files to CSS
gulp.task('css', ['css:sprite'], () =>
gulp.src('assets/scss/main.scss')
.pipe(sourcemaps.init())
.pipe(customSass())
.pipe(postcss(postcssPlugins))
.pipe(sourcemaps.write('.', { includeContent: true, sourceRoot: '../../assets/scss/' }))
.pipe(gulp.dest('dist/css/')));
// Generates a sprite
gulp.task('css:sprite', () =>
gulp.src('assets/images/sprite/*.png')
.pipe(spritesmith({
cssTemplate: 'assets/scss/_sprite.scss.hbs',
cssName: 'scss/_sprite.scss',
imgName: 'images/sprite.png',
retinaImgName: 'images/[email protected]',
retinaSrcFilter: 'assets/images/sprite/*@2x.png',
}))
.pipe(gulp.dest('dist/')));
// Optimizes the images
gulp.task('images', ['css:sprite'], () =>
gulp.src('assets/{images,smileys}/**/*')
.pipe(imagemin())
.pipe(gulp.dest('dist/')));
// Watch for file changes
gulp.task('watch-runner', () => {
gulp.watch('assets/js/*.js', ['js']);
gulp.watch(['assets/{images,smileys}/**/*', '!assets/images/sprite*.png'], ['images']);
gulp.watch(['assets/scss/**/*.scss', '!assets/scss/_sprite.scss'], ['css']);
gulp.watch('dist/**/*', file =>
livereload.changed(
path.join('static/', path.relative(path.join(__dirname, 'dist/'), file.path))
)
);
livereload.listen();
});
// https://github.com/gulpjs/gulp/issues/259#issuecomment-152177973
gulp.task('watch', cb => {
function spawnGulp(args) {
return require('child_process')
.spawn(
'node_modules/.bin/gulp',
args,
{stdio: 'inherit'}
)
}
function spawnBuild() {
return spawnGulp(['build'])
.on('close', spawnWatch)
}
function spawnWatch() {
return spawnGulp(['watch-runner'])
.on('close', spawnWatch)
}
spawnBuild();
});
// Compiles errors' CSS
gulp.task('errors', () =>
gulp.src('errors/scss/main.scss')
.pipe(sourcemaps.init())
.pipe(customSass())
.pipe(postcss(postcssPlugins))
.pipe(sourcemaps.write('.', { includeContent: true, sourceRoot: '../scss/' }))
.pipe(gulp.dest('errors/css/')));
gulp.task('test', ['js:lint']);
gulp.task('build', ['css', 'js', 'images']);
gulp.task('default', ['watch', 'test']);