-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
127 lines (108 loc) · 3.05 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
const gulp = require('gulp')
const sass = require('gulp-sass')
const babel = require('gulp-babel')
const postcss = require('gulp-postcss')
const autoprefixer = require('autoprefixer')
const sourcemaps = require('gulp-sourcemaps')
const cssnano = require('cssnano')
const rename = require('gulp-rename')
const browsersync = require('browser-sync').create()
const plumber = require('gulp-plumber')
const nodemon = require('gulp-nodemon')
const del = require('del')
const eslint = require('gulp-eslint')
const merge = require('merge-stream')
// Clean assets
function clean() {
return del(['./dist/'])
}
function css() {
const plugins = [autoprefixer(), cssnano()]
return gulp.src(['./app/scss/**/*.scss', './app/css/**/*.css'])
.pipe(sass().on('error', sass.logError))
.pipe(postcss(plugins))
.pipe(rename({ suffix: '.min' }))
.pipe(gulp.dest('./dist/app/css'))
.pipe(browsersync.stream())
}
// Lint scripts
function scriptsLint() {
return gulp
.src(['./app/**/*.js', './server/**/*.js', './gulpfile.js'])
.pipe(plumber())
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError())
}
function scripts() {
// Bootstrap
const bootstrap = gulp.src('./node_modules/bootstrap/dist/**/*')
.pipe(gulp.dest('./dist/app/vendor/bootstrap'))
// jQuery
const jquery = gulp.src([
'./node_modules/jquery/dist/*',
'!./node_modules/jquery/dist/core.js'
]).pipe(gulp.dest('./dist/app/vendor/jquery'))
const others = ['app/js', 'server'].map((el) => {
return merge(
gulp.src(`./${el}/**/*.js`)
.pipe(sourcemaps.init())
.pipe(plumber({
errorHandler: onError
}))
.pipe(babel({
presets: ['@babel/preset-env']
}))
.pipe(sourcemaps.write('.')),
gulp.src(`./${el}/**/*.json`)
).pipe(gulp.dest(`./dist/${el}/`))
})
// TODO: Po zmianie skryptu w game.js restartuje się serwer
return merge(bootstrap, jquery, others)
}
function resources() {
const files = gulp.src('./app/static/**/*')
.pipe(gulp.dest('./dist/app/static'))
const html = gulp.src('./app/*.html')
.pipe(gulp.dest('./dist/app/'))
return merge(files, html)
}
const onError = (err) => {
console.log(err)
}
function run(cb) {
return nodemon({
script: './dist/server/main.js'
}).on('start', () => {
browsersync.reload()
cb()
})
}
async function watch() {
gulp.watch('./dist/app/**/*').on('change', browsersync.reload)
gulp.watch(['./app/*.html', './app/static/**/*']).on('change', resources)
gulp.watch('./app/css/**/*', css)
gulp.watch(['./app/js/**/*.js', './server/**/*'], js)
}
async function browserSync() {
browsersync.init(null, {
proxy: 'http://localhost:' + (process.env.PORT || 3000),
files: ['./dist/app/**/*.*'],
port: 7000,
ws: true
})
}
const js = gulp.series(scriptsLint, scripts)
const build = gulp.series(clean, gulp.parallel(css, js, resources))
module.exports = {
nodemon,
js,
css,
run,
watch,
browserSync,
scripts,
scriptsLint,
build,
default: gulp.series(build, watch, run, browserSync)
}