forked from vatesfr/xen-orchestra
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
305 lines (257 loc) · 6.73 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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
'use strict'
// ===================================================================
var SRC_DIR = __dirname + '/src' // eslint-disable-line no-path-concat
var DIST_DIR = __dirname + '/dist' // eslint-disable-line no-path-concat
// Port to use for the livereload server.
//
// It must be available and if possible unique to not conflict with other projects.
// http://www.random.org/integers/?num=1&min=1024&max=65535&col=1&base=10&format=plain&rnd=new
var LIVERELOAD_PORT = 26242
var PRODUCTION = process.env.NODE_ENV === 'production'
var DEVELOPMENT = !PRODUCTION
if (!process.env.XOA_PLAN) {
process.env.XOA_PLAN = '5' // Open Source
}
// ===================================================================
var gulp = require('gulp')
// ===================================================================
function lazyFn (factory) {
var fn = function () {
fn = factory()
return fn.apply(this, arguments)
}
return function () {
return fn.apply(this, arguments)
}
}
// -------------------------------------------------------------------
var livereload = lazyFn(function () {
var livereload = require('gulp-refresh')
livereload.listen({
port: LIVERELOAD_PORT
})
return livereload
})
var pipe = lazyFn(function () {
var current
function pipeCore (streams) {
var i, n, stream
for (i = 0, n = streams.length; i < n; ++i) {
stream = streams[i]
if (!stream) {
// Nothing to do
} else if (stream instanceof Array) {
pipeCore(stream)
} else {
current = current
? current.pipe(stream)
: stream
}
}
}
var push = Array.prototype.push
return function (streams) {
try {
if (!(streams instanceof Array)) {
streams = []
push.apply(streams, arguments)
}
pipeCore(streams)
return current
} finally {
current = null
}
}
})
var resolvePath = lazyFn(function () {
return require('path').resolve
})
// -------------------------------------------------------------------
// Similar to `gulp.src()` but the pattern is relative to `SRC_DIR`
// and files are automatically watched when not in production mode.
var src = lazyFn(function () {
function resolve (path) {
return path
? resolvePath(SRC_DIR, path)
: SRC_DIR
}
return PRODUCTION
? function src (pattern, opts) {
var base = resolve(opts && opts.base)
return gulp.src(pattern, {
base: base,
cwd: base,
passthrough: opts && opts.passthrough,
sourcemaps: opts && opts.sourcemaps
})
}
: function src (pattern, opts) {
var base = resolve(opts && opts.base)
return pipe(
gulp.src(pattern, {
base: base,
cwd: base,
passthrough: opts && opts.passthrough,
sourcemaps: opts && opts.sourcemaps
}),
require('gulp-watch')(pattern, {
base: base,
cwd: base
}),
require('gulp-plumber')()
)
}
})
// Similar to `gulp.dest()` but the output directory is relative to
// `DIST_DIR` and default to `./`, and files are automatically live-
// reloaded when not in production mode.
var dest = lazyFn(function () {
function resolve (path) {
return path
? resolvePath(DIST_DIR, path)
: DIST_DIR
}
var opts = {
sourcemaps: {
path: '.'
}
}
return PRODUCTION
? function dest (path) {
return gulp.dest(resolve(path), opts)
}
: function dest (path) {
var stream = gulp.dest(resolve(path), opts)
stream.pipe(livereload())
return stream
}
})
// ===================================================================
function browserify (path, opts) {
if (opts == null) {
opts = {}
}
var bundler = require('browserify')(path, {
basedir: SRC_DIR,
debug: true,
extensions: opts.extensions,
fullPaths: false,
paths: SRC_DIR + '/common',
standalone: opts.standalone,
// Required by Watchify.
cache: {},
packageCache: {}
})
var plugins = opts.plugins
for (var i = 0, n = plugins && plugins.length; i < n; ++i) {
var plugin = plugins[i]
bundler.plugin(require(plugin[0]), plugin[1])
}
if (PRODUCTION) {
// FIXME: does not work with react-intl (?!)
// bundler.plugin('bundle-collapser/plugin')
} else {
bundler = require('watchify')(bundler)
}
// Append the extension if necessary.
if (!/\.js$/.test(path)) {
path += '.js'
}
path = resolvePath(SRC_DIR, path)
var stream = new (require('readable-stream'))({
objectMode: true
})
var write
function bundle () {
bundler.bundle(function onBundle (error, buffer) {
if (error) {
stream.emit('error', error)
return
}
write(new (require('vinyl'))({
base: SRC_DIR,
contents: buffer,
path: path
}))
})
}
if (PRODUCTION) {
write = function (data) {
stream.push(data)
stream.push(null)
}
} else {
stream = require('gulp-plumber')().pipe(stream)
write = function (data) {
stream.push(data)
}
bundler.on('update', bundle)
}
stream._read = function () {
this._read = function () {}
bundle()
}
return stream
}
// ===================================================================
gulp.task(function buildPages () {
return pipe(
src('index.pug', { sourcemaps: true }),
require('gulp-pug')(),
DEVELOPMENT && require('gulp-embedlr')({
port: LIVERELOAD_PORT
}),
dest()
)
})
gulp.task(function buildScripts () {
return pipe(
browserify('./index.js', {
plugins: [
// ['css-modulesify', {
['modular-css/browserify', {
css: DIST_DIR + '/modules.css'
}]
]
}),
require('gulp-sourcemaps').init({ loadMaps: true }),
PRODUCTION && require('gulp-uglify')(),
dest()
)
})
gulp.task(function buildStyles () {
return pipe(
src('index.scss', { sourcemaps: true }),
require('gulp-sass')(),
require('gulp-autoprefixer')([
'last 1 version',
'> 1%'
]),
PRODUCTION && require('gulp-csso')(),
dest()
)
})
gulp.task(function copyAssets () {
return pipe(
src(['assets/**/*', 'favicon.*']),
src('fontawesome-webfont.*', {
base: __dirname + '/node_modules/font-awesome/fonts', // eslint-disable-line no-path-concat
passthrough: true
}),
src(['!*.css', 'font-mfizz.*'], {
base: __dirname + '/node_modules/font-mfizz/dist', // eslint-disable-line no-path-concat
passthrough: true
}),
dest()
)
})
gulp.task('build', gulp.parallel(
'buildPages',
'buildScripts',
'buildStyles',
'copyAssets'
))
// -------------------------------------------------------------------
gulp.task(function clean (done) {
require('rimraf')(DIST_DIR, done)
})