1
+ /*
2
+ Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
3
+ This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
4
+ The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
5
+ The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
6
+ Code distributed by Google as part of the polymer project is also
7
+ subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
8
+ */
9
+
10
+ 'use strict' ;
11
+
12
+ // Include promise polyfill for node 0.10 compatibility
13
+ require ( 'es6-promise' ) . polyfill ( ) ;
14
+
15
+ // Include Gulp & tools we'll use
1
16
var gulp = require ( 'gulp' ) ;
17
+ var $ = require ( 'gulp-load-plugins' ) ( ) ;
2
18
var del = require ( 'del' ) ;
3
- var browserify = require ( 'browserify' ) ;
19
+ var runSequence = require ( 'run-sequence' ) ;
20
+ var browserSync = require ( 'browser-sync' ) ;
21
+ var reload = browserSync . reload ;
22
+ var merge = require ( 'merge-stream' ) ;
23
+ var path = require ( 'path' ) ;
24
+ var fs = require ( 'fs' ) ;
25
+ var glob = require ( 'glob-all' ) ;
26
+ var historyApiFallback = require ( 'connect-history-api-fallback' ) ;
27
+ var packageJson = require ( './package.json' ) ;
28
+ var crypto = require ( 'crypto' ) ;
29
+ var ensureFiles = require ( './tasks/ensure-files.js' ) ;
30
+
31
+ // var ghPages = require('gulp-gh-pages');
32
+
33
+ var AUTOPREFIXER_BROWSERS = [
34
+ 'ie >= 10' ,
35
+ 'ie_mob >= 10' ,
36
+ 'ff >= 30' ,
37
+ 'chrome >= 34' ,
38
+ 'safari >= 7' ,
39
+ 'opera >= 23' ,
40
+ 'ios >= 7' ,
41
+ 'android >= 4.4' ,
42
+ 'bb >= 10'
43
+ ] ;
44
+
45
+ var DIST = 'dist' ;
46
+
47
+ var dist = function ( subpath ) {
48
+ return ! subpath ? DIST : path . join ( DIST , subpath ) ;
49
+ } ;
50
+
51
+ var styleTask = function ( stylesPath , srcs ) {
52
+ return gulp . src ( srcs . map ( function ( src ) {
53
+ return path . join ( '.' , stylesPath , src ) ;
54
+ } ) )
55
+ . pipe ( $ . changed ( stylesPath , { extension : '.css' } ) )
56
+ . pipe ( $ . autoprefixer ( AUTOPREFIXER_BROWSERS ) )
57
+ . pipe ( gulp . dest ( '.tmp/' + stylesPath ) )
58
+ . pipe ( $ . minifyCss ( ) )
59
+ . pipe ( gulp . dest ( dist ( stylesPath ) ) )
60
+ . pipe ( $ . size ( { title : stylesPath } ) ) ;
61
+ } ;
62
+
63
+ var imageOptimizeTask = function ( src , dest ) {
64
+ return gulp . src ( src )
65
+ . pipe ( $ . imagemin ( {
66
+ progressive : true ,
67
+ interlaced : true
68
+ } ) )
69
+ . pipe ( gulp . dest ( dest ) )
70
+ . pipe ( $ . size ( { title : 'images' } ) ) ;
71
+ } ;
72
+
73
+ var optimizeHtmlTask = function ( src , dest ) {
74
+ var assets = $ . useref . assets ( {
75
+ searchPath : [ '.tmp' , '.' ]
76
+ } ) ;
77
+
78
+ return gulp . src ( src )
79
+ . pipe ( assets )
80
+ // Concatenate and minify JavaScript
81
+ . pipe ( $ . if ( '*.js' , $ . uglify ( {
82
+ preserveComments : 'some'
83
+ } ) ) )
84
+ // Concatenate and minify styles
85
+ // In case you are still using useref build blocks
86
+ . pipe ( $ . if ( '*.css' , $ . minifyCss ( ) ) )
87
+ . pipe ( assets . restore ( ) )
88
+ . pipe ( $ . useref ( ) )
89
+ // Minify any HTML
90
+ . pipe ( $ . if ( '*.html' , $ . minifyHtml ( {
91
+ quotes : true ,
92
+ empty : true ,
93
+ spare : true
94
+ } ) ) )
95
+ // Output files
96
+ . pipe ( gulp . dest ( dest ) )
97
+ . pipe ( $ . size ( {
98
+ title : 'html'
99
+ } ) ) ;
100
+ } ;
101
+
102
+ // Compile and automatically prefix stylesheets
103
+ gulp . task ( 'styles' , function ( ) {
104
+ return styleTask ( 'styles' , [ '**/*.css' ] ) ;
105
+ } ) ;
106
+
107
+ // Ensure that we are not missing required files for the project
108
+ // "dot" files are specifically tricky due to them being hidden on
109
+ // some systems.
110
+ gulp . task ( 'ensureFiles' , function ( cb ) {
111
+ var requiredFiles = [ '.bowerrc' ] ;
112
+
113
+ ensureFiles ( requiredFiles . map ( function ( p ) {
114
+ return path . join ( __dirname , p ) ;
115
+ } ) , cb ) ;
116
+ } ) ;
117
+
118
+ // Optimize images
119
+ gulp . task ( 'images' , function ( ) {
120
+ return imageOptimizeTask ( './images/**/*' , dist ( 'images' ) ) ;
121
+ } ) ;
122
+
123
+ // Copy all files at the root level (app)
124
+ gulp . task ( 'copy' , function ( ) {
125
+ var app = gulp . src ( [
126
+ './*' ,
127
+ '!./test' ,
128
+ '!./elements' ,
129
+ '!./bower_components' ,
130
+ '!./cache-config.json' ,
131
+ '!**/.DS_Store'
132
+ ] , {
133
+ dot : true
134
+ } ) . pipe ( gulp . dest ( dist ( ) ) ) ;
135
+
136
+ // Copy over only the bower_components we need
137
+ // These are things which cannot be vulcanized
138
+ var bower = gulp . src ( [
139
+ 'bower_components/{webcomponentsjs,platinum-sw,sw-toolbox,promise-polyfill}/**/*'
140
+ ] ) . pipe ( gulp . dest ( dist ( 'bower_components' ) ) ) ;
141
+
142
+ return merge ( app , bower )
143
+ . pipe ( $ . size ( {
144
+ title : 'copy'
145
+ } ) ) ;
146
+ } ) ;
4
147
5
- gulp . task ( 'default' , function ( ) {
6
- // default task
148
+ // Copy web fonts to dist
149
+ gulp . task ( 'fonts' , function ( ) {
150
+ return gulp . src ( [ './fonts/**' ] )
151
+ . pipe ( gulp . dest ( dist ( 'fonts' ) ) )
152
+ . pipe ( $ . size ( {
153
+ title : 'fonts'
154
+ } ) ) ;
7
155
} ) ;
8
156
9
- gulp . task ( 'clear' , function ( ) {
10
- return del ( [ 'elements.min.html' ] ) ;
157
+ // Scan your HTML for assets & optimize them
158
+ gulp . task ( 'html' , function ( ) {
159
+ return optimizeHtmlTask (
160
+ [ 'elements/**/*.html' , '!elements/{elements,test,bower_components}/**/*.html' ] ,
161
+ dist ( ) ) ;
11
162
} ) ;
12
163
13
- gulp . task ( 'minify' , function ( ) {
14
- var b = browserify ( 'bower_components/**/*' ) ;
15
-
16
- } ) ;
164
+ // Vulcanize granular configuration
165
+ gulp . task ( 'vulcanize' , function ( ) {
166
+ return gulp . src ( './elements/elements.html' )
167
+ . pipe ( $ . vulcanize ( {
168
+ stripComments : true ,
169
+ inlineCss : true ,
170
+ inlineScripts : true
171
+ } ) )
172
+ . pipe ( gulp . dest ( dist ( 'elements' ) ) )
173
+ . pipe ( $ . size ( { title : 'vulcanize' } ) ) ;
174
+ } ) ;
175
+
176
+ // Generate config data for the <sw-precache-cache> element.
177
+ // This include a list of files that should be precached, as well as a (hopefully unique) cache
178
+ // id that ensure that multiple PSK projects don't share the same Cache Storage.
179
+ // This task does not run by default, but if you are interested in using service worker caching
180
+ // in your project, please enable it within the 'default' task.
181
+ // See https://github.com/PolymerElements/polymer-starter-kit#enable-service-worker-support
182
+ // for more context.
183
+ gulp . task ( 'cache-config' , function ( callback ) {
184
+ var dir = dist ( ) ;
185
+ var config = {
186
+ cacheId : packageJson . name || path . basename ( __dirname ) ,
187
+ disabled : false
188
+ } ;
189
+
190
+ glob ( [
191
+ 'index.html' ,
192
+ './' ,
193
+ 'bower_components/webcomponentsjs/webcomponents-lite.min.js' ,
194
+ '{elements,scripts,styles}/**/*.*' ] ,
195
+ { cwd : dir } , function ( error , files ) {
196
+ if ( error ) {
197
+ callback ( error ) ;
198
+ } else {
199
+ config . precache = files ;
200
+
201
+ var md5 = crypto . createHash ( 'md5' ) ;
202
+ md5 . update ( JSON . stringify ( config . precache ) ) ;
203
+ config . precacheFingerprint = md5 . digest ( 'hex' ) ;
204
+
205
+ var configPath = path . join ( dir , 'cache-config.json' ) ;
206
+ fs . writeFile ( configPath , JSON . stringify ( config ) , callback ) ;
207
+ }
208
+ } ) ;
209
+ } ) ;
210
+
211
+ // Clean output directory
212
+ gulp . task ( 'clean' , function ( ) {
213
+ return del ( [ '.tmp' , dist ( ) ] ) ;
214
+ } ) ;
215
+
216
+ // Watch files for changes & reload
217
+ gulp . task ( 'serve' , [ 'styles' ] , function ( ) {
218
+ browserSync ( {
219
+ port : 5000 ,
220
+ notify : false ,
221
+ logPrefix : 'PSK' ,
222
+ snippetOptions : {
223
+ rule : {
224
+ match : '<span id="browser-sync-binding"></span>' ,
225
+ fn : function ( snippet ) {
226
+ return snippet ;
227
+ }
228
+ }
229
+ } ,
230
+ // Run as an https by uncommenting 'https: true'
231
+ // Note: this uses an unsigned certificate which on first access
232
+ // will present a certificate warning in the browser.
233
+ // https: true,
234
+ server : {
235
+ baseDir : [ '.tmp' , '.' ] ,
236
+ middleware : [ historyApiFallback ( ) ]
237
+ }
238
+ } ) ;
239
+
240
+ gulp . watch ( [ './**/*.html' , '!./bower_components/**/*.html' ] , reload ) ;
241
+ gulp . watch ( [ './styles/**/*.css' ] , [ 'styles' , reload ] ) ;
242
+ gulp . watch ( [ './scripts/**/*.js' ] , reload ) ;
243
+ gulp . watch ( [ './images/**/*' ] , reload ) ;
244
+ } ) ;
245
+
246
+ // Build and serve the output from the dist build
247
+ gulp . task ( 'serve:dist' , [ 'default' ] , function ( ) {
248
+ browserSync ( {
249
+ port : 5001 ,
250
+ notify : false ,
251
+ logPrefix : 'PSK' ,
252
+ snippetOptions : {
253
+ rule : {
254
+ match : '<span id="browser-sync-binding"></span>' ,
255
+ fn : function ( snippet ) {
256
+ return snippet ;
257
+ }
258
+ }
259
+ } ,
260
+ // Run as an https by uncommenting 'https: true'
261
+ // Note: this uses an unsigned certificate which on first access
262
+ // will present a certificate warning in the browser.
263
+ // https: true,
264
+ server : dist ( ) ,
265
+ middleware : [ historyApiFallback ( ) ]
266
+ } ) ;
267
+ } ) ;
268
+
269
+ // Build production files, the default task
270
+ gulp . task ( 'default' , [ 'clean' ] , function ( cb ) {
271
+ // Uncomment 'cache-config' if you are going to use service workers.
272
+ runSequence (
273
+ [ 'ensureFiles' , 'copy' , 'styles' ] ,
274
+ [ 'images' , 'fonts' , 'html' ] ,
275
+ 'vulcanize' , // 'cache-config',
276
+ cb ) ;
277
+ } ) ;
278
+
279
+ // Build then deploy to GitHub pages gh-pages branch
280
+ gulp . task ( 'build-deploy-gh-pages' , function ( cb ) {
281
+ runSequence (
282
+ 'default' ,
283
+ 'deploy-gh-pages' ,
284
+ cb ) ;
285
+ } ) ;
286
+
287
+ // Deploy to GitHub pages gh-pages branch
288
+ gulp . task ( 'deploy-gh-pages' , function ( ) {
289
+ return gulp . src ( dist ( '**/*' ) )
290
+ // Check if running task from Travis CI, if so run using GH_TOKEN
291
+ // otherwise run using ghPages defaults.
292
+ . pipe ( $ . if ( process . env . TRAVIS === 'true' , $ . ghPages ( {
293
+ remoteUrl :
'https://[email protected] /polymerelements/polymer-starter-kit.git' ,
294
+ silent : true ,
295
+ branch : 'gh-pages'
296
+ } ) , $ . ghPages ( ) ) ) ;
297
+ } ) ;
298
+
299
+ // Load tasks for web-component-tester
300
+ // Adds tasks for `gulp test:local` and `gulp test:remote`
301
+ require ( 'web-component-tester' ) . gulp . init ( gulp ) ;
302
+
303
+ // Load custom tasks from the `tasks` directory
304
+ try {
305
+ require ( 'require-dir' ) ( 'tasks' ) ;
306
+ } catch ( err ) {
307
+ // Do nothing
308
+ }
0 commit comments