gulpfile.js 10.0 KB

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