-
Notifications
You must be signed in to change notification settings - Fork 3
/
gulpfile.js
151 lines (126 loc) · 4.13 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
require('browserify-handlebars');
var _ = require('underscore');
var backendDependencies = ['express', 'node-ssi'];
var baseDir = './os-import';
var browserify = require('browserify');
var buffer = require('vinyl-buffer');
var depcheck = require('depcheck');
var distDir = baseDir + '/dist';
var frontendDependencies = _.chain(require('./package.json').dependencies)
.omit(backendDependencies)
.keys()
.value();
var gulp = require('gulp');
var less = require('gulp-less');
var minifyCss = require('gulp-minify-css');
var path = require('path');
var prefixer = require('gulp-autoprefixer');
var rename = require('gulp-rename');
var resolve = require('resolve');
var srcDir = baseDir + '/src';
var scriptsDir = srcDir + '/scripts';
// Provide frontend app as a single bundle.
var bundler = browserify({
cache : {},
debug : true,
entries : [scriptsDir + '/app.js'],
fullPaths : true,
packageCache: {},
transform : ['browserify-handlebars']
});
var source = require('vinyl-source-stream');
var sourcemaps = require('gulp-sourcemaps');
var stylesDir = srcDir + '/styles';
var uglify = require('gulp-uglify');
var watchify = require('watchify');
// Don't include vendor dependencies in this bundle
bundler.external(frontendDependencies);
// Run and return the scripts pipeline on bundle
function scriptPipeline(bundle, outfile, options) {
console.log('Bundling: ' + outfile);
var
outBundle = bundle.pipe(source(outfile)).pipe(buffer());
if(options && options.uglify)
outBundle = outBundle.pipe(uglify());
return outBundle.pipe(gulp.dest(distDir));
}
// Provide frontend dependencies as a single bundle.
gulp.task('vendor-scripts', function() {
var
vendorBundler = browserify({});
frontendDependencies.forEach(function(id) {
// Avoid AMD version of backbone-forms
if(id === 'backbone-forms')
vendorBundler.require(resolve.sync(id + '/distribution/backbone-forms'), {
expose: id
});
else
vendorBundler.require(resolve.sync(id), {expose: id});
});
return scriptPipeline(vendorBundler.bundle(), 'vendor.min.js', {
uglify: true
});
});
gulp.task('app-scripts', function() {
return scriptPipeline(bundler.bundle(), 'app.min.js', {uglify: true});
});
gulp.task('app-scripts-watched', function() {
var
watcher = watchify(bundler);
watcher.on('update', function() {
scriptPipeline(watcher.bundle(), 'app.min.js');
});
return scriptPipeline(watcher.bundle(), 'app.min.js');
});
// List unused dependencies in stderr.
gulp.task('check-deps', function() {
depcheck(path.resolve('./'), {
withoutDev: false,
ignoreDirs: ['dist', 'node_modules']
}, function(unused) {
if(!_.isEmpty(unused.dependencies))
console.error('Unused dependencies: ', unused.dependencies.join(', '));
if(!_.isEmpty(unused.devDependencies))
console.error(
'Unused dev dependencies: ',
unused.devDependencies.join(', ')
);
if(!_.isEmpty(unused.invalidFiles))
console.error(
'JS files that couldn\'t be parsed: ',
unused.invalidFiles.join(', ')
);
});
});
gulp.task('copy-static', function() {
return gulp.src([
path.join(baseDir, 'src', 'static', '/**/*')
]).pipe(gulp.dest(distDir));
});
gulp.task('landing-scripts', function() {
return scriptPipeline(browserify({
cache : {},
entries : [scriptsDir + '/components/ui/landing.js'],
fullPaths : true,
packageCache: {}
}).bundle(), 'landing.min.js', {uglify: true});
});
// Provide frontend styles as a single bundle.
gulp.task('styles', function() {
// Style files dir structure may be comlicated, pick manually files to be compiled
gulp.src([path.join(stylesDir, 'styles.less')])
.pipe(sourcemaps.init())
.pipe(less())
.pipe(prefixer({browsers: ['last 4 versions']}))
.pipe(sourcemaps.write())
.pipe(minifyCss({compatibility: 'ie8'}))
.pipe(rename({suffix: '.min'}))
.pipe(gulp.dest(distDir));
});
gulp.task('default', [
'vendor-scripts', 'app-scripts', 'landing-scripts', 'styles', 'copy-static'
]);
gulp.task('dev', [
'vendor-scripts', 'app-scripts-watched', 'landing-scripts', 'styles',
'copy-static'
]);