-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
179 lines (160 loc) · 5.26 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
'use strict';
var gulp = require('gulp'),
concat = require('gulp-concat'),
uglify = require('gulp-uglify'),
connect = require('gulp-connect'),
rename = require('gulp-rename'),
sass = require('gulp-sass'),
prefix = require('gulp-autoprefixer'),
runSequence = require('run-sequence'),
maps = require('gulp-sourcemaps'),
cleanCSS = require('gulp-clean-css'),
clean = require('gulp-clean');
var scssFiles = './src/**/*.scss';
var htmlFile = './src/index.html';
var manualAddedFonts = './src/fonts/**/*.*';
var fonts = [
'./node_modules/font-awesome/fonts/**/*.*',
manualAddedFonts
];
// Add your external libs css here
var libCssFiles = [
'./node_modules/normalize.css/*.css',
'./node_modules/foundation-sites/dist/foundation-flex.min.css',
'./node_modules/font-awesome/css/font-awesome.min.css'
];
// Add your external libs js here
var libJsFiles = [
];
var finalVersionFolder = './public/';
var finalScriptFolder = finalVersionFolder + 'js/';
var finalStyleFolder = finalVersionFolder + 'css/';
var finalFontFolder = finalVersionFolder + 'fonts/';
// Styles Gulp tasks
// ----------------------------------------------------------------------------
gulp.task('libcss', function () {
return gulp.src(libCssFiles)
.pipe(concat('libs.css'))
.pipe(cleanCSS({compatibility: 'ie10'}))
.pipe(gulp.dest(finalStyleFolder))
.pipe(connect.reload());
});
gulp.task('libjs', function () {
return gulp.src(libJsFiles)
.pipe(concat('libs.js'))
.pipe(gulp.dest(finalScriptFolder))
.pipe(connect.reload());
});
gulp.task('sass', function() {
return gulp.src(scssFiles)
.pipe(maps.init())
.pipe(sass().on('error', sass.logError))
.pipe(prefix({
browsers: ['last 2 versions'],
cascade: false
}))
.pipe(concat('site.css'))
.pipe(maps.write('./'))
.pipe(gulp.dest(finalStyleFolder))
.pipe(connect.reload());
});
gulp.task('html', function () {
gulp.src(htmlFile)
.pipe(gulp.dest(finalVersionFolder))
.pipe(connect.reload());
});
gulp.task('fonts', function() {
gulp.src(fonts)
.pipe(gulp.dest(finalFontFolder))
.pipe(connect.reload());
});
// React Gulp tasks
// ----------------------------------------------------------------------------
var browserify = require('browserify'),
babelify = require('babelify'),
gutil = require('gulp-util'),
source = require('vinyl-source-stream');
var reactFiles = './src/**/*.js';
var reactAppFile = './src/app.js';
var reactCoreFinalFile = 'mainReactCore.js';
var reactFinalAppFile = 'mainJs.js';
// External dependencies you do not want to rebundle while developing,
// but include in your application deployment
var dependencies = [
'react',
'react-dom'
];
// keep a count of the times a task refires
var scriptsCount = 0;
gulp.task('react', function () {
return bundleApp(false).pipe(connect.reload());
});
gulp.task('deploy', function (){
bundleApp(true);
});
// Private Functions
// ----------------------------------------------------------------------------
function bundleApp(isProduction) {
scriptsCount++;
console.log(scriptsCount);
// Browserify will bundle all our js files together in to one and will let
// us use modules in the front end.
var appBundler = browserify({
entries: reactAppFile,
debug: true
})
// If it's not for production, a separate vendors.js file will be created
// the first time gulp is run so that we don't have to rebundle things like
// react everytime there's a change in the js file
if (!isProduction && scriptsCount === 1){
// create vendors.js for dev environment.
browserify({
require: dependencies,
debug: true
})
.bundle()
.on('error', gutil.log)
.pipe(source(reactCoreFinalFile))
.pipe(gulp.dest(finalScriptFolder));
}
if (!isProduction){
// make the dependencies external so they dont get bundled by the
// app bundler. Dependencies are already bundled in vendor.js for
// development environments.
dependencies.forEach(function(dep){
appBundler.external(dep);
})
}
return appBundler
// transform ES6 and JSX to ES5 with babelify
.transform("babelify", {presets: ["es2015", "react"]})
.bundle()
.on('error',gutil.log)
.pipe(source(reactFinalAppFile))
.pipe(gulp.dest(finalScriptFolder));
}
// Common Gulp tasks
// ----------------------------------------------------------------------------
gulp.task('watch', function() {
gulp.watch(scssFiles,['sass']);
gulp.watch(htmlFile,['html']);
gulp.watch(libCssFiles,['libcss']);
gulp.watch(libJsFiles,['libjs']);
gulp.watch(reactFiles,['react']);
});
gulp.task('connect', function () {
connect.server({
root: finalVersionFolder,
livereload: true,
port: 4000
})
});
gulp.task('clean', function (){
return gulp.src(finalVersionFolder, {read: false})
.pipe(clean());
});
gulp.task('default', function(callback) {
runSequence('clean', 'html', 'sass', 'react', 'libcss', 'libjs', 'fonts', 'watch', 'connect',
callback
);
});