'How do I setup a "build" script for a custom (vanilla Javascript) Ionic app?

I'm a tech teacher and I'm designing a coding course for high school students. I want to teach them Ionic without also having to teach them React, Angular, or Vue. They already know basic web development, so I want them to be able to create iOS, Android, and web apps using only vanilla Javascript + Ionic.

When I run "ionic init" to get started, I'm forced to select from Angluar, React, Vue, or Custom. So, of course, I select Custom.

Then I start building a "Hello World" app. When it comes time to view the simple app in Android Studio, I run "ionic capacitor add android" and then "npx cap open android." All good.

The problem arises when I start adding more to my simple app, and then try to view those changes in Android Studio. Normally, this would be done by running "ionic capacitor copy android". But that doesn't work in my custom app. I get this error message:

[ERROR] Cannot perform build.

Since you're using the custom project type, you must provide the ionic:build npm script so the Ionic CLI can build your project.

How do I solve this problem? How I provide a build script for my custom app?

Thank you for your help!



Solution 1:[1]

Since you are using vanilaJS you shouldn't need any build step at all. Just put your index.html to www folder, with some content. You can start off with something like

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">

    <title>Ionic without any framework</title>

    <script type="module" src="https://cdn.jsdelivr.net/npm/@ionic/core@latest/dist/ionic/ionic.esm.js"></script>
    <script nomodule src="https://cdn.jsdelivr.net/npm/@ionic/core@latest/dist/ionic/ionic.js"></script>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@ionic/core@latest/css/ionic.bundle.css"/>
</head>
<body>

<!-- We create a vanilla Javascript function to call the alert -->
<script>
    hello = async function () {
        alert('Hello World!');
    };
</script>

<!-- We declare an Ionic app using the <ion-app/> element -->
<ion-app>
    <!-- Cool thing, the Ionic CSS utilities could be used too -->
    <ion-content text-center>
        <h1>Basic usage</h1>
        <!-- We add an ion-button with an onclick event -->
        <ion-button onclick="hello()">Click me</ion-button>
    </ion-content>
</ion-app>

</body>
</html>

and then ionic capacitor copy android should work. (I assume following initialization steps ionic init, npm init, ionic capacitor add android)

Solution 2:[2]

I think what is needed after making any changes is to run npx cap sync and then npx cap open android.

Solution 3:[3]

Since it would appear you are trying to teach them the bar bones of development I would not even use Ionic. It is a framework for cross-platform development. And once you make a choice of using that framework, you may as well buy in to one of the Javascript/Typescript development frameworks to get the advantages those bring.

For doing POJO work targeted to iOS and Android, I would investigate using Cordova.

Consider it the difference between teaching a student the basics of audio recording on a four-track tape deck rather than a 128-track digital studio with all the bells and whistles.

Many of the basics are the same without the complications of all the advancements.

At my day job doing Enterprise-level financial applications we still use Cordova and have no plans to migrate any time soon.

In my admittedly biased opinion, Cordova would be a better starting point for a "Plain old Javascript object" based introduction to cross-platform development.

This will make the leap into Ionic and Capacitor easier.

Because, again just my opinion, if they haven't learned the advantages of a framework like Angular, going to Ionic/Capacitor is the cart before the horse.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tomas Panik
Solution 2 S.B
Solution 3