-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
131 lines (107 loc) · 2.71 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
const gulp = require('gulp');
const path = require('path');
const nodemon = require('gulp-nodemon');
const babel = require('gulp-babel');
const watch = require('gulp-watch');
const logger = require('gulp-logger');
const del = require('del');
const {
generateScssStub,
isStyleChanged
} = require('./build/gulp/generate-scss-stub');
let nodemonStream = null;
const { log, error } = console;
const rs = timeout => {
if (nodemonStream) {
nodemonStream.emit('restart', timeout);
}
};
gulp.task('clean', () => del(['dist']));
gulp.task('compile-server-src', ['clean'], () =>
gulp
.src('./client/**/*.js')
.pipe(
logger({
before: 'Starting code compiling...',
after: 'Compiling complete!',
showChange: true
})
)
.pipe(babel())
.pipe(gulp.dest('./dist/server'))
);
gulp.task('generate-style-stubs', ['compile-server-src'], () =>
gulp
.src('./client/**/*.scss')
.pipe(generateScssStub())
.pipe(gulp.dest('./dist/server'))
);
gulp.task('start-nodemon', ['compile-server-src'], done => {
nodemonStream = nodemon({
script: './server/app.js',
watch: ['./server'],
env: {
BABEL_ENV: 'client',
NODE_ENV: 'development'
},
done
});
nodemonStream
.on('restart', () => {
log('restarted!');
})
.on('crash', () => {
error('Application has crashed!\n');
rs(5); // restart the server in 10 seconds
});
});
gulp.task('watch-server-src', () =>
watch(['client/**/*.{js,scss}'], event => {
const { path: filepath, dirname } = event;
const relativePath = dirname.replace(path.dirname(__filename), '.');
const destPath = relativePath.replace(
`.${path.sep}client`,
`.${path.sep}dist${path.sep}server`
);
const ext = path.extname(filepath);
if (ext === '.scss' && isStyleChanged(filepath)) {
const paths = [
`${relativePath}/Container.js`,
`${relativePath}/index.js`
];
gulp
.src(paths)
.pipe(
logger({
before: 'Starting code compiling...',
after: 'Compiling complete!',
showChange: true
})
)
.pipe(babel())
.pipe(gulp.dest(destPath));
gulp
.src(filepath)
.pipe(generateScssStub())
.pipe(gulp.dest(destPath));
rs();
} else if (ext === '.js') {
gulp
.src(filepath)
.pipe(
logger({
before: 'Starting code compiling...',
after: 'Compiling complete!',
showChange: true
})
)
.pipe(babel())
.pipe(gulp.dest(destPath));
}
})
);
gulp.task('default', [
'generate-style-stubs',
'start-nodemon',
'watch-server-src'
]);