-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
146 lines (133 loc) · 4.03 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
// Import everything important
const gulp = require('gulp');
const plumber = require("gulp-plumber");
const rename = require("gulp-rename");
const browserSync = require('browser-sync').create();
const browserify = require('gulp-browserify');
const gutil = require('gulp-util');
const sourcemaps = require('gulp-sourcemaps');
// For SASS -> CSS
const sass = require('gulp-sass');
const postcss = require("gulp-postcss");
const autoprefixer = require("autoprefixer");
const cssnano = require("cssnano");
const sassLint = require('gulp-sass-lint');
// HTML
const htmlmin = require('gulp-htmlmin');
// JavaScript/TypeScript
const babel = require('gulp-babel');
const jshint = require('gulp-jshint');
const uglify = require('gulp-uglify');
const concat = require('gulp-concat');
// Define Important Varaibles
const src = './src';
const dest = './dist';
// Function for reload the Browser
const reload = (done) => {
browserSync.reload();
done();
};
// Function for serve the dev server in borwsaer
const serve = (done) => {
browserSync.init({
server: {
baseDir: `${dest}`
}
});
done();
};
// Compile sass into css with gulp
const css = () => {
// Find SASS
return gulp.src(`${src}/sass/**/*.sass`)
// Init Plumber
.pipe(plumber())
// Lint SASS
.pipe(sassLint({
options: {
formatter: 'stylish',
},
rules: {
'no-ids': 1,
'final-newline': 0,
'no-mergeable-selectors': 1,
'indentation': 0
}
}))
// Format SASS
.pipe(sassLint.format())
// Start Source Map
.pipe(sourcemaps.init())
// Compile SASS -> CSS
.pipe(sass.sync({ outputStyle: "compressed" })).on('error', sass.logError)
// add SUffix
.pipe(rename({ basename: 'main', suffix: ".min" }))
// Add Autoprefixer & cssNano
.pipe(postcss([autoprefixer(), cssnano()]))
// Write Source Map
.pipe(sourcemaps.write(''))
// Write everything to destination folder
.pipe(gulp.dest(`${dest}/css`))
// Reload Page
.pipe(browserSync.stream());
};
// Compile .html to minify .html
const html = () => {
// Find SASS
return gulp.src(`${src}/*.html`)
// Init Plumber
.pipe(plumber())
// Compile HTML -> minified HTML
.pipe(htmlmin({
collapseWhitespace: true,
removeComments: true,
html5: true,
removeEmptyAttributes: true,
removeTagWhitespace: true,
sortAttributes: true,
sortClassName: true
}))
// Write everything to destination folder
.pipe(gulp.dest(`${dest}`));
};
// Compile .js to minify .js
const script = () => {
// Find SASS
return gulp.src(`${src}/js/**/*.js`)
// Init Plumber
.pipe(plumber(((error) => {
gutil.log(error.message);
})))
// Start useing source maps
.pipe(sourcemaps.init())
// Use Babel
.pipe(babel({
presets: ['@babel/env']
}))
// concat
.pipe(concat('concat.js'))
// Browserfy
.pipe(
browserify({})
)
// Report of jslint
.pipe(jshint.reporter('jshint-stylish'))
// Minify
.pipe(uglify())
// add SUffix
.pipe(rename({ basename: 'main', suffix: ".min" }))
// Write Sourcemap
.pipe(sourcemaps.write(''))
// Write everything to destination folder
.pipe(gulp.dest(`${dest}/js`));
};
// Function to watch our Changes and refreash page
const watch = () => gulp.watch([`${src}/*.html`, `${src}/js/**/*.js`, `${src}/sass/**/*.sass`], gulp.series(css, script, html, reload));
// All Tasks for this Project
const dev = gulp.series(css, script, html, serve, watch);
// Just Build the Project
const build = gulp.series(css, script, html);
// Default function (used when type gulp)
exports.dev = dev;
exports.build = build;
exports.default = build;