-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
124 lines (104 loc) · 3.26 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
const { series, parallel, src, dest, watch } = require('gulp');
const livereload = require('gulp-livereload');
const env = require('gulp-env');
const isProd = require('minimist')(process.argv.slice(2)).prod;
const PUBLIC_PATH = 'public/';
env.set({ NODE_TLS_REJECT_UNAUTHORIZED: 0 });
function webpackLogger (err) {
const chalk = require('chalk');
let time = new Date().toTimeString().substr(0,8);
let message = 'Finished ' + chalk.green('webpack') + ' build';
if (err) { message = chalk.red(err); time = chalk.red(time); }
else time = chalk.grey(time);
console.log(`[${time}] ${message}`); /* eslint no-console: 0 */
}
let serverStarted = false;
function server (done) {
const nodemon = require('gulp-nodemon');
return nodemon({ script: './app.js', watch: ['./server'], ext: 'js html' })
.on('start', () => {
if (serverStarted) return;
serverStarted = true;
setTimeout(done, 500);
});
}
function eslint () {
const gulpEslint = require('gulp-eslint');
return src(['client/**/*.js', 'server/**/*.js'])
.pipe(gulpEslint())
.pipe(gulpEslint.format())
.pipe(gulpEslint.failAfterError());
}
function assets () {
return src(['assets/*.*', 'node_modules/chart.js/dist/Chart.min.js'])
.pipe(dest(`${PUBLIC_PATH}`));
}
function styl () {
const cssmin = require('gulp-clean-css');
const sourcemaps = require('gulp-sourcemaps');
const concat = require('gulp-concat');
const stylus = require('gulp-stylus');
const noop = require('through2').obj;
return src(['client/index.styl', 'client/**/*.styl'])
.pipe(isProd ? noop() : sourcemaps.init())
.pipe(stylus({ paths: ['client'], 'include css': true }))
.pipe(isProd ? cssmin({ keepSpecialComments: 0 }) : noop())
.pipe(concat('app.css'))
.pipe(isProd ? noop() : sourcemaps.write())
.pipe(dest(PUBLIC_PATH))
.pipe(livereload());
}
function js () {
const path = require('path');
const webpack = require('webpack');
const webpackStream = require('webpack-stream');
const webpackConfig = {
entry: { index: './client/index.js' },
output: {
filename: 'app.js',
path: path.join(__dirname, 'public'),
publicPath: './public/',
},
resolve: { extensions: ['.js', '.json', '.html'] },
stats: 'normal', // minimal
module: {
rules: [
{ test: /\.js$/, use: 'babel-loader', exclude: /node_modules/ },
{
test: /\.html$/,
exclude: /node_modules/,
use: { loader: 'svelte-loader', options: { css: false } },
},
]
}
};
if (!isProd) {
webpackConfig.devtool = 'inline-source-map';
webpackConfig.mode = 'development';
}
else {
const MinifyPlugin = require('babel-minify-webpack-plugin');
webpackConfig.plugins = [ new MinifyPlugin() ];
}
return src(['client/index.js'])
.pipe(webpackStream(webpackConfig, webpack, webpackLogger))
.on('error', function () { this.emit('end'); })
.pipe(dest(PUBLIC_PATH))
.pipe(livereload());
}
function watchTask () {
if (isProd) return;
livereload.listen();
watch('client/**/*.styl', styl);
watch('client/**/*.js', js);
watch('client/**/*.html', js);
watch('assets/**/*.*', assets);
}
const defaultTask = parallel(assets, styl, eslint, js);
exports.styl = styl;
exports.eslint = eslint;
exports.assets = assets;
exports.js = js;
exports.server = server;
exports.default = defaultTask;
exports.watch = series(defaultTask, watchTask);