gulpfile.js 9.6 KB

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