-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
139 lines (119 loc) · 3.67 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
const {task, series, parallel, src, dest, watch} = require('gulp');
const sass = require('gulp-sass')(require('sass'));
const dc = require('postcss-discard-comments');
const browserSync = require('browser-sync');
const postcss = require('gulp-postcss');
const csscomb = require('gulp-csscomb');
const cssnano = require('cssnano');
const rename = require('gulp-rename');
const autoprefixer = require('autoprefixer');
const mqpacker = require('css-mqpacker');
const sortCSSmq = require('sort-css-media-queries');
const option = process.argv[3];
const PATH = {
scssFolder: './assets/scss/',
scssFiles: './assets/scss/**/*.scss',
scssRoot: './assets/scss/style.scss',
cssFolder: './assets/css/',
cssFiles: './assets/css/*.css',
cssFile: './assets/css/style.css',
htmlFolder: './',
htmlFiles: './*.html',
jsFolder: './assets/js/',
jsFiles: './assets/js/**/*.js',
imgFolder: './assets/img/'
};
const PLUGINS = [
dc({ discardComments: true }),
autoprefixer({
overrideBrowserslist: [
'last 5 versions',
'> 0.1%'
],
cascade: true
}),
mqpacker({sort: sortCSSmq})
];
function scss() {
return src(PATH.scssRoot)
.pipe(sass({outputStyle: 'expanded'}).on('error', sass.logError))
.pipe(postcss(PLUGINS))
.pipe(csscomb())
.pipe(dest(PATH.cssFolder))
.pipe(browserSync.stream());
}
function scssMin() {
const pluginsForMinify = [...PLUGINS, cssnano({preset: 'default'})];
return src(PATH.scssRoot)
.pipe(sass().on('error', sass.logError))
.pipe(csscomb())
.pipe(postcss(pluginsForMinify))
.pipe(rename({ suffix: '.min' }))
.pipe(dest(PATH.cssFolder))
}
function scssDev() {
const pluginsForDevMode = [...PLUGINS]
pluginsForDevMode.splice(1,1)
return src(PATH.scssRoot, {sourcemaps: true})
.pipe(sass({outputStyle: 'expanded'}).on('error', sass.logError))
.pipe(postcss(pluginsForDevMode))
.pipe(dest(PATH.cssFolder, {sourcemaps: true}))
.pipe(browserSync.stream());
}
function comb() {
return src(PATH.scssFiles)
.pipe(csscomb())
.pipe(dest(PATH.scssFolder));
}
function syncInit() {
browserSync({
server: {baseDir: './'},
notify: false
});
}
async function sync() {
browserSync.reload();
}
function watchFiles() {
syncInit();
if (!option) watch(PATH.scssFiles, series(scss, scssMin));
if (option === '--dev') watch(PATH.scssFiles, series(scssDev));
if (option === '--css') watch(PATH.cssFiles, sync);
watch(PATH.htmlFiles, sync);
watch(PATH.jsFiles, sync);
}
function createStructure() {
let file = [];
let scssFiles = [];
scssFiles[0] = `${PATH.scssFolder}style.scss`;
scssFiles[1] = `${PATH.scssFolder}_variables.scss`;
scssFiles[2] = `${PATH.scssFolder}_skin.scss`;
scssFiles[3] = `${PATH.scssFolder}_common.scss`;
scssFiles[4] = `${PATH.scssFolder}_footer.scss`;
scssFiles[5] = `${PATH.scssFolder}_header.scss`;
file[0] = `${PATH.htmlFolder}index.html`;
file[1] = `${PATH.cssFolder}style.css`;
file[2] = `${PATH.jsFolder}main.js`;
file[3] = scssFiles;
src('*.*', {read: false})
.pipe(dest(PATH.scssFolder))
.pipe(dest(PATH.cssFolder))
.pipe(dest(PATH.jsFolder))
.pipe(dest(PATH.imgFolder));
return new Promise((resolve) => setTimeout(() => {
for (let i = 0; i < file.length; i++) if (!Array.isArray(file[i])) {
require('fs').writeFileSync(file[i], '');
console.log(file[i]);
} else for (let j = 0; j < file[i].length; j++) {
require('fs').writeFileSync(file[i][j], '');
console.log(file[i][j]);
}
resolve(true);
}, 1000));
}
task('comb', series(comb));
task('scss', series(scss));
task('dev', series(scssDev));
task('min', series(scssMin));
task('cs', series(createStructure));
task('watch', watchFiles);