-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.babel.js
197 lines (178 loc) · 4.88 KB
/
gulpfile.babel.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
'use strict';
import gulp from 'gulp';
import gulpLoadPlugins from 'gulp-load-plugins';
import nodemon from 'nodemon';
import webpack from 'webpack';
import webpackStream from 'webpack-stream';
import webpackDevMiddleware from 'webpack-dev-middleware';
import webpackConfig from './webpack.config.js';
import browserSyncImport from 'browser-sync';
import stripAnsi from 'strip-ansi';
import del from 'del';
import lazypipe from 'lazypipe';
import _ from 'lodash';
import rs from 'run-sequence';
import opn from 'opn';
const runSequence = rs.use(gulp);
const browserSync = browserSyncImport.create();
const plugins = gulpLoadPlugins();
const serverPath = 'server';
const clientPath = 'client';
const paths = {
dist: 'dist',
client: {
manifest: `${clientPath}/manifest.json`,
assets: `${clientPath}/assets/**/*`,
},
server: {
scripts: [
`${serverPath}/**/!(*.spec|*.integration).js`,
`!${serverPath}/config/local.env.sample.js`
]
}
};
let transpileServer = lazypipe()
.pipe(plugins.sourcemaps.init)
.pipe(plugins.babel, {
plugins: [
'transform-class-properties',
'transform-runtime'
]
})
.pipe(plugins.sourcemaps.write, '.');
function onServerLog(log) {
/*eslint-disable*/
console.log(
plugins.util.colors.white('[') +
plugins.util.colors.yellow('nodemon') +
plugins.util.colors.white('] ') +
log.message
);
/*eslint-enable*/
}
gulp.task('eslint', () => {
return gulp
.src([
'server/**/*.js',
'client/scripts/**/*.js',
'client/**/*.html',
'gulpfile.babel.js'
])
.pipe(plugins.eslint())
.pipe(plugins.eslint.format())
.pipe(plugins.if(!browserSync.active, plugins.eslint.failOnError()));
});
gulp.task('start:server:demo', () => {
process.env.NODE_ENV = 'demo';
nodemon(`-w ${serverPath} ${serverPath}`).on('log', onServerLog);
});
gulp.task('start:server', () => {
process.env.NODE_ENV = 'development';
nodemon(`-w ${serverPath} ${serverPath}`).on('log', onServerLog);
});
gulp.task('start:server:prod', () => {
process.env.NODE_ENV = 'production';
process.env.SESSION_SECRET = 'AWESOME_SECRET';
console.log(`-w ${serverPath} ${serverPath}`);
nodemon(`dist/${serverPath} app.js`).on('log', onServerLog);
});
gulp.task('start:client', () => {
const config = require('./server/config/environment');
const compiler = webpack(webpackConfig.get({ build: false }));
const port = 3000;
browserSync.init({
proxy: `localhost:${config.port}`,
plugins: ['bs-fullscreen-message'],
open: false,
//port: port,
middleware: [
webpackDevMiddleware(compiler, {
noInfo: false,
stats: {
colors: true,
timings: true,
chunks: false
},
lazy: false,
watchOptions: {
aggregateTimeout: 1000,
poll: true
}
})
]
});
/**
* Reload all devices when bundle is complete
* or send a fullscreen error message to the browser instead
*/
var compiledFirstTime = true;
compiler.plugin('done', function(stats) {
if (compiledFirstTime) {
opn(`http://localhost:${port}`);
compiledFirstTime = false;
}
if (stats.hasErrors() || stats.hasWarnings()) {
return browserSync.sockets.emit('fullscreen:message', {
title: 'Webpack Error:',
body: stripAnsi(stats.toString()),
timeout: 100000
});
}
browserSync.reload();
});
});
gulp.task('transpile:server', () => {
return gulp
.src(_.union(paths.server.scripts))
.pipe(transpileServer())
.pipe(gulp.dest(`${paths.dist}/${serverPath}`));
});
gulp.task('serve', ['start:server', 'start:client']);
gulp.task('serve:demo', ['start:server:demo', 'start:client']);
gulp.task('serve:dist', cb => {
runSequence(
'build',
'start:server:prod',
cb
);
});
gulp.task('webpack:dist', function() {
const webpackDistConfig = webpackConfig.get({ build: true });
return gulp
.src(webpackDistConfig.entry.app)
.pipe(webpackStream(webpackDistConfig, webpack))
.on('error', (err) => {
this.emit('end'); // Recover from errors
})
.pipe(gulp.dest(`${paths.dist}/public`));
});
gulp.task('build', cb => {
runSequence(
[
'clean:dist',
'clean:tmp'
],
'transpile:server',
'copy:assets',
'copy:manifest',
'copy:server',
'webpack:dist',
cb);
});
gulp.task('clean:dist', () => del([`${paths.dist}`], { dot: true }));
gulp.task('clean:tmp', () => del(['.tmp/**/*'], { dot: true }));
gulp.task('copy:assets', () => {
return gulp.src([paths.client.assets])
.pipe(gulp.dest(`${paths.dist}/public/assets`));
});
gulp.task('copy:manifest', () => {
return gulp.src([paths.client.manifest])
.pipe(gulp.dest(`${paths.dist}/public`));
});
gulp.task('copy:server', () => {
return gulp.src([
'package.json'
], { cwdbase: true })
.pipe(gulp.dest(paths.dist));
});
gulp.task('default', ['build']);