- Change some code
- Run the code through crisper because the Content Security Policy for Chrome Apps doesn't allow inline scripts
- Reload the Chrome App from chrome://extensions/
- Repeat
After a bit of googling I found this nice article by Konstantin Raev that deals with the problem of live-reload for (non-Polymer) Chrome Apps and offers a straight-forward, working solution,
using tiny-lr and his own adapation of livereload.js (to work around some Chrome Apps security restrictions).
Using this gulp task will update or reload the Chrome App when any of the source files change, and you can just load your source folder as unpacked extensions, launch your app and start developing/testing:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var gulp = require('gulp'); | |
var tinylr = require('tiny-lr'); | |
gulp.task('dev', function () { | |
var lr = tinylr(); | |
lr.listen(35729); | |
gulp.watch(['**.{js,css,html}'], function (evt) { | |
lr.changed({ | |
body: { | |
files: [evt.path] | |
} | |
}); | |
}); | |
}); |
My first lazy approach was to listen for any changes in the source folders, then run a full build and use the dist folder as unpacked extension.
A full build as per the Polymer Starter Kit involves quite a few steps, like minimizing the css/js/html, optimizing the images and vulcanizing the elements:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gulp.task('build', function (cb) { | |
runSequence( | |
['copy', 'styles'], | |
'elements', | |
['jshint', 'images', 'html'], | |
'vulcanize', | |
cb | |
); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gulp.task('vulcanize', function () { | |
var DEST_DIR = 'dist/elements'; | |
return gulp.src('dist/elements/elements.vulcanized.html') | |
.pipe($.vulcanize({ | |
stripComments: true, | |
inlineCss: true, | |
inlineScripts: true | |
})) | |
.pipe($.crisper()) | |
.pipe(gulp.dest(DEST_DIR)) | |
.pipe($.size({title: 'vulcanize'})); | |
}); |
gulp dev
) I first run a build
, and then repeat the build step whenever something changes in the app
folder. The build creates files in the dist
folder (which is loaded as unpacked app), and livereload is triggered by listening to changes in this folder.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gulp.task('dev', ['build'], function () { | |
var lr = tinylr(); | |
lr.listen(35729); | |
gulp.watch(['app/**'], ['build']); | |
gulp.watch(['dist/**'], function (evt) { | |
lr.changed({ | |
body: { | |
files: [evt.path] | |
} | |
}); | |
}); | |
}); |
So instead I added a simplified dev-build that basically only copies all the files to a `dev` folder (to be loaded as unpacked app)...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gulp.task('dev', ['clean:dev'], function (cb) { | |
runSequence( | |
['copy:app', 'copy:elements', 'styles'], | |
'elements', | |
['jshint', 'images'], | |
'crisper', | |
'watch', | |
cb | |
); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gulp.task('crisper', function () { | |
var elements = gulp.src(['app/elements/**/*.html']) | |
.pipe($.crisper()) | |
.pipe(gulp.dest('dev/elements')); | |
var bower = gulp.src(['bower_components/**/*.html']) | |
.pipe($.crisper()) | |
.pipe(gulp.dest('dev/bower_components')); | |
return merge(bower, elements) | |
.pipe($.size({title: 'crisper'})); | |
}); |
dev/bower_components/
instead of dev/bower_components/polymer/
. This issue is now fixed so make sure to use the newest version 0.0.5 of gulp-crisper.When watching for changes I also don't update everything, every time something changes but listen for specific changes and only update the necessary parts.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gulp.task('watch', function () { | |
var lr = tinylr(); | |
lr.listen(35729); | |
gulp.watch(['app/*'], ['copy:app']); | |
gulp.watch(['app/**/*.js'], ['jshint', 'copy:app']); | |
gulp.watch(['app/styles/**/*.css'], ['styles']); | |
gulp.watch(['app/elements/**/*.css'], ['elements']); | |
gulp.watch(['app/elements/**/*.html'], function () { | |
runSequence( | |
'copy:elements', | |
'crisper' | |
); | |
}); | |
gulp.watch(['app/images/**/*'], ['images']); | |
gulp.watch(['dev/**'], function (evt) { | |
lr.changed({ | |
body: { | |
files: [evt.path] | |
} | |
}); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gulp.watch(['dev/**'], $.batch(function (events, cb) { | |
var paths = []; | |
events.on('data', function (evt) { | |
paths.push(evt.path); | |
}).on('end', function () { | |
lr.changed({ | |
body: { | |
files: paths | |
} | |
}); | |
cb(); | |
}); | |
})); |
Here's a quick video of how this process looks like now.
So with all of this done I can now proceed to work on my Polymer Chrome App after having learnt far more about gulp than I originally intended ☺