forked from alexanderHDWCTW/GraphWhy.org
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
84 lines (74 loc) · 2.7 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
'use strict';
var autoprefixer = require('gulp-autoprefixer')
, browserify = require('browserify')
, buffer = require('vinyl-buffer')
, fs = require('fs')
, gulp = require('gulp')
, gulpIf = require('gulp-if')
, nodemon = require('gulp-nodemon')
, path = require('path')
, promise = require('es6-promise').polyfill()
, rename = require('gulp-rename')
, sass = require('gulp-sass')
, source = require('vinyl-source-stream')
, uglify = require('gulp-uglify')
, reactify = require('reactify')
/*
* In vanilla CSS, @import statements do another HTTP request to the server.
*
* In Sass, if you're importing a .sass or .scss file, the @import statement will
* be replaced with the contents of the file being imported. If you're importing a .css
* file, the @import statement remains intact in the outputted .css file.
*/
function npmModuleImporter(file, prev, done) {
// Import module if possible
try {
var basePath = path.resolve('node_modules', file)
var pkgInfo = path.join(basePath, 'package.json')
var info = JSON.parse(fs.readFileSync(pkgInfo))
// If the package.json doesn't have a `style` field, then find index.css
var newFilePath = path.join(basePath, info.style || 'index.css')
// If the imported file is a `.css` file.
if (path.extname(newFilePath) === '.css') {
var contents = fs.readFileSync(newFilePath).toString()
return done({ contents: contents })
} else {
return done({ file: newFilePath })
}
} catch(e) {
// If we fail to find/import the module, try finding it in client/ directory
try {
var contents = fs.readFileSync(path.resolve('client/sass', file)).toString()
return done({ contents: contents })
} catch(e) {
return done({ file: file })
}
}
}
gulp.task('compile-js', function() {
return browserify('./client/js/index.js')
.transform(reactify)
.bundle()
.pipe(source('scripts.min.js'))
.pipe(buffer())
.pipe(gulpIf(process.env.NODE_ENV === 'production', uglify()))
.pipe(gulp.dest('pub'))
})
gulp.task('compile-sass', function() {
return gulp.src('./client/sass/index.scss')
.pipe(sass({ importer: npmModuleImporter, outputStyle: 'compressed' }))
.pipe(autoprefixer({ browsers: ['last 2 versions'], cascade: false }))
.pipe(rename('styles.min.css'))
.pipe(gulp.dest('pub'))
})
gulp.task('compile', ['compile-js', 'compile-sass'])
gulp.task('server', ['compile'], function() {
gulp.watch('./client/sass/**', ['compile-sass'])
gulp.watch('./client/js/*.js', ['compile-js'])
return nodemon(
{ script: 'server'
, env : { NODE_ENV: 'development'}
, watch : ['server/**/*']
}
)
})