forked from bengourley/anytime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
168 lines (142 loc) · 4.56 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
'use strict'
////////////////////////////////////////////////////////////////////////////////
// Dependencies //
////////////////////////////////////////////////////////////////////////////////
var autoprefixer = require('gulp-autoprefixer')
, del = require('del')
, ghPages = require('gulp-gh-pages')
, gulp = require('gulp')
, header = require('gulp-header')
, htmlmin = require('gulp-htmlmin')
, http = require('http')
, jade = require('gulp-jade')
, nano = require('gulp-cssnano')
, sass = require('gulp-sass')
, st = require('st')
, webpack = require('webpack')
////////////////////////////////////////////////////////////////////////////////
// Config //
////////////////////////////////////////////////////////////////////////////////
var paths =
{ webSrcHtml: './website/index.jade'
, webSrcJs: './website/js/index.js'
, webSrcScss: './website/styles/index.scss'
, webDest: './website/build/'
, libSrcJs: './src/anytime.js'
, libDest: './dist/'
}
var webpackPlugins =
[ new webpack.optimize.DedupePlugin()
, new webpack.optimize.OccurenceOrderPlugin()
, new webpack.optimize.UglifyJsPlugin(
{ compress: { warnings: false }
, output: { comments: false }
, sourceMap: false
}
)
]
////////////////////////////////////////////////////////////////////////////////
// Gulp tasks for the website //
////////////////////////////////////////////////////////////////////////////////
// Delete generated website files
gulp.task('clean:web', function() {
del.sync(paths.webDest, { force: true })
})
// Process main HTML file
gulp.task('html', function() {
return gulp.src(paths.webSrcHtml)
.pipe(jade())
.pipe(htmlmin({ removeComments: true, collapseWhitespace: true }))
.pipe(gulp.dest(paths.webDest))
})
// Process SCSS files
gulp.task('styles', function() {
return gulp.src(paths.webSrcScss)
.pipe(sass().on('error', sass.logError))
.pipe(autoprefixer({ browsers: ['last 2 versions'] }))
.pipe(nano())
.pipe(gulp.dest(paths.webDest))
})
// Process JS scripts
gulp.task('scripts', function(cb) {
webpack(
{ entry: paths.webSrcJs
, output:
{ path: paths.webDest
, filename: 'index.js'
}
, plugins: webpackPlugins
}
, function(webpack_err, stats) {
if (webpack_err) cb(webpack_err)
cb()
}
)
})
// Compile library website
gulp.task('build:web', ['clean:web', 'html', 'styles', 'scripts'])
// Launch static server for website (localhost:8080) and watch for changes
gulp.task('watch:web', ['build:web'], function() {
gulp.watch(paths.webSrcHtml, ['html'])
gulp.watch(paths.webSrcScss, ['styles'])
gulp.watch(paths.webSrcJs, ['scripts'])
http.createServer(
st(
{ path: paths.webDest
, index: 'index.html'
}
)
).listen(8080)
})
// Deploy to GitHub Pages
gulp.task('deploy', ['build:web'], function() {
return gulp.src(paths.webDest + '/**/*')
.pipe(ghPages())
})
////////////////////////////////////////////////////////////////////////////////
// Gulp tasks for the library //
////////////////////////////////////////////////////////////////////////////////
// Delete generated Anytime files
gulp.task('clean:lib', function() {
del.sync(paths.libDest, { force: true })
})
var buildLib = function(filename, externals, cb) {
var pkg = require('./package.json')
var banner =
[ '/**'
, ' * <%= pkg.name %> - <%= pkg.description %>'
, ' * @version <%= pkg.version %>'
, ' * @link <%= pkg.homepage %>'
, ' * @license <%= pkg.license %>'
, ' */'
, ''
].join('\n')
// Bundle with Webpack
webpack(
{ entry: paths.libSrcJs
, output:
{ path: paths.libDest
, filename: filename
, library: 'anytime'
, libraryTarget: 'umd'
}
, plugins: webpackPlugins
, externals: externals
}
, function(webpack_err, stats) {
if (webpack_err) cb(webpack_err)
gulp.src(paths.libDest + filename)
.pipe(header(banner, { pkg: pkg } ))
.pipe(gulp.dest(paths.libDest))
.on('end', cb)
}
)
}
gulp.task('build:lib-with-moment', function(cb) {
buildLib('anytime-with-moment.js', {}, cb);
})
gulp.task('build:lib-without-moment', function(cb) {
buildLib('anytime.js', { 'moment': 'moment' }, cb);
})
// Compile Anytime library
gulp.task('build:lib', ['clean:lib', 'build:lib-with-moment', 'build:lib-without-moment'])