-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
185 lines (163 loc) · 5.07 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
// jshint ignore: start
var gulp = require('gulp');
var watchify = require('watchify');
var browserify = require('browserify');
var babelify = require('babelify');
var envify = require('envify/custom');
var sourcemaps = require('gulp-sourcemaps');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var gutil = require('gulp-util');
var es = require('event-stream');
var fs = require('fs');
var rev = require('gulp-rev');
var revReplace = require('gulp-rev-replace');
var del = require('del');
var ghPages = require('gulp-gh-pages');
var concat = require('gulp-concat');
var sass = require('gulp-sass');
var autoprefixer = require('gulp-autoprefixer');
var connect = require('gulp-connect');
// Configuration
var sourcePath = './src/';
var entryFiles = [
'app.js'
];
var output = {
'dist': './dist',
'js': './dist/assets/js',
'css': './dist/assets/css'
};
var vendorFiles = [
'node_modules/mithril/mithril.js',
'node_modules/underscore/underscore.js'
];
var configFiles = [
'./src/index.html'
];
var absolutePath = '/';
var ES6 = {
bundle: function(file) {
var opts = {
entries: [sourcePath + file],
paths: ['./node_modules', sourcePath],
cache: {},
packageCache: {}
};
var b = browserify(opts);
b.file = file;
b.transform(babelify).transform(envify(require('./config.js')));
return b;
},
build: function(bundle) {
return bundle.bundle()
// log errors if they happen
.on('error', gutil.log.bind(gutil, 'Browserify Error'))
.pipe(source(bundle.file))
// optional, remove if you don't need to buffer file contents
.pipe(buffer())
// optional, remove if you dont want sourcemaps
.pipe(sourcemaps.init({loadMaps: true})) // loads map from browserify file
// Add transformation tasks to the pipeline here.
.pipe(sourcemaps.write('./')) // writes .map file
.pipe(gulp.dest(output.js));
},
watch: function(bundle) {
var w = watchify(bundle, {debug: true});
w.on('log', gutil.log);
w.on('time', function(time) {
gutil.log(gutil.colors.green('Browserify'), bundle.file, gutil.colors.blue('in ' + time + ' ms'));
});
w.on('update', ES6.build.bind(null, bundle));
return w;
}
};
gulp.task('vendor', function() {
return gulp.src(vendorFiles)
.pipe(sourcemaps.init())
.pipe(concat('vendor.js'))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest(output.js));
});
gulp.task('sass', function () {
return gulp.src('./src/styles/[^_]*.scss')
.pipe(sass().on('error', sass.logError))
.pipe(autoprefixer({
browsers: ['last 2 versions', 'ie >= 9'], // More info at https://github.com/ai/browserslist
cascade: false
}))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest(output.css));
});
gulp.task('config', function() {
return gulp.src(configFiles)
.pipe(gulp.dest(output.dist));
});
gulp.task('bundle', function() {
// map them to our stream function
var bundles = entryFiles.map(function(file) {
return ES6.build(ES6.bundle(file));
});
// create a merged stream
return es.merge.apply(null, bundles);
});
gulp.task('clean', function() {
return del.sync([output.dist + '/**', '!'+ output.dist]);
});
gulp.task('watch', ['vendor', 'sass', 'config'], function () {
gulp.watch('./src/styles/**/*.scss', ['sass']);
gulp.watch(configFiles, ['config']);
var streams = entryFiles.map(function(file) {
return ES6.build(ES6.watch(ES6.bundle(file)));
});
// create a merged stream
return es.merge.apply(null, streams);
});
gulp.task('serve', ['watch'], function() {
return connect.server({
root: './dist',
livereload: true,
fallback: './dist/index.html'
});
});
gulp.task('rev-build', ['clean', 'vendor', 'bundle', 'sass'], function(){
return gulp.src(['dist/**/*.css', 'dist/**/*.js'])
.pipe(rev())
.pipe(gulp.dest(output.dist))
.pipe(rev.manifest())
.pipe(gulp.dest(output.dist));
});
gulp.task('rev-rename', ['rev-build'], function() {
var manifestFile = output.dist + '/rev-manifest.json';
var manifest = gulp.src(manifestFile);
return gulp.src(configFiles)
.pipe(revReplace({
manifest: manifest,
prefix: absolutePath
}))
.pipe(gulp.dest(output.dist));
});
gulp.task('revision', ['rev-rename'], function() {
var manifestFile = output.dist + '/rev-manifest.json';
var manifestArr = require(manifestFile);
// Rename sourcemaps
for (var i in manifestArr) {
fs.rename(output.dist + '/' + i + '.map', output.dist + '/' + manifestArr[i] + '.map', function() {
// Do nothing
});
}
// Delete the original files from the source dir
var toClean = Object.keys(manifestArr).map(function(p) {
return [output.dist, p].join('/');
});
return del(toClean);
});
gulp.task('pre', function() {
absolutePath = '/mithril-framework/';
});
gulp.task('publish', ['pre', 'build'], function() {
return gulp.src('./dist/**/*')
.pipe(ghPages());
});
gulp.task('build', ['revision']);
gulp.task('default', ['serve']);