Skip to content

ionic serve doesn't support cordova browser plugins #354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
xeoncross opened this issue Apr 6, 2015 · 48 comments
Closed

ionic serve doesn't support cordova browser plugins #354

xeoncross opened this issue Apr 6, 2015 · 48 comments

Comments

@xeoncross
Copy link

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?

@AMar4enko
Copy link

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.

@xeoncross
Copy link
Author

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
Copy link
Author

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 xeoncross changed the title How do you develop for a browser while using a cordova plugin? ionic serve doesn't support cordova browser plugins Apr 8, 2015
@aleclofabbro
Copy link

any news about it?

@crorodriguezro
Copy link

any news about it? X2

@lrettig
Copy link

lrettig commented Aug 5, 2015

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. Wizcorp/phonegap-facebook-plugin#866, ionic-team/ng-cordova#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.

@manucorporat
Copy link
Contributor

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

@mark-veenstra
Copy link

+1

2 similar comments
@Screeze
Copy link

Screeze commented Nov 15, 2015

+1

@liamzebedee
Copy link

+1

@mark-veenstra
Copy link

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

@derekdata
Copy link

+1

2 similar comments
@fiunchinho
Copy link

+1

@sebastianovide
Copy link

+1

@sebastianovide
Copy link

workaround in #790

@dcanlas
Copy link

dcanlas commented Mar 7, 2016

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

@xinbenlv
Copy link

xinbenlv commented Jun 3, 2016

+1

2 similar comments
@CrazedCoding
Copy link

+1

@wow3065
Copy link

wow3065 commented Jun 20, 2016

+1

@lordlothar99
Copy link

+1

3 similar comments
@movrack
Copy link

movrack commented Jun 28, 2016

+1

@schaergeek
Copy link

+1

@barlos23
Copy link

+1

@BradyNadeau
Copy link

+1
I'm experiencing this issue as well, and it really sucks that ionic run browser --livereload does not work either..
Is there anyone on the Ionic team that is looking into this?? I haven't seen anyone from them comment on this yet.

@joshuaohana
Copy link

Throwing another +1 onto the heap

@marcusgraf
Copy link

+1

@ghost
Copy link

ghost commented Nov 7, 2016

This is really horrible... can you at least throw some log that that's the problem?
Wasted a whole day on this...

@morrisonbrett
Copy link

If I do a ionic build browser, I noticed that the cordova.js and it's dependencies get built to /platforms/browser/cordova/platform_www. The question is (maybe), how do we get that output via WebPack to get generated to /www? Would LOVE to know the answer to that.

@ghost
Copy link

ghost commented Nov 9, 2016

as mark-veenstra already wrote - but let me fix the port from 8100 -> 8000 which is the port the "ionic run browser" works:

add this to your gulp file under the other tasks:

gulp.task('browser:serve', function() {
    gulp.src(paths.browser)
        .pipe(webserver({
            livereload: {enable: true, port: 35729},
            directoryListing: false,
            host: localhost,
            port: 8000,
            open: true
        }));
});

@derotune
Copy link

ionic run browser --live-reload throws Error loading cordova-browser for me. Does anybody knows something about this

@joshuaohana
Copy link

@lordprettyflacko I get that as well but am able to build and serve for browser (pulling in all js and hooks) with ionic build browser --desktop --testing && http-server platforms/browser/www/

@derotune
Copy link

@joshuaohana i tried this but my system cant find the http-server command. However it works for me now with ionic serve browser. I dont know why cause i tried so much thing but in the end it works.

@BradyNadeau
Copy link

BradyNadeau commented Nov 23, 2016

What I have done as a hacky workaround for the time being is just do an ionic build browser and then I copy the platform_www folder from the platforms/browser directory into my www folder and then I reference it inside my index.html with <script src="platform_www/cordova.js"></script>. After doing that my ionic serve will then load all my cordova browser plugins.. (Works 90% of the time, i usually just refresh my page if the plugins don't load on initial startup.)

  1. ionic build browser
  2. copy platforms/browser/platform_www folder to www folder
  3. add <script src="platform_www/cordova.js"></script> to index.html
  4. ionic serve (refresh page if plugins don't load at first.)

@dgofman
Copy link

dgofman commented Dec 1, 2016

By using "ionic run browser --livereload" command I can see the trigger is active:
"HTML changed: www\index.html"

however we need to run a command to compile our changes:

cordova prepare browser

I hope someone can fix it ASAP.

@danielehrhardt
Copy link

+1 Same here

@petermetz
Copy link

After inspecting my node_modules/@ionic/app-scripts/dist/serve.js
I've managed to come up with this change in the package.json's npm script for ionic:serve =>
"ionic:serve": "ionic-app-scripts serve --iscordovaserve --wwwDir platforms/browser/www/ --buildDir platforms/browser/www/build"

This makes my ionic serve use the browser platform as it's basis and all the good stuff is there provided by serve out of the box (all that I need anyway, didn't check everything).

My only issue at present is kind of unrelated, the splash screen plugin requests img/logo.png while this doesn't seem to exist under my root www of platforms/browser/www/.

My logo.png is in the src/assets directory, it would be cool if that got copied over but this may very well be a splash screen plugin issue instead of something to be done on ionic's side.

@meirotstein
Copy link

At the meantime you can use the following in order to use cordova contacts plugin in ionic serve:
https://github.com/meirotstein/cordova-plugin-contacts-mock

@morrisonbrett
Copy link

@petermetz That is a GREAT solution. Working perfectly for me. Thank you!

Slight mod - I like having source maps built:

"ionic:serve": "ionic-app-scripts serve --sourceMap source-map --iscordovaserve --wwwDir platforms/browser/www/ --buildDir platforms/browser/www/build"

@stovmascript
Copy link

stovmascript commented Jan 4, 2017

@petermetz Epic solution, thank you!

To the unrelated issue img/logo.png 404, I solved it by customizing ionic-app-scripts/config/copy.config.js. You can provide your own copy.config.js:

// <projectRoot>/copy.config.js
const defaultConfig = require('@ionic/app-scripts/config/copy.config');

module.exports = Object.assign({}, defaultConfig, {
	copyLogo: {
		src: 'platforms/browser/img/logo.png',
		dest: '{{WWW}}/img',
	},
});

So in your case, you should be able to use {{SRC}}/assets/logo.png as the src.

If you're not already using an extended config like this, note that you also have to add:

// ...
  "config": {
    "ionic_copy": "copy.config.js"
  }
// ...

to your package.json and it works properly from @ionic/app-scripts": "^0.0.48 - other configs work, but a bug specific to copy.config was fixed in this version.

zhuowei added a commit to kylemsguy/Vroomy that referenced this issue Mar 18, 2017
aeremin added a commit to sth-larp/deus-mobile that referenced this issue Mar 26, 2017
selvagsz added a commit to razorpay/razorpay-cordova-sample-app that referenced this issue Jul 21, 2017
akshaybhalotia pushed a commit to razorpay/razorpay-cordova-sample-app that referenced this issue Jul 21, 2017
* ionic start rzp-ionic3-example blank --type=ionic-angular

* cordova platform add browser/ios/android && cordova plugin add https://github.com/razorpay/razorpay-cordova.git --save

* RZP payment

* Use this workaround to load browser plugins ionic-team/ionic-cli#354 (comment)

* Define type for RazorpayCheckout

* Update Readme
@pakbaz
Copy link

pakbaz commented Mar 21, 2018

@petermetz your solution won't work when using azure mobile client plugin. anybody has any idea for a workaround?

@imhoffd imhoffd added the project type: ionic-angular Ionic Angular 2-3 projects label Jul 12, 2018
aeremin added a commit to alice-larp/alice-larp-sdk-deprecated that referenced this issue Aug 28, 2018
@nharrer
Copy link

nharrer commented Feb 14, 2019

With ionic 4 this can be done with:

$ ionic cordova build browser
$ ionic serve --cordova --platform browser

see: #3043

@imhoffd imhoffd removed the project type: ionic-angular Ionic Angular 2-3 projects label Feb 15, 2019
@imhoffd
Copy link
Contributor

imhoffd commented Feb 15, 2019

Yeah, I think we can close this issue. I think the original request is now fulfilled.

#3043 is for a related feature request.

@imhoffd imhoffd closed this as completed Feb 15, 2019
aeremin added a commit to sr-2020/nodejs-monorepo that referenced this issue Jun 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests