-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgulpfile.js
151 lines (131 loc) · 3.84 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
const gulp = require('gulp');
const watch = require ('gulp-watch');
const browserSync = require('browser-sync').create();
const postcss = require('gulp-postcss');
const autoprefixer = require('autoprefixer');
const cssvars = require('postcss-simple-vars');
const nested = require('postcss-nested');
const cssImport = require('postcss-import');
const mixins = require('postcss-mixins');
const hexrgba = require('postcss-hexrgba');
const webpack = require ('webpack');
const del = require('del');
const cleanCSS = require('gulp-clean-css');
const rev = require('gulp-rev');
const revReplace = require("gulp-rev-replace");
//compiles public css from source
function cssCompile() {
return gulp.src('./_dev/css/app.css') //source
.pipe(postcss([cssImport, mixins, cssvars, nested, hexrgba, autoprefixer]))
.on('error', function(errorInfo) {
console.log(errorInfo.toString());
this.emit('end');
})
.pipe(gulp.dest('./css/')); //save to destination
}
//injects public css from source
function cssInject() {
return gulp.src('./css/app.css')
.pipe(browserSync.stream());
}
function jsCompile(callback) {
webpack(require('./webpack.config.js'), function(err, stats) {
if (err) {
console.log(err.toString());
}
console.log(stats.toString());
callback();
});
}
//reload browser
function reload() {
browserSync.reload();
}
//browser sync
function browser_sync() {
browserSync.init({
server: {
baseDir: "./"
}
});
}
//files to be watched
var publicStyleWatch = './_dev/css/**/*.css';
var publicScriptsWatch = './_dev/js/**/*.js';
var htmlWatch = './**/*.html';
//watch files and do things on change
function watch_files() {
watch(publicStyleWatch, gulp.series(cssCompile, cssInject));
watch(htmlWatch, reload);
watch(publicScriptsWatch, gulp.series(jsCompile, reload));
}
//define the watch task
gulp.task('watch', gulp.parallel(browser_sync, watch_files));
// CREATE A PRODUCTION VERSION
gulp.task('build', gulp.series(clean, copy_files, minify_css, uglify_js, bust_css_cache, bust_js_cache, rewrite_css, rewrite_js));
function clean() {
return del(['./dist/']);
}
function copy_files() {
var pathsToCopy = [
'./**/*',
'!./_dev/**',
'!./node_modules/**',
'!./.git/**',
'!./gulpfile.js',
'!./webpack.config.js',
'!./webpack.production.config.js',
'!./package.json',
'!./package-lock.json',
'!./.gitignore',
'!./css/app.css',
'!./js/app.js',
'!./LICENSE.md',
'!./README.md',
]
return gulp.src(pathsToCopy) //source
.pipe(gulp.dest('./dist/')); //save to destination
}
function minify_css() {
return gulp.src('./css/app.css') //source
.pipe(cleanCSS({compatibility: 'ie8', debug: true}, (details) => {
console.log(`${details.name}: ${details.stats.originalSize}`);
console.log(`${details.name}: ${details.stats.minifiedSize}`);
}))
.pipe(gulp.dest('./dist/css/')); //save to destination
}
function uglify_js(callback) {
webpack(require('./webpack.production.config.js'), function(err, stats) {
if (err) {
console.log(err.toString());
}
console.log(stats.toString());
callback();
});
}
function bust_css_cache() {
return gulp.src('./dist/css/app.css')
.pipe(rev())
.pipe(gulp.dest('./dist/css'))
.pipe(rev.manifest())
.pipe(gulp.dest('./dist/css'));
}
function bust_js_cache() {
return gulp.src('./dist/js/app.js')
.pipe(rev())
.pipe(gulp.dest('./dist/js'))
.pipe(rev.manifest())
.pipe(gulp.dest('./dist/js'));
}
function rewrite_css() {
let manifest = gulp.src('./dist/css/rev-manifest.json');
return gulp.src('./dist/index.html')
.pipe(revReplace({ manifest }))
.pipe(gulp.dest('./dist/'));
}
function rewrite_js() {
let manifest = gulp.src('./dist/js/rev-manifest.json');
return gulp.src('./dist/index.html')
.pipe(revReplace({ manifest }))
.pipe(gulp.dest('./dist/'));
}