-
Notifications
You must be signed in to change notification settings - Fork 13
/
gulpfile.js
158 lines (139 loc) · 3.88 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
process.env.DEBUG = process.env.DEBUG || 'ar:*';
require('dotenv').load();
var gulp = require('gulp');
// var gutil = require('gulp-util');
var notify = require('gulp-notify');
var plumber = require('gulp-plumber');
var sourcemaps = require('gulp-sourcemaps');
var stylus = require('gulp-stylus');
var swiss = require('kouto-swiss');
var nodemon = require('nodemon');
var debugFactory = require('debug');
var webpack = require('webpack');
var webpackDevMiddleware = require('webpack-dev-middleware');
var webpackHotMiddleware = require('webpack-hot-middleware');
var browserSync = require('browser-sync');
var yargs = require('yargs');
var pckg = require('./package.json');
var webpackConfig = require('./webpack.config');
var debug = debugFactory('ar:gulp');
var sync = browserSync.create('ar-sync-server');
var reload = sync.reload.bind(sync);
// user definable ports
var port = yargs.argv.port || process.env.PORT || '3001';
var syncPort = yargs.argv['sync-port'] || process.env.SYNC_PORT || '3000';
// make sure sync ui port does not interfere with proxy port
var syncUIPort = yargs.argv['sync-ui-port'] ||
process.env.SYNC_UI_PORT ||
parseInt(syncPort, 10) + 2;
var isNotSSR = !process.env.SSR;
var paths = {
server: pckg.main,
serverIgnore: [
'client/**/*',
'package.json',
'gulpfile.js'
],
stylus: './common/index.styl',
stylusFiles: [
'./client/**/*.styl',
'./common/**/*.styl'
],
public: './public',
syncWatch: [
'./server/views/**.jade',
'./public/main.css'
]
};
if (isNotSSR) {
debug('no ssr');
paths.serverIgnore.push('common/**/*');
}
function errorHandler() {
var args = Array.prototype.slice.call(arguments);
// Send error to notification center with gulp-notify
notify.onError({
title: 'Compile Error',
message: '<%= error %>'
}).apply(this, args);
// Keep gulp from hanging on this task
this.emit('end');
}
gulp.task('serve', function(cb) {
var called = false;
const monitor = nodemon({
script: paths.server,
ext: '.jsx .js .json',
ignore: paths.serverIgnore,
// exec: path.join(__dirname, 'node_modules/.bin/babel-node'),
env: {
NODE_ENV: process.env.NODE_ENV || 'development',
DEBUG: process.env.DEBUG || 'ar:*',
PORT: port
}
})
.on('start', function() {
if (!called) {
called = true;
setTimeout(function() {
cb();
});
}
})
.on('restart', function(files) {
if (files) { debug('Files that changes: ', files); }
});
// add clean hear to prevent nodemon from running after
// <C-c> exit
// see: JacksonGariety/gulp-nodemon/issues/77
process.once('SIGINT', () => {
monitor.once('exit', () => {
/* eslint-disable no-process-exit */
process.exit(0);
/* eslint-enable no-process-exit */
});
});
});
gulp.task('stylus', function() {
return gulp.src(paths.stylus)
.pipe(plumber({ errorHandler: errorHandler }))
.pipe(sourcemaps.init())
.pipe(stylus({
use: swiss()
}))
.pipe(sourcemaps.write())
.pipe(gulp.dest(paths.public + '/css'))
.pipe(reload({ stream: true }));
});
var syncDependents = [
'serve',
'stylus'
];
gulp.task('dev-server', syncDependents, function() {
webpackConfig.entry.bundle = [
'webpack/hot/dev-server',
'webpack-hot-middleware/client'
].concat(webpackConfig.entry.bundle);
var bundler = webpack(webpackConfig);
sync.init(null, {
ui: {
port: syncUIPort
},
proxy: 'http://localhost:' + port,
logLeval: 'debug',
files: paths.syncWatch,
port: syncPort,
open: false,
middleware: [
webpackDevMiddleware(bundler, {
publicPath: webpackConfig.output.publicPath,
stats: 'errors-only'
}),
webpackHotMiddleware(bundler)
]
});
});
gulp.task('watch', function() {
gulp.watch(paths.stylusFiles, ['stylus']);
});
gulp.task('default', [ 'serve', 'stylus', 'dev-server', 'watch' ]);