forked from loranger/laravel-elixir-bower
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
179 lines (147 loc) · 5.59 KB
/
index.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
var gulp = require('gulp');
var mainBowerFiles = require('main-bower-files');
var elixir = require('laravel-elixir');
var filter = require('gulp-filter');
var notify = require('gulp-notify');
var minify = require('gulp-minify-css');
var uglify = require('gulp-uglify');
var concat = require('gulp-concat');
var changed = require('gulp-changed');
var base64 = require('gulp-base64');
var test = require('gulp-if');
var ignore = require('gulp-ignore');
var getFileSize = require("filesize");
var _ = require('lodash');
elixir.extend('bower', function(options) {
var config = this;
var options = _.merge({
debugging: false,
css: {
file: 'vendor.css',
output: config.cssOutput
},
js: {
file: 'vendor.js',
output: config.jsOutput
},
font: {
output: 'public/fonts'
},
img: {
output: 'public/imgs',
extInline: [ 'gif', 'png'],
maxInlineSize: 32 * 1024 //max 32k on ie8
}
}, options);
gulp.task('bower', ['bower-css', 'bower-js', 'bower-fonts', 'bower-imgs']);
gulp.task('bower-css', function () {
var onError = function (err) {
notify.onError({
title: "Laravel Elixir",
subtitle: "Bower Files CSS Compilation Failed!",
message: "Error: <%= error.message %>",
icon: __dirname + '/../icons/fail.png'
})(err);
this.emit('end');
};
return gulp.src(mainBowerFiles({debugging: options.debugging}))
.on('error', onError)
.pipe(filter('**/*.css'))
.pipe(test(options.img.maxInlineSize > 0, base64({
extensions: options.img.extInline,
maxImageSize: options.img.maxInlineSize, // bytes
debug: options.debugging,
})))
.pipe(concat(options.css.file))
.pipe(minify())
.pipe(gulp.dest(options.css.output))
.pipe(notify({
title: 'Laravel Elixir',
subtitle: 'CSS Bower Files Imported!',
icon: __dirname + '/../icons/laravel.png',
message: ' '
}));
});
gulp.task('bower-js', function () {
var onError = function (err) {
notify.onError({
title: "Laravel Elixir",
subtitle: "Bower Files JS Compilation Failed!",
message: "Error: <%= error.message %>",
icon: __dirname + '/../icons/fail.png'
})(err);
this.emit('end');
};
return gulp.src(mainBowerFiles({debugging: options.debugging}))
.on('error', onError)
.pipe(filter('**/*.js'))
.pipe(concat(options.js.file))
.pipe(uglify())
.pipe(gulp.dest(options.js.output))
.pipe(notify({
title: 'Laravel Elixir',
subtitle: 'Javascript Bower Files Imported!',
icon: __dirname + '/../icons/laravel.png',
message: ' '
}));
});
gulp.task('bower-fonts', function(){
var onError = function (err) {
notify.onError({
title: "Laravel Elixir",
subtitle: "Bower Files Font Copy Failed!",
message: "Error: <%= error.message %>",
icon: __dirname + '/../icons/fail.png'
})(err);
this.emit('end');
};
return gulp.src(mainBowerFiles({
debugging: options.debugging,
filter: (/\.(eot|svg|ttf|woff|woff2|otf)$/i)
}))
.on('error', onError)
.pipe(changed(options.font.output))
.pipe(gulp.dest(options.font.output))
.pipe(notify({
title: 'Laravel Elixir',
subtitle: 'Font Bower Files Imported!',
icon: __dirname + '/../icons/laravel.png',
message: ' '
}));
});
gulp.task('bower-imgs', function () {
var onError = function (err) {
notify.onError({
title: "Laravel Elixir",
subtitle: "Bower Files Images Copy Failed!",
message: "Error: <%= error.message %>",
icon: __dirname + '/../icons/fail.png'
})(err);
this.emit('end');
};
var isInline = function (file) {
var filesize = file.stat ? getFileSize(file.stat.size) : getFileSize(Buffer.byteLength(String(file.contents)));
var fileext = file.path.split('.').pop();
if (options.debugging)
{
console.log("Size of file:" + file.path + " (" + 1024*parseFloat(filesize) +" / max="+options.img.maxInlineSize+")");
}
return options.img.extInline.indexOf(fileext) > -1 && 1024*parseFloat(filesize) < options.img.maxInlineSize;
}
return gulp.src(mainBowerFiles({
debugging: options.debugging,
filter: (/\.(png|bmp|gif|jpg|jpeg)$/i)
}))
.on('error', onError)
.pipe(ignore.exclude(isInline)) // Exclude inlined images
.pipe(changed(options.img.output))
.pipe(gulp.dest(options.img.output))
.pipe(notify({
title: 'Laravel Elixir',
subtitle: 'Images Bower Files Imported!',
icon: __dirname + '/../icons/laravel.png',
message: ' '
}));
});
return this.queueTask('bower');
});