This repository has been archived by the owner on Jan 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
195 lines (173 loc) · 5.38 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
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
const path = require('path'),
del = require('del'),
chalk = require('chalk'),
dedent = require('dedent-js'),
gulp = require('gulp'),
GulpClass = require('classy-gulp'),
/**
* Gulp plugins starting with "gulp-<name>" are loaded automatically under gulpPlugins.<name>
* You can rename them or call functions on required plugins via options object passed to gulp-load-plugins:
* {
* rename: {},
* postRequireTransforms: {}
* }
* Others are manually appended via the second array.
*/
gulpPlugins = {
...require('gulp-load-plugins')(),
...{
browserSync: require('browser-sync').create(),
webpack: require('webpack-stream'),
yargs: require('yargs'),
/**
* enables us to define webpack entry points via gulp.src
*/
named: require('vinyl-named'),
},
},
gulpOptions = {
...require('./config/gulp.config'),
...{
production: !!(gulpPlugins.yargs.argv.production),
analyzeWebpack: !!(gulpPlugins.yargs.argv.analyze),
},
};
class Flow extends GulpClass {
constructor() {
super();
/**
* A friendly greeting for you, you beautiful ;)
*/
console.log(chalk.blue(dedent(`
* Hey! I'm gulp. ༼つಠ益ಠ༽つ ─=≡ΣO))
* Your personal workflow magician.
`)));
}
defineTasks() {
/**
* All tasks which are accessible via "gulp <taskName>" are defined here.
*/
return {
build: gulp.series(this.clean, gulp.parallel(this.copyFiles, this.styles, this.scripts)),
nodemon: gulp.series(this.server),
server: gulp.series(this.server, this.startBrowserSync, (done) => {
done();
}),
deploy: gulp.series('build'),
default: gulp.series('build', 'server', this.watch),
};
}
/**
* Runs everything we need to do with CSS.
*/
styles() {
return gulp.src([path.join(gulpOptions.styles.path.scss, '/**/*.scss')])
.pipe(gulpPlugins.plumber())
.pipe(gulpPlugins.sourcemaps.init())
.pipe(gulpPlugins.sass(gulpOptions.styles.sassOptions).on('error', gulpPlugins.sass.logError))
.pipe(gulpPlugins.autoprefixer({ browsers: gulpOptions.styles.autoprefixerCompatibility }))
.pipe(gulpPlugins.sourcemaps.write('.'))
.pipe(gulp.dest(gulpOptions.styles.path.css))
.pipe(gulpPlugins.browserSync.stream({ match: '**/*.{css|map}' }));
}
/**
* Runs everything we need to do with JS.
*/
scripts() {
return gulp.src([path.join(gulpOptions.js.src, gulpOptions.js.entryFile)])
.pipe(gulpPlugins.plumber())
.pipe(gulpPlugins.named())
.pipe(gulpPlugins.webpack(require('./config/webpack.config.js')({
environment: gulpOptions.production ? 'production' : 'development',
analyze: gulpOptions.analyzeWebpack,
})))
.pipe(gulp.dest(gulpOptions.js.dist));
}
copyFiles() {
return gulp.src(gulpOptions.copyFiles, { base: 'src' })
.pipe(gulp.dest('dist'));
}
/**
* Watches for changes and automatically performs a given task depending on the type of file changed.
* @param {function} [done] - An automatically assigned and invoked callback to signal asynchronous completion (do not use!)
*/
watch(done) {
gulp.watch(path.join(gulpOptions.styles.path.scss, '/**/*.scss'), this.styles);
gulp.watch(path.join(gulpOptions.html.src, `/**/*${gulpOptions.html.ext}`), gulp.series(this.reload));
gulp.watch(path.join(gulpOptions.js.src, '**/*.js'), gulp.series(this.scripts, this.reload));
done();
}
/**
* ========= "Utility" classes start =========
*/
/**
* ========= BrowserSync =========
*/
/**
* Starts the browsersync proxy server
* @param {function} [done] - An automatically assigned and invoked callback to signal asynchronius completion (do not use!)
*/
startBrowserSync(done) {
setTimeout(() => {
gulpPlugins.browserSync.init(gulpOptions.app.browserSync);
done();
}, 1000);
}
/**
* Reloads the whole page via browsersync
* @param {function} [done] - An automatically assigned and invoked callback to signal asynchronius completion (do not use!)
*/
reload(done) {
gulpPlugins.browserSync.reload();
done();
}
/**
* ======== Nodemon ========
*/
/**
* Initializes a nodemon server
* @param {function} [done] - An automatically assigned and invoked callback to signal asynchronous completion (do not use!)
*/
server(done) {
const _this = this;
let called = false;
const server = gulpPlugins.nodemon(gulpOptions.app.nodemon);
server.on('start', () => {
if (!called) {
called = true;
done();
}
});
server.on('restart', () => {
console.log(chalk.green(dedent(`
*
* Retarting nodemon server...
*
`)));
gulp.series(_this.reload);
});
server.on('crash', () => {
console.log(chalk.green(dedent(`
*
* Nodemon server crashed! Restarting in 5 seconds...
*
`)));
server.emit('restart', 5); // restart the server in 5 seconds
});
}
/**
* ======== Misc ========
*/
/**
* cleans the dist directory
*/
clean(done) {
del.sync('dist');
done();
}
}
/**
* Let's get the party started!
* Don't forget to have fun on this new project! (✿ ◕ ‿ ◕)ᓄ ╰U╯
*/
gulp.registry(new Flow());