|
| 1 | +--- |
| 2 | +title: 'Ionic Core Custom Components' |
| 3 | +sidebar_label: Ionic Core |
| 4 | +--- |
| 5 | + |
| 6 | +# Setting Up an Ionic Core Project with Vite (Vanilla JS) |
| 7 | + |
| 8 | +This guide walks you through integrating [Ionic Core](https://ionicframework.com/docs/components) with a plain JavaScript (Vanilla) project using Vite. You'll get tree-shaking support, efficient bundle sizes, and a solid development experience. |
| 9 | + |
| 10 | +# 1. Navigate to your desired directory and create a new Vite project |
| 11 | +Open your terminal and run: |
| 12 | +```shell |
| 13 | +npm create vite@latest my-vanilla-ionic-app -- --template vanilla |
| 14 | +``` |
| 15 | + |
| 16 | +# 2. Move into your project folder |
| 17 | + |
| 18 | +```shell |
| 19 | +cd my-vanilla-ionic-app |
| 20 | +``` |
| 21 | + |
| 22 | +# 3. Install project dependencies |
| 23 | +Install both Vite's dependencies and Ionic Core: |
| 24 | +```shell |
| 25 | +npm install |
| 26 | +npm install @ionic/core |
| 27 | +``` |
| 28 | + |
| 29 | +# 5. Link Ionic CSS in `index.html` |
| 30 | + |
| 31 | +Add the following `<link>` inside the `<head>` tag in index.html: |
| 32 | + |
| 33 | +`<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@ionic/core/css/ionic.bundle.css" />` |
| 34 | + |
| 35 | +```html |
| 36 | +<!DOCTYPE html> |
| 37 | +<html lang="en"> |
| 38 | + <head> |
| 39 | + <meta charset="UTF-8" /> |
| 40 | + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 41 | + <link rel="icon" type="image/svg+xml" href="/vite.svg" /> |
| 42 | + <title>Ionic Core - Example</title> |
| 43 | + <link |
| 44 | + rel="stylesheet" |
| 45 | + href="https://cdn.jsdelivr.net/npm/@ionic/core/css/ionic.bundle.css" |
| 46 | + /> |
| 47 | + </head> |
| 48 | + <body> |
| 49 | + <ion-app> |
| 50 | + <ion-header> |
| 51 | + <ion-toolbar> |
| 52 | + <ion-title>My Ionic App</ion-title> |
| 53 | + </ion-toolbar> |
| 54 | + </ion-header> |
| 55 | + <ion-content class="ion-padding"> |
| 56 | + <ion-button expand="block">Click Me</ion-button> |
| 57 | + </ion-content> |
| 58 | + </ion-app> |
| 59 | + </body> |
| 60 | +</html> |
| 61 | +``` |
| 62 | + |
| 63 | +# 6. Configure main.js for Tree-Shaking |
| 64 | + |
| 65 | + - Open your project's main JavaScript entry file, which Vite typically names `main.js` in the project's root. |
| 66 | + - Add the Ionic `defineCustomElements` code to this file: |
| 67 | + |
| 68 | +```js |
| 69 | +import { defineCustomElements } from '@ionic/core/loader'; |
| 70 | + |
| 71 | +const resourcesUrl = '/'; |
| 72 | + |
| 73 | +defineCustomElements(window, { |
| 74 | + components: [ |
| 75 | + 'ion-app', |
| 76 | + 'ion-button', |
| 77 | + 'ion-card', |
| 78 | + 'ion-header', |
| 79 | + 'ion-toolbar', |
| 80 | + 'ion-title', |
| 81 | + 'ion-content', |
| 82 | + ], |
| 83 | + resourcesUrl: resourcesUrl, |
| 84 | +}); |
| 85 | +``` |
| 86 | + |
| 87 | +# 7. Run the Development Server |
| 88 | + |
| 89 | +Now that we're finally all set-up we can start our development server: |
| 90 | + |
| 91 | +```shell |
| 92 | +npm run dev |
| 93 | +``` |
| 94 | + |
| 95 | +Your app will be available at http://localhost:5173. You should see Ionic-styled components working as expected. |
| 96 | + |
| 97 | +9. Build for Production: |
| 98 | + |
| 99 | +```shell |
| 100 | +npm run build |
| 101 | +``` |
| 102 | + |
| 103 | +The optimized production build will be output to the dist/ directory. Tree-shaking ensures only the components listed in main.js are included. |
| 104 | + |
| 105 | +# Bonus: Framework Integrations |
| 106 | + |
| 107 | +You can use Ionic Web Components in other frameworks that support custom elements. |
| 108 | + |
| 109 | +Live examples below: |
| 110 | + |
| 111 | +- [Svelte](https://stackblitz.com/edit/vitejs-vite-bp6vxnem?file=src%2FApp.svelte) |
| 112 | + |
| 113 | +- [Alpine.JS](https://stackblitz.com/edit/vitejs-vite-kg36bvri?file=index.html) |
| 114 | + |
| 115 | +- [Lit.JS](https://stackblitz.com/edit/vitejs-vite-twfn9ilc?file=package.json) |
| 116 | + |
| 117 | +> Note: When integrating Ionic Core into projects using libraries such as Svelte, Alpine, or Lit, be aware that you won't have the built-in form and routing capabilities that are tightly coupled with Ionic's official Angular, React, and Vue framework integrations. You'll need to handle these aspects using the patterns and tools provided by your chosen library. |
0 commit comments