-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
217 lines (197 loc) · 5.45 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* Gulp modules */
const gulp = require('gulp');
const autoPrefixer = require('gulp-autoprefixer');
const compileJSX = require('gulp-compile-jsx');
const clean = require('gulp-clean');
const concat = require('gulp-concat');
const cssMin = require('gulp-cssmin');
const gulpIf = require('gulp-if');
const less = require('gulp-less');
const plumber = require('gulp-plumber');
const staticI18nHtml = require('gulp-static-i18n-html');
const srcMaps = require('gulp-sourcemaps');
const uglify = require('gulp-uglify');
const util = require('gulp-util');
const babelify = require('babelify');
const browserify = require('browserify');
const watchify = require('watchify');
const argv = require('yargs').argv;
/* Vinyl modules */
const buffer = require('vinyl-buffer');
const srcStream = require('vinyl-source-stream');
/* Create production and dependencies */
const production = process.env.NODE_ENV === 'production';
const dependencies = [
'alt',
'react',
'react-dom',
'react-router',
'underscore'
];
/* Get dest parameter */
//const dest = argv.p[argv.p.length - 1] == '/' ? argv.p : argv.p + '/';
var dest;
if (argv.p === undefined) {
dest = './test/dest/'
} else {
dest = argv.p + '/';
}
/* Combine all JavaScript libraries into a single file for fewer HTTP requests */
gulp.task('vendor', function() {
return gulp
.src([
'./bower_components/jquery/dist/jquery.js',
'./bower_components/magnific-popup/dist/jquery.magnific-popup.js',
'./bower_components/semantic/dist/semantic.js'
])
.pipe(concat('vendor.js'))
.pipe(gulpIf(production, uglify({
mangle: false
})))
.pipe(gulp.dest(dest + 'js/'));
});
/* Compile third-party dependencies separately for faster performance */
gulp.task('browserify-vendor', function() {
return browserify()
.require(dependencies)
.bundle()
.pipe(srcStream('vendor.bundle.js'))
.pipe(buffer())
.pipe(gulpIf(production, uglify({
mangle: false
})))
.pipe(gulp.dest(dest + 'js/'));
});
/* Compile only project files, excluding all third-party dependencies */
gulp.task('browserify', ['browserify-vendor'], function() {
return browserify({
entries: './lib/template/main.jsx',
extensions: [
'.js',
'.json',
'.es6',
'.jsx'
],
debug: false
})
.external(dependencies)
.transform(babelify, {
presets: [
'es2015',
'react'
]
})
.bundle()
.pipe(srcStream('bundle.js'))
.pipe(buffer())
.pipe(srcMaps.init({
loadMaps: true
}))
.pipe(gulpIf(production, uglify({
mangle: false
})))
.pipe(srcMaps.write('.'))
.pipe(gulp.dest(dest + 'js/'));
});
/* Same as browserify task, but will also watch for changes and re-compile */
gulp.task('browserify-watch', ['browserify-vendor'], function() {
var bundler = watchify(browserify({
entries: './lib/template/main.jsx',
debug: true
},
watchify.args
));
bundler.external(dependencies);
bundler.transform(babelify, {
presets: [
'es2015',
'react'
]
});
bundler.on('update', rebundle);
return rebundle();
function rebundle() {
var start = Date.now();
return bundler
.bundle()
.on('error', function(err) {
util.log(util.colors.red(err.toString()));
})
.on('end', function() {
util.log(util.colors.green('Finished rebundling in', (Date.now() - start) + 'ms.'));
})
.pipe(srcStream('bundle.js'))
.pipe(buffer())
.pipe(srcMaps.init({
loadMaps: true
}))
.pipe(srcMaps.write('.'))
.pipe(gulp.dest(dest + 'js/'));
}
});
/* Compile less stylesheets. */
gulp.task('styles', function() {
return gulp
.src('./lib/template/less/main.less')
.pipe(plumber())
.pipe(less())
.pipe(autoPrefixer())
.pipe(gulpIf(production, cssMin()))
.pipe(gulp.dest(dest + 'css/'));
});
/* Watch task */
gulp.task('watch', function() {
return gulp.watch('./lib/template/less/**/*.less', ['styles']);
});
/* Duplicate themes */
gulp.task('themes', function() {
return gulp
.src('./lib/template/themes/*/*/*/*')
.pipe(gulp.dest(dest + 'js/themes/'))
.pipe(gulp.dest(dest + 'css/themes/'));
});
/* Initialize HTML */
gulp.task('setup', function() {
return gulp
.src('./lib/template/index.html')
.pipe(gulp.dest(dest));
})
/* Publish HTML file */
gulp.task('i18n', function() {
return gulp
.src(dest + 'index.html')
.pipe(staticI18nHtml({
locale: ''
}))
.pipe(gulp.dest(dest));
});
/* Clean the unuse folder in destination folder */
gulp.task('clean', function() {
return gulp
.src(dest + 'en', {
read: false
})
.pipe(clean());
})
/* Default task */
gulp.task('default', [
'styles',
'vendor',
'browserify-watch',
'watch',
'i18n',
'clean'
]);
/* Build task */
gulp.task('build', [
'styles',
'vendor',
'browserify',
'i18n',
'clean'
]);
/* Initialize task */
gulp.task('initial', [
'themes',
'setup'
])