generated from codyhouse/codyhouse-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
160 lines (142 loc) · 4.64 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
var gulp = require('gulp');
var sass = require('gulp-sass')(require('sass-embedded'));
var sassGlob = require('gulp-sass-glob');
var browserSync = require('browser-sync').create();
var postcss = require('gulp-postcss');
var autoprefixer = require('autoprefixer');
var concat = require('gulp-concat');
var rename = require('gulp-rename');
var uglify = require('gulp-uglify');
var cleanCSS = require('gulp-clean-css');
var purgecss = require('gulp-purgecss');
// js file paths
var utilJsPath = 'main/assets/js'; // util.js path - you may need to update this if including the framework as external node module
var componentsJsPath = 'main/assets/js/components/*.js'; // component js files
var scriptsJsPath = 'main/assets/js'; //folder for final scripts.js/scripts.min.js files
// css file paths
var cssFolder = 'main/assets/css'; // folder for final style.css/style-custom-prop-fallbac.css files
var scssFilesPath = 'main/assets/css/**/*.scss'; // scss files to watch
function reload(done) {
browserSync.reload();
done();
}
/* Gulp watch tasks */
// This task is used to convert the scss to css and compress it.
gulp.task('sass', function() {
return gulp.src(scssFilesPath)
.pipe(sassGlob({sassModules: true}))
.pipe(sass().on('error', sass.logError))
.pipe(postcss([autoprefixer()]))
.pipe(gulp.dest(cssFolder))
.pipe(browserSync.reload({
stream: true
}))
.pipe(rename('style.min.css'))
.pipe(cleanCSS())
.pipe(gulp.dest(cssFolder))
.pipe(browserSync.reload({
stream: true
}));
});
// This task is used to combine all js files in a single scripts.min.js.
gulp.task('scripts', function() {
return gulp.src([utilJsPath+'/util.js', componentsJsPath])
.pipe(concat('scripts.js'))
.pipe(gulp.dest(scriptsJsPath))
.pipe(browserSync.reload({
stream: true
}))
.pipe(rename('scripts.min.js'))
.pipe(uglify())
.pipe(gulp.dest(scriptsJsPath))
.pipe(browserSync.reload({
stream: true
}));
});
// This task is used to reload the project whan changes are made to a html/scss/js file.
gulp.task('browserSync', gulp.series(function (done) {
browserSync.init({
server: {
baseDir: 'main'
},
notify: false
})
done();
}));
gulp.task('watch', gulp.series(['browserSync', 'sass', 'scripts'], function () {
gulp.watch('main/*.html', gulp.series(reload));
gulp.watch('main/assets/css/**/*.scss', gulp.series(['sass']));
gulp.watch(componentsJsPath, gulp.series(['scripts']));
}));
/* Gulp dist task */
// create a distribution folder for production
var distFolder = 'dist/';
var assetsFolder = 'dist/assets/';
gulp.task('dist', async function(){
// remove unused classes from the style.css file with PurgeCSS and copy it to the dist folder
await purgeCSS();
// minify the scripts.js file and copy it to the dist folder
await minifyJs();
// copy any additional js files to the dist folder
await moveJS();
// copy all the assets inside main/assets/img folder to the dist folder
await moveAssets();
// copy all html files inside main folder to the dist folder
await moveContent();
console.log('Distribution task completed!')
});
function purgeCSS() {
return new Promise(function(resolve, reject) {
var stream = gulp.src(cssFolder+'/style.css')
.pipe(purgecss({
content: ['main/*.html', scriptsJsPath+'/scripts.js'],
safelist: {
standard: ['.is-hidden', '.is-visible'],
deep: [/class$/],
greedy: []
},
defaultExtractor: content => content.match(/[\w-/:%@]+(?<!:)/g) || []
}))
.pipe(gulp.dest(distFolder+'/assets/css'));
stream.on('finish', function() {
resolve();
});
});
};
function minifyJs() {
return new Promise(function(resolve, reject) {
var stream = gulp.src(scriptsJsPath+'/scripts.js')
.pipe(uglify())
.pipe(gulp.dest(distFolder+'/assets/js'));
stream.on('finish', function() {
resolve();
});
});
};
function moveJS() {
return new Promise(function(resolve, reject) {
var stream = gulp.src([scriptsJsPath+'/*.js', '!'+scriptsJsPath+'/scripts.js', '!'+scriptsJsPath+'/scripts.min.js'], { allowEmpty: true })
.pipe(gulp.dest(assetsFolder+'js'));
stream.on('finish', function() {
resolve();
});
});
};
function moveAssets() {
return new Promise(function(resolve, reject) {
var stream = gulp.src(['main/assets/img/**'], { allowEmpty: true })
.pipe(gulp.dest(assetsFolder+'img'));
stream.on('finish', function() {
resolve();
});
});
};
function moveContent() {
return new Promise(function(resolve, reject) {
var stream = gulp.src('main/*.html')
.pipe(gulp.dest(distFolder));
stream.on('finish', function() {
resolve();
});
});
};