ionic serve doesn't support cordova browser plugins
Some (but not all) cordova plugins have Javascript proxies if the features they offer can also be used in a regular browser.
I'm using one such plugin and I need to access the cordova.plugins.____ object. However, the cordova.js file is a 404 when using ionic serve. It's not a 404 when using ionic build browser.
How do you access the cordova object in javascript when using ionic serve? Or do you have to ionic build browser every single time you want to see your changes?
Create stubbed version of service:
Geo.service('Geolocation', function ($q){
this.getCurrentLocation = function (){
var d;
if(window.cordova){
d = $q.defer();
navigator.geolocation.getCurrentPosition(function (position){
d.resolve({
lat: position.coords.latitude,
lng: position.coords.longitude
});
}, function (){
d.resolve(DEFAULT_LOCATION);
}, {frequency:5000, maximumAge: 0, timeout: 5000});
return d.promise;
}
return $q.when(DEFAULT_LOCATION);
};
});
Make sure everything works fine in browser, run your app in live mode on emulator like
$ ionic emulate android -l -c -s
Use safari for iOS or Chrome for Android to debug app running on the device.
That is certainly a solution for skipping certain features when not on a mobile device - but what if the cordova plugin contains code for a browser version as well? Then you still have to access it via cordova.plugins and the only way it seems to get that cordova.js/cordova_plugins.js code is to run ionic build browser.
Is there no way to develop without having to rebuild all the time?
At the moment I'm trying to work around this by using the gulp file to process everything for me. I'm still surprised that you can't use a cordova plugin while developing in browser.
First I manually pulled the ./plugins/name/www/*.js out and placed them inside ./www/plugin/file.js. This allows me to modify them as I work and ionic serve picks them up. From here, gulp takes over.
gulp.task('plugin-overwrite', function() {
// Step 1: Move the edited files back into the plugin folder
gulp.src('./www/plugin/file.js')
.pipe(gulp.dest('./plugins/com.plugin/src/browser/'));
// Step 2: Build the project
if (sh.exec('ionic build browser').code !== 0) {
console.log('Failed to build for browser');
process.exit(1);
}
// Step 3: Move built files back into webroot
gulp.src(['./platforms/browser/www/cordova.js', './platforms/browser/www/cordova_plugins.js'])
.pipe(gulp.dest('./www/build/'));
gulp.src('./platforms/browser/www/plugins')
.pipe(gulp.dest('./www/build/'));
});
Then my index.html manually includes ./www/build/cordova.js which loads the plugins out of ./www/build/plugins/...
If anyone has any better ideas please let me know.
any news about it?
any news about it? X2
Hello,
I'm new to ionic and it's been a wonderful experience except for the fact that I lost several hours completely stuck thanks to this issue. The 'ionic serve' experience is great and it's not at all obvious that you don't have access to cordova plugins, nor to 'cordova' itself, in this environment.
Any news (X3) on fixing this? It has caused headaches for lots of people already (cf. https://github.com/Wizcorp/phonegap-facebook-plugin/issues/866, https://github.com/driftyco/ng-cordova/issues/446, http://stackoverflow.com/questions/27906239/facebookconnectplugin-is-not-defined-ngcordova-ionic-app, etc.) and fixing it will be critical to maintaining the otherwise high quality of the ionic experience.
Thank you.
any news about it? X4 here, problems with phonegap-facebook-plugin
+1
+1
+1
At this moment we solved this issue by generating a Gulp command that serves the platforms/browser/www directory. You should manually build for browser each time you make a change (ionic build browser):
gulp.task('browser:serve', function() {
gulp.src(paths.browser)
.pipe(webserver({
livereload: {enable: true, port: 35729},
directoryListing: false,
host: localhost,
port: 8100,
open: true
}));
});
+1
+1
+1
workaround in https://github.com/driftyco/ionic-cli/issues/790
Having similar problems when using phonegap-facebook-plugin. Would also like to know if possible to resolve this when doing 'ionic serve'
+1
+1
+1
+1