-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
194 lines (165 loc) · 4.65 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
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
const gulp = require('gulp'),
browserSync = require('browser-sync'),
livereload = require('gulp-livereload'),
sourcemaps = require('gulp-sourcemaps'),
del = require('del'),
webpack = require('webpack-stream'),
gulpif = require('gulp-if'),
VueLoaderPlugin = require('vue-loader/lib/plugin')
/* === Красивое отображение ошибок === */
const notify = require('gulp-notify')
function emit_end(err) {
this.emit('end')
}
/* --- Красивое отображение ошибок --- */
/* === Файлы проекта === */
const conf = {
src: './app',
dest: './build',
dest_assets: './build/assets'
}
const html_files = [
conf.src + '/**/*.html',
]
const js_files = [
'!' + conf.src + '/**/*.map',
conf.src + '/**/*.js',
conf.src + '/*.js',
conf.src + '/**/*.vue',
conf.src + '/*.vue'
]
const assets_files = [
conf.src + '/assets/**/*.*'
]
var isDev = false // Прод
// let isDev = true // Дев
var isProd = !isDev
var webpack_config = {
output: {
filename: 'main.js'
},
module: {
rules: [{
test: /\.txt$|\.png$|\.jpg$|\.jpeg$|\.svg$/i,
use: [{
loader: 'url-loader',
options: {
fallback: 'file-loader',
},
}, ],
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: '/node_modules/'
},
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
hotReload: false
}
},
{
test: /\.css$/,
use: [
'vue-style-loader',
'css-loader'
]
},
{
test: /\.scss$/,
use: [
'vue-style-loader',
'css-loader',
'sass-loader'
]
},
{
test: /\.sass$/,
use: [
'vue-style-loader',
'css-loader',
{
loader: 'sass-loader',
options: {
indentedSyntax: true
}
}
]
}
]
},
plugins: [
new VueLoaderPlugin()
],
mode: isDev ? 'development' : 'production',
devtool: isDev ? 'eval-source-map' : 'none',
}
/* --- Файлы проекта --- */
// Пользовательские скрипты проекта
function browser_sync() {
browserSync({
server: {
baseDir: conf.dest // './build'
},
notify: false,
open: false,
reloadOnRestart: true,
cors: true,
})
}
function js() {
return gulp.src(conf.src + '/main.js')
.pipe(webpack(webpack_config).on("error", notify.onError(function(error) {
return "Error webpack: " + error.message;
})).on('error', emit_end))
.pipe(gulpif(isDev, sourcemaps.init({ loadMaps: true })))
.pipe(gulpif(isDev, gulp.dest(conf.dest)))
.pipe(gulpif(isProd, gulp.dest(conf.dest)))
.pipe(gulpif(isDev, sourcemaps.write(conf.dest)))
.pipe(browserSync.reload({ stream: true }))
.pipe(livereload())
}
function html() {
return gulp.src(html_files)
.pipe(gulp.dest(conf.dest))
.pipe(browserSync.reload({ stream: true }))
.pipe(livereload())
}
gulp.task('watch', ['setDev', 'build', 'browser_sync'], function() {
livereload.listen()
gulp.watch(html_files, ['html'])
gulp.watch(js_files, ['js'])
})
gulp.task('build', ['removedist', 'livereload2build', 'js'], function() {
// return gulp.src(assets_files)
// .pipe(gulp.dest(conf.dest_assets))
})
function livereload2build() {
if (isDev) {
return gulp.src([
conf.src + '/livereload.js',
conf.src + '/index.html',
])
.pipe(gulp.dest(conf.dest))
}
}
function removedist() {
try {
return del.sync(conf.dest)
} catch (err) {}
}
function setDev() {
isDev = true
isProd = false
webpack_config.mode = isDev ? 'development' : 'production'
webpack_config.devtool = isDev ? 'eval-source-map' : 'none'
}
gulp.task('browser_sync', browser_sync)
gulp.task('js', js)
gulp.task('html', html)
gulp.task('removedist', removedist)
gulp.task('livereload2build', livereload2build)
gulp.task('setDev', setDev)
gulp.task('default', ['watch'])