-
Notifications
You must be signed in to change notification settings - Fork 62
/
Gulpfile.js
284 lines (260 loc) · 8.85 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
"use strict";
var gulp = require('gulp');
var del = require('del');
var jshint = require('gulp-jshint');
var less = require('gulp-less');
var path = require('path');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var gulpIf = require('gulp-if');
var gutil = require('gulp-util');
var webpack = require('webpack-stream');
var sourcemaps = require('gulp-sourcemaps');
var header = require('gulp-header');
var rename = require('gulp-rename');
var ghPages = require('gulp-gh-pages');
var tap = require('gulp-tap');
var metalsmith = require('metalsmith');
var msMarkdown = require('metalsmith-markdown');
var msReplace = require('metalsmith-text-replace');
var msRegisterHelpers = require('metalsmith-register-helpers');
var msLayouts = require('metalsmith-layouts');
var msCollections = require('metalsmith-collections');
var msCollectionMetadata = require('metalsmith-collection-metadata');
var msNavigation = require('metalsmith-navigation');
var msWatch = require('metalsmith-watch');
var msIgnore = require('metalsmith-ignore');
var msAssets = require('metalsmith-assets');
var zip = require('gulp-zip');
var merge = require('merge-stream');
var browserSync = require('browser-sync');
var
packageFile = 'package.json',
pkg = require('./' + packageFile),
paths = {
scripts: ['src/**/*.js', '!src/jquery.SPServices Intellisense.js'],
less: ['src/less/**/*.less'],
docs: ['docs/**/*.md'],
dist: ['dist/**/*']
},
// buildDate = gulp.template.today('yyyy-mm-dd'),
// buildYear = gulp.template.today('yyyy'),
// buildId = (new Date()).getTime(),
banner = "/*\n" +
"* <%= pkg.name %> - <%= pkg.description_short %>\n" +
"* Version <%= pkg.version %>\n" +
"* @requires <%= pkg.requires %>\n" +
"*\n" +
"* Copyright (c) <%= pkg.copyright %>\n" +
"* Examples and docs at:\n" +
"* <%= pkg.homepage %>\n" +
"* Licensed under the MIT license:\n" +
"* http://www.opensource.org/licenses/mit-license.php\n" +
"*/\n" +
"/*\n" +
"* @description <%= pkg.description_long %>\n" +
"* @type jQuery\n" +
"* @name <%= pkg.name %>\n" +
"* @category Plugins/<%= pkg.name %>\n" +
"* @author <%= pkg.authors %>\n" +
// "* @build <%= pkg.name %> <%= pkg.version %> <%= grunt.template.today('yyyy-mm-dd hh:MM:ss') %>\n" +
"*/\n";
gulp.task('config', function() {
fs = require("fs");
pkg = fs.readFileSync(packageFile, "utf8");
gutil.log(pkg.toString());
});
gulp.task('clean:build', function() {
// You can use multiple globbing patterns as you would with `gulp.src`
return del(['build']);
});
gulp.task('clean:docs', function() {
return del(['dist/docs/**/*']);
});
// Convert .less files to .css
gulp.task('less', function () {
return gulp.src(paths.less)
.pipe(less({
paths: [ path.join(__dirname, 'less', 'includes') ]
}))
.pipe(gulp.dest('src/css'));
});
// Lint the files to catch any issues
gulp.task('lint', function() {
return gulp.src(paths.scripts)
.pipe(jshint())
.pipe(jshint.reporter('default'));
});
// Gulp watch syntax
gulp.task('watch', ['less'], function(){
gulp.watch(paths.less, ['less']);
// Other watchers
});
gulp.task('scripts', function() {
// Minify and copy all JavaScript (except vendor scripts)
// with sourcemaps all the way down
return gulp.src(paths.scripts)
.pipe(sourcemaps.init())
// .pipe(uglify())
.pipe(header(banner, { pkg : pkg } ))
.pipe(concat('jQuery.SPServices-' + pkg.version + '.js'))
.pipe(sourcemaps.write())
.pipe(gulp.dest('build/'));
});
/*
gulp.task('fixdocs', function () {
return gulp.src(paths.docs) //paths.docs
.pipe(tap(function(file) {
gutil.log(file.path);
var tmp = file.path.split('\\');
var cat = tmp.length > 4 ? tmp[4] : '';
var name = tmp[tmp.length - 1].split('.')[0];
file.contents = Buffer.concat([
new Buffer(['---',
'label: ' + name,
'id: ' + name,
'categorySlug: \'' + cat + '\'',
'categoryLabel: \'' + cat + '\'',
'categorySort: \'alphabetical\'', // 'alphabetical' || 'rank'
'documentSort: \'alphabetical\'', // 'alphabetical' || 'rank'
'', ''].join('\n')),
file.contents
])
}))
.pipe(gulp.dest('dist/tmp'));
});
*/
gulp.task('docs', ['clean:docs'], function () {
return metalsmith(__dirname)
.metadata({
site: {
title: pkg.name,
description: pkg.description_long
},
version: pkg.version,
copyright: pkg.copyright,
repository: pkg.repository.url,
license: pkg.licenses[0]
})
.source('./docs')
.clean(false) // Don't delete files while Gulp tasks are running
.destination('./dist/docs')
.use(msWatch({
paths: {
'${source}/**/*': true, // Rebuild an individual file when it is changed
"docs/**/*.md": "**/*.md", // Rebuild all .md files when a .md file is changed
"docs/layouts/**/*.*": "**/*.md" // Rebuild all .md files when a template file is changed
}
}))
.use(msIgnore('layouts/**/*')) // Don't output template files in dist/docs
.use(msMarkdown())
.use(msReplace({
'**/*.html': [
{
find: /.md"/gi,
replace: '.html"'
},
{
find: /.md#/gi,
replace: '.html#'
},
{
find: /<table>/gi,
replace: '<table class="table">' // Bootstrap table class
},
{
find: /<code class="lang-/gi,
replace: '<code class="language-' // Prism.js classes are prefixed with language- instead of -lang
}
]
}))
.use(msCollections({
'All': {
pattern: '**/*' // Used by msCollectionMetadata
}
}))
.use(msCollectionMetadata({
'collections.All': {
nav_group_global: 'global' // Add all pages to 'global' nav; this ensure that every page has a 'nav_path' property
}
}))
.use(msNavigation({
global: {
filterProperty: 'nav_group_global',
sortBy: 'nav_sort',
breadcrumbProperty: 'breadcrumb_path'
},
primary: {
filterProperty: 'nav_group',
sortBy: 'nav_sort'
},
featured: {
filterProperty: 'nav_group',
sortBy: 'nav_sort'
}
}))
.use(msRegisterHelpers({
directory: 'docs/layouts/helpers'
}))
.use(msLayouts({
engine: 'handlebars',
directory: 'docs/layouts',
partials: 'docs/layouts/partials',
default: 'main.hbs'
}))
.use(msAssets({
'source': './docs/assets',
'destination': 'assets'
}))
.build(function (err) {
if (err) {
throw err;
}
});
});
// Build module
gulp.task('build', function() {
return gulp.src(paths.scripts)
.pipe(webpack(require('./webpack.config.js'), null, function(err, stats) {
// Output stats so we can tell what happened
gutil.log(stats.toString());
}))
.pipe(rename('jQuery.SPServices-' + pkg.version + '.js'))
.pipe(gulp.dest('build/')) // SPServices.js
.pipe(gulpIf('*.js', uglify())) // Minify all modules
.pipe(rename('jQuery.SPServices-' + pkg.version + '.min.js'))
.pipe(gulp.dest('build/')); // SPServices.min.js
});
// Run local server for viewing docs
gulp.task('servedocs', ['docs'], function() {
// Create browser-sync instance
var bs = browserSync.create();
// Start browser-sync server for HTML docs
bs.init({
files: 'dist/**/*',
server: {
baseDir: 'dist/docs'
},
reloadDelay: 300, // Allow all dist/docs HTML files to be updated before reloading
reloadDebounce: 1000 // Only reload once per second (avoids multiple reloads when many docs are updated simultaneously e.g. when a template file is updated)
});
});
// Deploy to gh-pages
gulp.task('deploydocs', function() {
return merge(
gulp.src(paths.dist).pipe(zip('SPServices.zip')),
gulp.src('dist/docs/**/*')
).pipe(ghPages());
});
// Default task(s).
gulp.task('default', [
'clean:build',
'lint',
'less',
'scripts',
'build'
// 'concat',
// 'copy:processBuildVariables',
// 'uglify',
// 'zip'
]);