This repository has been archived by the owner on Nov 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Gulpfile.js
196 lines (166 loc) · 5.99 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
(function(require){
'use strict';
var project = require('./package.json'),
path = require('path'),
outputPath = path.join(__dirname, 'dist', project.name),
distWebPath = path.join(outputPath, 'web', project.name),
production = process.env.NODE_ENV === 'production';
// Load plugins
var gulp = require('gulp'),
gutil = require('gulp-util'),
buffer = require('gulp-buffer'),
transform = require('vinyl-transform'),
source = require('vinyl-source-stream'),
fs = require('fs'),
stream = require('stream'),
mustache = require('mustache'),
gulpstache = require("gulp-mustache"),
less = require('gulp-less'),
rename = require('gulp-rename'),
concat = require('gulp-concat'),
del = require('del'),
eslint = require('gulp-eslint'),
karma = require('karma').server,
notifier = require('node-notifier'),
browserify = require('browserify'),
babelify = require('babelify'),
debowerify = require('debowerify'),
sourcemaps = require('gulp-sourcemaps');
var runSequence = require('run-sequence');
var replace = require('gulp-replace');
// Optimisers
var useref = require('gulp-useref');
var gulpif = require('gulp-if');
var uglify = require('gulp-uglify');
var minifyHtml = require('gulp-minify-html');
var minifyCss = require('gulp-minify-css');
var rev = require('gulp-rev');
var revReplace = require('gulp-rev-replace');
var browserified = transform(function(filename) {
var b = browserify({entries: filename, debug: true});
return b.bundle();
});
function mustachifyWebParts(webParts, templatePath){
var webParts = project.webParts || [],
template = fs.readFileSync(templatePath, {encoding: 'utf8'}),
src = stream.Readable({ objectMode: true });
src._read = function () {
mustache.parse(template); // speeds up mustache templating
webParts.forEach(function(webPart) {
var templatedOutput = mustache.render(template, {htmlFilePrefix: webPart.htmlFilePrefix});
this.push(new gutil.File({ cwd: __dirname, base: path.join(__dirname, './webParts/'), path: path.join(__dirname, './webParts/', webPart.name + '.webpart.xml'), contents: new Buffer(templatedOutput) }))
this.push(null)
}, this)
}
return src
};
// Module definition XML for LabKey
gulp.task('labkey:module', function() {
var semver = project.version.split(".");
return gulp.src("./templates/module.xml.mustache")
.pipe(gulpstache({
name: project.name,
// LabKey needs double values for the version, so can't cope with SemVer.
// We'll lump together minor and patch versions so 1.2.3 => 1.23
version: semver[0] + '.' + semver.slice(1).join('')
}))
.pipe(rename('module.xml'))
.pipe(gulp.dest(path.join(outputPath, 'config')));
});
// Generates the XML definitions for each WebPart
gulp.task('labkey:webParts', function() {
return mustachifyWebParts(project.webParts, "./templates/webpart.xml.mustache")
.pipe(gulp.dest(path.join(outputPath, 'views')));
});
gulp.task('views', ['labkey:webParts'], function() {
return gulp.src("./views/**/*.html")
.pipe(replace('/labking/', '<%=contextPath%>/labking/'))
.pipe(gulp.dest(path.join(outputPath, 'views')));
});
// Styles
gulp.task('styles', function() {
return gulp.src('./less/**/*.less')
.pipe(less())
.pipe(gulp.dest(path.join(distWebPath, 'styles')));
});
// Fonts
gulp.task('fonts', function() {
return gulp.src('./bower_components/fontawesome/fonts/*')
.pipe(gulp.dest(path.join(distWebPath, 'fonts')));
});
gulp.task('scripts:test', function (done) {
karma.start({
configFile: __dirname + '/karma.conf.js',
singleRun: true,
// 'osx' reporter hangs this test
reporters: ['progress']
}, done);
});
// Validate JavaScript
gulp.task('scripts:validate', function() {
return gulp.src(['./js/**/*(!spec).js'])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failOnError());
});
// Build JavaScript with Browserify to index.js
gulp.task('scripts:compile', function() {
return browserify('./js/labking.module.js', {
standalone: 'noscope',
debug: true
})
.bundle()
.pipe(source('application.js'))
.pipe(buffer())
.pipe(gulp.dest(path.join(distWebPath, 'js')))
});
gulp.task('useref', [
'views',
'styles',
'scripts:compile'
], function() {
var userefAssets = useref.assets({
searchPath: 'dist/labking/web'
});
var pipeline = gulp.src("views/**/*.html")
.pipe(userefAssets) // Concatenate with gulp-useref
.pipe(gulpif('*.js', uglify({mangle: true})))
.pipe(gulpif('*.css', minifyCss()))
.pipe(rev()) // Rename the concatenated files
.pipe(userefAssets.restore())
.pipe(useref())
.pipe(revReplace()) // Substitute in new filenames
.pipe(replace('../web/', '<%=contextPath%>/'))
.pipe(gulp.dest(path.join(outputPath, 'views')));
});
// Clean
gulp.task('clean', function(cb) {
del(['dist/**'], cb);
});
gulp.task('build:develop', function() {
gulp.start(
'fonts',
'labkey:module',
'styles',
'scripts:compile',
'scripts:validate',
'views'
);
});
gulp.task('build:optimised', function() {
gulp.start(
'fonts',
'labkey:module',
'scripts:validate',
'useref'
);
});
// Default task
gulp.task('default', function (cb) {
runSequence(
'scripts:test',
'build:optimised',
cb
);
});
}(require));