-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
199 lines (160 loc) · 4.39 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
195
196
197
198
199
const gulp = require('gulp');
const imagemin = require('gulp-imagemin');
const imageminOptipng = require('imagemin-optipng');
const imageminSvgo = require('imagemin-svgo');
const imageminMozjpeg = require('imagemin-mozjpeg');
const browserSync = require('browser-sync').create();
const sass = require('gulp-sass');
const nodemon = require('gulp-nodemon');
const chalk = require('chalk');
const logSymbols = require('log-symbols');
const dotenv = require('dotenv').config({ path: './config/.env' });
const webpack = require('webpack-stream');
const notify = require('gulp-notify');
const minifyejs = require('gulp-minify-ejs');
const rename = require('gulp-rename');
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const inject = require('gulp-inject-string');
const cleanCSS = require('gulp-clean-css');
gulp.task('bundle', () => {
const stream = gulp
.src('./src/assets/js/app.js')
.pipe(webpack({
watch: true,
devtool: process.env.NODE_ENV === "development" ? 'inline-source-map' : false,
mode: process.env.NODE_ENV || "development",
entry: {
app: './src/assets/js/app.js'
},
output: {
filename: 'app.js',
},
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js'
}
},
performance: {
hints: false,
maxEntrypointSize: 512000,
maxAssetSize: 512000
},
module: {
rules: [
{
test: /\.m?js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
},
{
test: /\.scss$/,
use: [
'vue-style-loader',
'css-loader',
{
loader: 'sass-loader'
}
]
},
{
test: /\.vue$/,
loader: 'vue-loader'
}
]
},
plugins: [
new VueLoaderPlugin()
]
}));
if (process.env.NODE_ENV === "production") {
return stream
.pipe(inject.wrap('<script async>', '</script>'))
.pipe(rename('scripts.ejs'))
.pipe(notify('Success! The JS was compress to .ejs file'))
.pipe(gulp.dest('dist/views/partials'));
} else if (process.env.NODE_ENV === "development") {
return stream
.pipe(gulp.dest('./dist/js'))
.pipe(notify('Success! The scripts were compiled.'));
}
})
/* Compile and minify styles */
gulp.task('sass', () => {
return gulp
.src('src/assets/scss/app.scss')
.pipe(sass().on('error', sass.logError))
.pipe(cleanCSS({ compatibility: 'ie8' }))
.pipe(gulp.dest('./dist/css'))
.pipe(inject.wrap('<style async>', '</style>'))
.pipe(rename('styles.ejs'))
.pipe(gulp.dest('dist/views/partials'))
.pipe(notify('Success! The styles were compiled.'));
});
gulp.task('imagemin', function () {
gulp.src('./src/assets/img/**/*')
.pipe(imagemin([
imageminMozjpeg({
quality: 90
}),
imageminOptipng({
optimizationLevel: 5
}),
imageminSvgo({
plugins: [{
removeViewBox: true
},
{
cleanupIDs: false
}
]
})
]))
.pipe(gulp.dest('./dist/img'))
});
gulp.task('browser-sync', ['nodemon'], function () {
const PORT = process.env.PORT ? process.env.PORT : 5000;
browserSync.init(null, {
proxy: 'http://localhost:' + PORT,
port: 3000,
open: false,
sync: false
});
});
gulp.task('nodemon', function (cb) {
let started = false;
return nodemon({
script: 'index.js',
ext: 'vue html scss js',
ignore: [
".git",
"node_modules/**/*.*",
"./dist/css/app.min.css"
],
watch: [
"./"
]
}).on('start', function () {
if (!started) {
cb();
started = true;
}
});
});
/* Minify HTML */
gulp.task('minify-html', function () {
return gulp
.src(['src/views/**/*', 'src/views/**'])
.pipe(minifyejs())
.pipe(gulp.dest('dist/views'))
});
gulp.task('default', ['browser-sync', 'sass', 'imagemin', 'minify-html'], function () {
gulp.watch("src/views/**/*.*", ['minify-html']);
gulp.watch("src/assets/scss/**/*.*", ['sass']);
console.log(logSymbols.success, chalk.green('Success! The styles and scripts were compiled.'));
});
gulp.task('webpack', ['bundle']);