-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
184 lines (162 loc) · 5.02 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
var fs = require('fs');
var gulp = require('gulp');
var util = require('gulp-util');
var size = require('gulp-size');
var sass = require('gulp-sass');
var rename = require('gulp-rename');
var uglify = require('gulp-uglify');
var eslint = require('gulp-eslint');
var connect = require('gulp-connect');
var liveReload = require('gulp-livereload');
var scsslint = require('gulp-scss-lint');
var rimraf = require('rimraf');
var autoprefixer = require('gulp-autoprefixer');
var generateWebpackConfig = require('./generateWebpackConfig.js');
var webpackStream = require('webpack-stream');
// setup
var environment = util.env.type || 'development';
var srcDir = './src/';
var buildDir = './build/';
var imagesDir = 'images/';
var mediaDir = 'media/';
var soundsDir = 'sounds/';
var htmlEntry = 'index.html';
var htmlEntryFull = srcDir + htmlEntry;
var htmlArtifact = 'index.html';
var jsxEntry = 'entrypoint.jsx';
var jsxEntryFull = srcDir + jsxEntry;
var jsxArtifact = 'bundle.js';
var jsxWebpackConfig = generateWebpackConfig(jsxEntryFull, jsxArtifact);
var scssEntry = 'entrypoint.scss';
var scssEntryFull = srcDir + scssEntry;
var scssArtifact = 'app.css';
// connect config: the port that you'd like to run your dev server on
var servePort = 1337;
function doWebpack(config) {
return gulp.src(config.entry)
.pipe(webpackStream(config))
.pipe(environment === 'production' ? uglify() : util.noop())
.pipe(gulp.dest(buildDir))
.pipe(size({ title: 'js' }))
.pipe(connect.reload())
.pipe(liveReload());
}
function fsExistsSync(filePath) {
try {
fs.statSync(filePath);
return true;
} catch (err) {
return false;
}
}
function mkdirsIfMissing() {
for (var i = 0; i < arguments.length; i++) {
if (!fsExistsSync(arguments[i])) fs.mkdirSync(arguments[i]);
}
}
function rmIfExists() {
for (var i = 0; i < arguments.length; i++) {
if (fsExistsSync(arguments[i])) rimraf.sync(arguments[i]);
}
}
var utilTasks = {
'setup-build': function () {
mkdirsIfMissing(
buildDir,
buildDir + imagesDir
);
},
'clean': function () {
rmIfExists(buildDir);
},
'lint-js': function () {
return gulp.src(['src/**/*.{js,jsx}', '!node_modules/**', 'test/**/*'])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError())
;
},
'lint-scss': function() {
return gulp.src(['src/**/*.scss'])
.pipe(scsslint())
;
},
};
var buildTasks = {
'build-html': function () {
return gulp.src(htmlEntryFull)
.pipe(gulp.dest(buildDir));
},
'build-js': function () {
return doWebpack(jsxWebpackConfig.getConfig(environment));
},
'build-scss': function () {
return gulp.src(scssEntryFull)
.pipe(sass().on('error', sass.logError))
.pipe(rename(scssArtifact))
.pipe(autoprefixer())
.pipe(gulp.dest(buildDir))
.pipe(size({title: 'css'}));
},
'build-images': function () {
return gulp.src([srcDir + imagesDir + '/**'])
.pipe(gulp.dest(buildDir + imagesDir));
},
'build-sounds': function () {
return gulp.src([srcDir + mediaDir + soundsDir + '/**'])
.pipe(gulp.dest(buildDir + soundsDir));
},
};
var watchTasks = {
'watch-html': function () {
return gulp.watch([srcDir + '**/*.html'], ['build-html']);
},
'watch-js': function () {
var config = jsxWebpackConfig.getConfig(environment);
config.watch = true;
return doWebpack(config);
},
'watch-scss': function () {
return gulp.watch([srcDir + '**/*.scss'], ['build-scss']);
},
'watch-images': function () {
return gulp.watch([srcDir + imagesDir + '/*'], ['build-images']);
},
'watch-sounds': function () {
return gulp.watch([srcDir + mediaDir + soundsDir + '/*'], ['build-sounds']);
},
};
var serveTasks = {
'serve': function () {
connect.server({
root: buildDir,
port: servePort,
livereload: {
port: 35729,
},
});
},
};
gulp.task('setup-build', utilTasks['setup-build']);
// Build
gulp.task('build-js', buildTasks['build-js']);
gulp.task('build-html', buildTasks['build-html']);
gulp.task('build-scss', buildTasks['build-scss']);
gulp.task('build-images', buildTasks['build-images']);
gulp.task('build-sounds', buildTasks['build-sounds']);
// Watch
gulp.task('watch-js', ['build-js'], watchTasks['watch-js']);
gulp.task('watch-scss', ['build-scss'], watchTasks['watch-scss']);
gulp.task('watch-html', ['build-html'], watchTasks['watch-html']);
gulp.task('watch-images', ['build-images'], watchTasks['watch-images']);
gulp.task('watch-sounds', ['build-sounds'], watchTasks['watch-sounds']);
// Serve
gulp.task('serve', serveTasks['serve']);
// Lint
gulp.task('lint-js', utilTasks['lint-js']);
gulp.task('lint-scss', utilTasks['lint-scss']);
// USE THESE!!
gulp.task('clean', utilTasks.clean);
gulp.task('lint', ['lint-js', 'lint-scss']);
gulp.task('build', ['setup-build', 'build-js', 'build-html', 'build-scss', 'build-images', 'build-sounds']);
gulp.task('watch', ['setup-build', 'serve', 'watch-js', 'watch-scss', 'watch-html', 'watch-images', 'watch-sounds']);