Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cordova hot reloading on device without Ionic

I'm using Cordova without Ionic or any other Framework. My problem is that I don't find any hot reload features or plugins for Cordova without using Ionic. Is there any solution to live reload on the iOS simulator without any frameworks?

like image 702
Eyk Rehbein Avatar asked Sep 17 '17 10:09

Eyk Rehbein


People also ask

How do I run ionic Cordova on a device?

ionic cordova run. Run an Ionic project on a connected device. ionic cordova run [<platform>] [options] Build your app and deploy it to devices and emulators using this command. Optionally specify the --livereload option to use the dev server from ionic serve for livereload functionality.

How do I start the live reload process with the ionic CLI?

The Ionic CLI includes a complete Live Reload experience, automating all of the steps that are detailed manually below. Install it along with native-run (a cross-platform command-line utility for running native binaries on devices and simulators/emulators): Next, use the ionic cap run command to start the Live Reload process:

How do I deploy an ionic app to an Android device?

For Android devices, the Ionic CLI will automatically forward the dev server port. This means you can use a localhost address and it will refer to your computer when loaded in the Web View, not the device. The following all-in-one command will start a live-reload server on localhost and deploy the app to an Android device using Cordova:

Does capacitor support live reload with Ionic CLI?

Capacitor does not yet have a programmatic build for development (track this issue for progress), so the Ionic CLI does not automatically forward ports for iOS and Android. To use Live Reload with Capacitor, make sure you're either using a virtual device or a hardware device connected to the same Wi-Fi network as your computer.


1 Answers

I've implemented a custom way of 'hot reloading' in Cordova. I don't know how original it is but it works well for my needs. In broad lines it works like this: in development mode a static webserver is started and cordova is instructed that the content is the url of this server: <content src="http://10.0.3.2:8080" />.

The static server also listens to changes in the assets (js/css/html) and auto reloads. We use gulp connect (https://www.npmjs.com/package/gulp-connect) to achieve this.

In production mode you have to compile the assets and instruct cordova to use the regular static file to load your app.

Details:

In cordova.xml this is the line that tells cordova where to start the app:

<content src="index.html" />

So this has to be replaced with a 'dynamic' version that would allow hot reload. I achieved this by using gulp-connect which starts a static file server.

  gulp.task('connect', function () {
    return connect.server({
      root: 'www',
      livereload: true,
      fallback: 'www/index.html',
      https: false
    });
  });

I created two tasks which switch the cordova configuration in development and in production:

  // Development
  // adds the localhost(on the emulator as 10.0.3.2) as
  // the content source for the cordova app
  gulp.task('cordova-dev-server-android', function () {
    return gulp.src(['config.xml'])
      .pipe(replace(/(<content src=\")(.+)(\" \/>)/g, "$1http://10.0.3.2:8080$3"))
      .pipe(gulp.dest('.'));
  });

  // Production
  // adds the static file as
  // the content source for the cordova app
  gulp.task('cordova-static-file', function () {
    return gulp.src(['config.xml'])
      .pipe(replace(/(<content src=\")(.+)(\" \/>)/g, "$1index.html$3"))
      .pipe(gulp.dest('.'));
  });

One important thing you have to ensure that the Cordova javascript files are accessible by the development web server. Again, I achieved this with two tasks for development/production.

  // Development
  // Creates symlinks for the devserver
  // so the app has access to cordova files
  gulp.task('create-cordova-symlink-android', ['remove-cordova-symlink'], function () {
    return gulp.src('')
      .pipe(exec('ln -sv ../platforms/android/assets/www/cordova.js www'))
      .pipe(exec.reporter())
      .pipe(exec('ln -sv ../platforms/android/assets/www/cordova_plugins.js www'))
      .pipe(exec.reporter())
      .pipe(exec('ln -sv ../platforms/android/assets/www/plugins www'))
      .pipe(exec.reporter());
  }); 


  // Production
  // Removes symlinks for production
  // see create-cordova-symlink-android
  gulp.task('remove-cordova-symlink', function () {
    var options = {
      continueOnError: true
    };
    return gulp.src('')
      .pipe(exec('rm www/cordova.js', options))
      .pipe(exec('rm www/cordova_plugins.js', options))
      .pipe(exec('rm -Rf www/plugins', options));
  });

I am using gulp here but this can be implemented using any task runner, and of course for other platforms you have to modify this code a bit.

Hope this helps.

like image 63
Ionut Popa Avatar answered Oct 10 '22 20:10

Ionut Popa