-
Notifications
You must be signed in to change notification settings - Fork 2
/
gulpfile.js
78 lines (73 loc) · 2.78 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
const babelify = require('babelify');
const browserify = require('browserify');
const buffer = require('vinyl-buffer');
const cssAutoPrefixer = require('gulp-autoprefixer');
const gulp = require('gulp');
const less = require('gulp-less');
const logger = require('./util/logger');
const mold = require('mold-source-map');
const nodemon = require('gulp-nodemon');
const path = require('path');
const config = require('./config/config');
const rename = require('gulp-rename');
const source = require('vinyl-source-stream');
const sourcemaps = require('gulp-sourcemaps');
// Compiles assets and start the nodemon server.
gulp.task('default', ['less:compile', 'jsx:compile', 'dev']);
// Starts the nodemon server that automatically compiles assets and restarts on relevant file changes.
gulp.task('dev', (cb) => {
let started = false;
return nodemon({
script: 'app.js',
ignore: ['public/css/*', 'public/js/*', 'log/*'],
ext: 'js,jsx,less,ejs,json',
verbose: false,
tasks: (changedFiles) => {
const tasks = new Set();
changedFiles.forEach((file) => {
logger.info(`Changed: ${file}`);
if (path.extname(file) === '.less')
tasks.add('less:compile');
if (path.extname(file) === '.jsx')
tasks.add('jsx:compile');
});
return [...tasks];
}
}).on('start', () => {
if (!started) {
cb();
started = true;
}
});
});
// Compiles the LESS file 'main.less' in 'assets/less' into CSS file 'main.css' in 'public/css'
gulp.task('less:compile', () => {
return gulp.src('assets/less/main.less')
.pipe(less())
.on('error', (err) => {
logger.error(`compiling LESS\n\tFile: ${err.filename}\n\tLine: ${err.line}\n\tType: ${err.type}\n\tMessage: ${err.message}`);
return this;
})
.pipe(cssAutoPrefixer({
browsers: ['> 1%'],
cascade: false
}))
.pipe(rename(`main.css`))
.pipe(gulp.dest('public/css'));
});
// Compiles and bundles the JSX file 'main.jsx' in 'assets/jsx' into JS file 'main.js' to 'public/css'
gulp.task('jsx:compile', () => {
return browserify({ entries: 'assets/jsx/main.jsx', extensions: ['.js', '.jsx'], debug: true })
.transform(babelify, { presets: ['es2015'] })
.bundle()
.on('error', (err) => {
logger.error(`compiling JSX\n\tFile: ${err.filename}\n\tMessage: ${err.message}`);
})
.pipe(mold.transformSourcesRelativeTo('./'))
.pipe(source('main.jsx'))
.pipe(buffer())
.pipe(rename('main.js'))
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('public/js'));
});