-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.babel.js
256 lines (233 loc) · 6.88 KB
/
gulpfile.babel.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
'use strict';
import { src, dest, watch, series, parallel } from 'gulp';
// Importing all the Gulp-related packages we want to use
import autoprefixer from 'autoprefixer';
import babel from 'rollup-plugin-babel';
import browserSyncReuseTab from 'browser-sync-reuse-tab';
import {create as bsCreate} from 'browser-sync';
import cssnano from 'cssnano';
import commonjs from 'rollup-plugin-commonjs';
import del from 'del';
import { eslint } from 'rollup-plugin-eslint';
import gulpif from 'gulp-if';
import minimist from 'minimist';
import notify from 'gulp-notify';
import plumber from 'gulp-plumber';
import postcss from 'gulp-postcss';
import rename from 'gulp-rename';
import resolve from 'rollup-plugin-node-resolve';
import rollup from 'gulp-better-rollup';
import sass from 'gulp-sass';
import sourcemaps from 'gulp-sourcemaps';
import uglify from 'rollup-plugin-uglify';
const browserSync = bsCreate();
const bsReuseTab = browserSyncReuseTab(browserSync);
/**
* ! Options for development / production conditionals
* * default is development env
* * official Gulp recipe for environments
* @ https://github.com/gulpjs/gulp/blob/6b92e9225d20584a4ea3b7fea6b2d9d3fe159e5e/docs/recipes/pass-arguments-from-cli.md
*/
const knownOptions = {
string: 'env',
default: { env: process.env.NODE_ENV || 'development' },
production: { env: process.env.NODE_ENV || 'production' }
};
const options = minimist(process.argv.slice(2), knownOptions);
/**
* ! Set file paths for source and destination.
*/
const dirs = {
src: 'src',
dest: 'dist'
};
const htmlPaths = {
src: `${dirs.src}/**/*.html`,
dest: dirs.dest,
};
const sassPaths = {
src: `${dirs.src}/scss/**/*.scss`,
dest: `${dirs.dest}/css/`
};
const jsPaths = {
src: `${dirs.src}/js/**/*.js`,
dest: `${dirs.dest}/js/`
};
const phpPaths = {
src: `${dirs.src}/**/*.php`,
dest: dirs.dest
};
const fontsPaths = {
src: `${dirs.src}/fonts/*`,
dest: `${dirs.dest}/fonts/`
};
const imagesPaths = {
src: `${dirs.src}/imgs/*`,
dest: `${dirs.dest}/imgs/`
};
/**
* ! Server function:
* * initiate BrowserSync server
* * uses `browser-sync-reuse-tab` to always open in same browser tab
* @ https://www.npmjs.com/package/browser-sync-reuse-tab
* * run with `gulp server`
*/
export function server(done) {
browserSync.init({
server: {
baseDir: './dist/'
},
port: 3000,
injectChanges: true,
open: false // do not automatically open browser, will open in same tab if already opened
}, bsReuseTab);
done();
}
/**
* ! Reload function:
* * reloads BrowserSync server
* * used in watchFiles function
* * run with `gulp reload`
*/
export function reload(done) {
browserSync.reload();
done();
}
/**
* ! HTML function:
* * copies html files from src/ to dist/
* * run with `gulp html`
*/
export function html() {
return src(htmlPaths.src, { allowEmpty: true })
.pipe(dest(htmlPaths.dest))
.pipe(browserSync.stream())
.pipe(notify({ message: 'Task: HTML complete!', onLast: true }));
}
/**
* ! CSS function:
* @ uses 'gulp-better-rollup' https://www.npmjs.com/package/gulp-better-rollup
* * compiles the src/scss/styles.scss file into styles.css
* * copies js files to dist/js/
* * minifies js if in production environment
* * run with `gulp scripts`
*/
export function css(){
return src(sassPaths.src, { allowEmpty: true })
.pipe(plumber()) // initialize plumber first
.pipe(sourcemaps.init()) // initialize sourcemaps
.pipe(sass().on('error', sass.logError)) // convert sass
.pipe(postcss([autoprefixer()])) // run postcss autoprefixer
.pipe(gulpif(options.env === 'production', postcss([cssnano()]))) // minify css if production environment
.pipe(sourcemaps.write('.')) // write sourcemaps file in current directory
.pipe(plumber.stop())
.pipe(dest(sassPaths.dest)) // put CSS and sourcemaps in dist/css folder
.pipe(browserSync.stream())
.pipe(notify({ message: 'TASK: CSS complete!', onLast: true })
);
}
/**
* ! Scripts function:
* @ uses 'gulp-better-rollup' https://www.npmjs.com/package/gulp-better-rollup
* * checks for js errors
* * copies js files to dist/js/
* * minifies js if in production environment
* * run with `gulp scripts`
*/
export function scripts() {
return src(jsPaths.src, { allowEmpty: true })
.pipe(plumber()) // initialize plumber first
.pipe(sourcemaps.init()) // initialize sourcemaps
.pipe(rollup({
plugins: [
resolve(),
commonjs(),
eslint(),
babel({
exclude: 'node_modules/**'
}),
gulpif(options.env === 'production', uglify.uglify()) // minify js if in production environment
]
},{
format: 'iife'
}))
.pipe(rename({ suffix: '.min' })) // rename file to use .min.js
.pipe(sourcemaps.write('.')) // write sourcemaps file in current directory
.pipe(plumber.stop())
.pipe(dest(jsPaths.dest)) // put js files and sourcemaps in dist/js folder
.pipe(browserSync.stream())
.pipe(notify({ message: 'TASK: "scripts" completed!', onLast: true }));
}
/**
* ! PHP function:
* * copies all php files to dist/
* * run with `gulp php`
*/
export function php() {
return src(phpPaths.src, { allowEmpty: true })
.pipe(dest(phpPaths.dest))
.pipe(browserSync.stream())
.pipe(notify({ message: 'TASK: "php" completed!', onLast: true }));
}
/**
* ! Fonts function:
* * copies font files to dist/fonts/
* * run with `gulp fonts`
*/
export function fonts() {
return src(fontsPaths.src, { allowEmpty: true })
.pipe(dest(fontsPaths.dest))
.pipe(browserSync.stream())
.pipe(notify({ message: 'TASK: "fonts" completed', onLast: true })
);
}
/**
* ! Images function:
* * copies image files to dist/img/
* * run with `gulp images`
*/
export function images() {
return src(imagesPaths.src, { allowEmpty: true })
.pipe(dest(imagesPaths.dest))
.pipe(browserSync.stream())
.pipe(notify({ message: 'TASK: "images" completed', onLast: true })
);
}
/**
* ! Clean function:
* * deletes dist/ folder for a clean build
* * run with `gulp clean`
*/
export function clean() {
return del([dirs.dest]);
}
/**
* ! WatchFiles function:
* * watches for changes
* * runs coresponding functions if changes
* * reloads BrowserSync
* * run with `gulp watchFiles`
*/
export function watchFiles() {
watch(htmlPaths.src, series(html, reload));
watch(sassPaths.src, series(css, reload));
watch(phpPaths.src, series(php, reload));
watch(fontsPaths.src, series(fonts, reload));
watch(imagesPaths.src, series(images, reload));
}
/**
* ! Build:
* * deletes the dist/ files
* * runs other fucntions to rebuild dist/
* * run with `gulp build`
*/
export const build = series(clean, parallel(scripts, css, html, php, fonts, images));
/**
* ! Gulp default:
* * deletes the dist/ files
* * runs other fucntions to rebuild dist/
* * starts BrowserSync server
* * watches files for changes and reloads browser if changes
* * run with `gulp`
*/
exports.default = series(build, server, watchFiles);