ionic-cli icon indicating copy to clipboard operation
ionic-cli copied to clipboard

ionic serve doesn't support cordova browser plugins

Open xeoncross opened this issue 11 years ago • 48 comments

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?

xeoncross avatar Apr 06 '15 20:04 xeoncross

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.

AMar4enko avatar Apr 07 '15 10:04 AMar4enko

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?

xeoncross avatar Apr 07 '15 14:04 xeoncross

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.

xeoncross avatar Apr 08 '15 17:04 xeoncross

any news about it?

aleclofabbro avatar Jul 17 '15 22:07 aleclofabbro

any news about it? X2

crorodriguezro avatar Jul 28 '15 06:07 crorodriguezro

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.

lrettig avatar Aug 05 '15 12:08 lrettig

any news about it? X4 here, problems with phonegap-facebook-plugin

manucorporat avatar Aug 14 '15 17:08 manucorporat

+1

mark-veenstra avatar Sep 22 '15 13:09 mark-veenstra

+1

Screeze avatar Nov 15 '15 18:11 Screeze

+1

liamzebedee avatar Nov 16 '15 04:11 liamzebedee

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
        }));
});

mark-veenstra avatar Nov 16 '15 08:11 mark-veenstra

+1

derekdata avatar Jan 01 '16 16:01 derekdata

+1

fiunchinho avatar Jan 27 '16 22:01 fiunchinho

+1

sebastianovide avatar Feb 22 '16 08:02 sebastianovide

workaround in https://github.com/driftyco/ionic-cli/issues/790

sebastianovide avatar Feb 22 '16 09:02 sebastianovide

Having similar problems when using phonegap-facebook-plugin. Would also like to know if possible to resolve this when doing 'ionic serve'

dcanlas avatar Mar 07 '16 00:03 dcanlas

+1

xinbenlv avatar Jun 03 '16 03:06 xinbenlv

+1

CrazedCoding avatar Jun 09 '16 03:06 CrazedCoding

+1

wow3065 avatar Jun 20 '16 08:06 wow3065

+1

lordlothar99 avatar Jun 21 '16 21:06 lordlothar99