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

+1 Same here

danielehrhardt avatar Dec 09 '16 17:12 danielehrhardt

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.

petermetz avatar Dec 26 '16 16:12 petermetz

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

meirotstein avatar Dec 26 '16 23:12 meirotstein

@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"

morrisonbrett avatar Dec 28 '16 07:12 morrisonbrett

@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.

stovmascript avatar Jan 04 '17 14:01 stovmascript

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

pakbaz avatar Mar 21 '18 16:03 pakbaz

With ionic 4 this can be done with:

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

see: https://github.com/ionic-team/ionic-cli/issues/3043

nharrer avatar Feb 14 '19 23:02 nharrer

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

https://github.com/ionic-team/ionic-cli/issues/3043 is for a related feature request.

imhoffd avatar Feb 15 '19 01:02 imhoffd