-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
325 lines (275 loc) · 8.9 KB
/
Copy pathgulpfile.js
File metadata and controls
325 lines (275 loc) · 8.9 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
'use strict';
// Native dependencies
var path = require('path');
var exec = require('child_process').exec;
var util = require('util');
// External dependencies
var gulp = require('gulp');
var gutil = require('gulp-util');
var browserSync = require('browser-sync');
var del = require('del');
var runSequence = require('run-sequence');
var mergeStream = require('merge-stream');
// External dependnecies (browserify stuff)
var browserify = require('browserify');
var vinylSource = require('vinyl-source-stream');
var lazypipe = require('lazypipe');
var polyclean = require('polyclean');
// Load all installed gulp plugins into $
var $ = require('gulp-load-plugins')();
// Read current project data
var BOWER = require('./bower.json');
// Constants
var SRC_DIR = 'src';
var DIST_DIR = '.';
var DEMO_DIR = 'demo';
var TMP_DIR = 'tmp';
var LESS_DIR = [SRC_DIR + '/**/*.less'];
var CSS_DIR = [SRC_DIR + '/**/*.css'];
var JS_DIR = [SRC_DIR + '/**/*.js'];
var HTML_DIR = [SRC_DIR + '/**/*.html', DEMO_DIR + '/**/*.html'];
/**
* Options for the vulcanize-related tasks
* @type {Object}
*/
var VULCANIZE_OPTIONS = {
// Path where tmp files are stored for vulcanization
baseTmpPath: path.join(TMP_DIR, BOWER.name),
// Path to the component file
componentPath: path.join(TMP_DIR, BOWER.name, BOWER.name + '.html'),
// Path to the component injector file
injectorPath: path.join(TMP_DIR, BOWER.name, 'injector.html'),
};
/////////////////////
// auxiliary tasks //
/////////////////////
/**
* Cleans resources
*/
gulp.task('clean', function clean() {
del.sync(TMP_DIR);
});
/**
* Prepares components for vulcanization
*
* Copies multiple directories to match paths described in source
* These paths are emulated by our development server
* and in order to be "browserifiable", they must correspond
* in the filesystem.
*/
gulp.task('build-env', ['less', 'javascript'], function () {
var copySRC = gulp.src(SRC_DIR + '/*')
.pipe($.rename(function (p) {
p.dirname = BOWER.name;
}))
.pipe(gulp.dest(TMP_DIR));
var copyBOWER = gulp.src('bower_components/**/*')
.pipe(gulp.dest(TMP_DIR));
var copyNonBOWER = gulp.src('non_bower_components/**/*')
.pipe(gulp.dest(TMP_DIR));
return mergeStream(copySRC, copyBOWER, copyNonBOWER);
});
/////////////////////
// auxiliary tasks //
/////////////////////
/////////////////
// build tasks //
/////////////////
/**
* Task for compiling less
*/
gulp.task('less', function less() {
return gulp.src(LESS_DIR)
// .pipe($.changed(SRC_DIR, { extension: '.css' }))
.pipe($.duration('Compiling .less files'))
.pipe($.less())
.on('error', $.notify.onError({
title: 'Less compiling error',
message: '<%= error.message %>',
open: 'file:///<%= error.filename %>',
sound: 'Glass',
// Basso, Blow, Bottle, Frog, Funk, Glass, Hero,
// Morse, Ping, Pop, Purr, Sosumi, Submarine, Tink
icon: path.join(__dirname, 'logo.png'),
}))
.pipe($.autoprefixer({
browsers: [
'ie >= 10',
'ie_mob >= 10',
'ff >= 30',
'chrome >= 34',
'safari >= 7',
'opera >= 23',
'ios >= 7',
'android >= 4.4',
'bb >= 10'
],
cascade: false,
}))
.pipe($.polymerizeCss({
styleId: function (file) {
return path.basename(file.path, '.css') + '-styles';
}
}))
.pipe($.rename(function (path) {
path.basename += '-styles';
path.extname = '.html';
}))
// Put files at source dir in order to use them for vulcanization
.pipe(gulp.dest(SRC_DIR))
.pipe($.size({ title: 'less' }));
});
/**
*
*/
gulp.task('javascript', function () {
// set up the browserify instance on a task basis
var b = browserify({
entries: SRC_DIR + '/carbo-inspector.js',
debug: false
});
return b.bundle()
.on('error', $.util.log)
.pipe(vinylSource('carbo-inspector.bundle.js'))
.pipe(gulp.dest(SRC_DIR));
});
/**
* Function for vulcanize task
* We need the temporary directory to run vulcanize
* as the routes we use in development are virtual
*/
gulp.task('vulcanize:component', ['build-env'], function vulcanize() {
// Build process for post-vulcanized stuff
// Taken from polybuild.
// https://github.com/PolymerLabs/polybuild/blob/master/index.js
var vulcanizePipe = lazypipe()
// inline html imports, scripts and css
// also remove html comments
.pipe($.vulcanize, {
excludes: [
// polymer
path.join(TMP_DIR, '/polymer/polymer.html'),
// lodash
path.join(TMP_DIR, '/lodash/lodash.js'),
// carbo-highlighter
path.join(TMP_DIR, '/carbo-highlighter/carbo-highlighter.html')
],
inlineScripts: true,
inlineCss: true,
stripComments: true
})
// remove whitespace from inline css
.pipe(polyclean.cleanCss)
.pipe(polyclean.uglifyJs);
return gulp.src(VULCANIZE_OPTIONS.componentPath)
.pipe(vulcanizePipe())
.on('error', function (e) {
$.util.log($.util.colors.red(e));
})
.pipe(gulp.dest('.'))
.pipe($.size({title: 'vulcanize' }));
});
/**
* Adds the injection script to the component
*/
gulp.task('vulcanize:injector', ['build-env'], function () {
// Build process for post-vulcanized stuff
// Taken from polybuild.
// https://github.com/PolymerLabs/polybuild/blob/master/index.js
var vulcanizePipe = lazypipe()
// inline html imports, scripts and css
// also remove html comments
.pipe($.vulcanize, {
excludes: [
// polymer
path.join(TMP_DIR, '/polymer/polymer.html'),
// do not exclude lodash, as we are creating a
// single bundle here
],
inlineScripts: true,
inlineCss: true,
stripComments: true
})
// remove whitespace from inline css
// .pipe(polyclean.cleanCss)
// .pipe(polyclean.uglifyJs);
var polymerImportRegExp = /<link\s+.*?href=".*?polymer\.html".*?>/;
return gulp.src([VULCANIZE_OPTIONS.componentPath, VULCANIZE_OPTIONS.injectorPath])
.pipe($.concat('carbo-inspector.injector.html'))
.pipe(gulp.dest(VULCANIZE_OPTIONS.baseTmpPath))
.pipe(vulcanizePipe())
.on('error', function (e) {
$.util.log($.util.colors.red(e));
})
// Remove all imports referencing polymer
// so that the injected component does
// not try to load it's own polymer instance.
// Polymer requires to be loaded only once.
// Do removal at the end of the pipeline
// so that all references, including those written
// in dependent components are removed.
.pipe($.replace(
polymerImportRegExp,
'<!-- removed polymer ref -->'
))
.pipe(gulp.dest('.'))
.pipe($.size({title: 'vulcanize:injector' }));
});
// Register tasks
gulp.task('distribute', function () {
runSequence('vulcanize:component', 'vulcanize:injector', 'clean');
});
/////////////////
// build tasks //
/////////////////
///////////////////////
// development tasks //
///////////////////////
/**
* Serves the application
*/
gulp.task('serve', function () {
browserSync({
port: 4000,
server: {
baseDir: './',
index: './demo/src.html',
},
routes: {
'/bower_components': [
'/',
'bower_components',
// Serve demo/bower_components so that we can have
// demos inside folders, better packaged
'demo/bower_components'
]
},
// Serve static files as if they were available at the
// root url path
serveStatic: ['bower_components', 'non_bower_components'],
open: true,
// tunnel: true
});
});
/**
* Watches for changes and reloads the browser
*/
gulp.task('watch', function () {
// Watch LESS files for changes
gulp.watch(LESS_DIR, ['less']);
// Watch JS files
gulp.watch(JS_DIR, ['javascript']);
// Reload
var reloadDirs = HTML_DIR
.concat([
'carbo-inspector.injector.html',
SRC_DIR + '/carbo-inspector.bundle.js'
]);
gulp.watch(reloadDirs, browserSync.reload);
});
// Serve & watch
gulp.task('develop', ['less', 'javascript', 'serve', 'watch']);
gulp.task('default', ['develop']);
///////////////////////
// development tasks //
///////////////////////