Skip to content

Commit 9bc2371

Browse files
committed
docs(javascript): update the quickstart to fix npm run preview
1 parent c224da9 commit 9bc2371

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

docs/javascript/quickstart.md

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ npm install vite-plugin-static-copy --save-dev
8787

8888
Add a `vite.config.js` file at the project root with the following:
8989

90-
```js
90+
```js title="vite.config.js"
9191
import { defineConfig } from 'vite';
9292
import { viteStaticCopy } from 'vite-plugin-static-copy';
9393

@@ -122,18 +122,14 @@ This copies the necessary Ionic files that Capacitor will need to work with lazy
122122

123123
Replace the contents of `src/main.js` with the following:
124124

125-
```js
126-
// Determine if the app is running in Capacitor
127-
const isCapacitor = location.protocol === 'capacitor:' || (window.Capacitor && window.Capacitor.platform !== 'web');
128-
125+
```js title="src/main.js"
129126
// Load Ionic
130-
if (isCapacitor) {
131-
// In Capacitor, import Ionic directly from copied dist files
132-
import(/* @vite-ignore */ location.origin + '/ionic.esm.js');
133-
} else {
134-
// In the browser, use the normal loader
135-
import('@ionic/core/loader').then((m) => m.defineCustomElements(window));
136-
}
127+
(async () => {
128+
// Set the path to a variable to
129+
// prevent Vite from analyzing in dev
130+
const ionicPath = '/ionic.esm.js';
131+
await import(/* @vite-ignore */ ionicPath);
132+
})();
137133

138134
// Core CSS required for Ionic components to work properly
139135
import '@ionic/core/css/core.css';
@@ -158,7 +154,7 @@ This initializes Ionic based on the environment and then imports all of the avai
158154

159155
Update your `index.html` to configure the metadata and use Ionic components:
160156

161-
```html
157+
```html title="index.html"
162158
<!DOCTYPE html>
163159
<html lang="en" dir="ltr">
164160
<head>
@@ -190,7 +186,7 @@ This sets up the root of your application, using Ionic's `ion-app`, `ion-router`
190186

191187
Routes are defined in the `index.html` using `ion-route` components inside the `ion-router`:
192188

193-
```html
189+
```html title="index.html"
194190
<ion-router>
195191
<ion-route url="/" component="home-page"></ion-route>
196192
<ion-route url="/new" component="new-page"></ion-route>
@@ -203,7 +199,7 @@ When you visit the root URL (`/`), the `home-page` component will be loaded. Whe
203199

204200
Create a new directory called `pages` inside the `src` folder, then add a file called `HomePage.js` in that directory with the following content:
205201

206-
```js
202+
```js title="src/pages/HomePage.js"
207203
class HomePage extends HTMLElement {
208204
connectedCallback() {
209205
this.innerHTML = `
@@ -236,7 +232,7 @@ For detailed information about Ionic layout components, see the [Header](/docs/a
236232

237233
Next, add a `<script>` tag before the `src/main.js` import in `index.html` to import the Home page:
238234

239-
```html
235+
```html title="index.html"
240236
<script type="module">
241237
import './src/pages/HomePage.js';
242238
</script>
@@ -252,7 +248,7 @@ At this point your browser should be displaying the Home page.
252248

253249
You can enhance your Home page with more Ionic UI components. For example, add a [Button](/docs/api/button) to navigate to another page. Update the `HomePage` component in `src/pages/HomePage.js`:
254250

255-
```js
251+
```js title="src/pages/HomePage.js"
256252
class HomePage extends HTMLElement {
257253
connectedCallback() {
258254
this.innerHTML = `
@@ -283,7 +279,7 @@ customElements.define('home-page', HomePage);
283279

284280
Add a new file named `NewPage.js` to `src/pages` with the following content:
285281

286-
```js
282+
```js title="src/pages/NewPage.js"
287283
class NewPage extends HTMLElement {
288284
connectedCallback() {
289285
this.innerHTML = `
@@ -309,7 +305,7 @@ This creates a page with a [Back Button](/docs/api/back-button) in the [Toolbar]
309305

310306
Next, update the `<script>` tag which imports the Home page in the `index.html` file to also import the New page:
311307

312-
```html
308+
```html title="index.html"
313309
<script type="module">
314310
import './src/pages/HomePage.js';
315311
import './src/pages/NewPage.js';
@@ -320,7 +316,7 @@ Next, update the `<script>` tag which imports the Home page in the `index.html`
320316

321317
To navigate to the new page, update the button in `src/pages/HomePage.js` to be inside of an `ion-router-link`:
322318

323-
```html
319+
```html title="src/pages/HomePage.js"
324320
<ion-router-link href="/new">
325321
<ion-button>Navigate</ion-button>
326322
</ion-router-link>
@@ -338,7 +334,7 @@ Ionic JavaScript comes with [Ionicons](https://ionic.io/ionicons/) support. To u
338334

339335
Add the necessary imports and register the icons in `src/main.js`:
340336

341-
```js
337+
```js title="src/main.js"
342338
// ...Ionic initialization
343339

344340
// Icon imports
@@ -352,7 +348,7 @@ addIcons({ heart, logoIonic });
352348

353349
Next, update `src/pages/NewPage.js` to include the icons:
354350

355-
```js
351+
```js title="src/pages/NewPage.js"
356352
class NewPage extends HTMLElement {
357353
connectedCallback() {
358354
this.innerHTML = `
@@ -383,7 +379,7 @@ For more information, see the [Icon documentation](/docs/api/icon) and the [Ioni
383379

384380
Let's add a button that can scroll the content area to the bottom. Update `src/pages/NewPage.js` to include scrollable content and a scroll button:
385381

386-
```js
382+
```js title="src/pages/NewPage.js"
387383
class NewPage extends HTMLElement {
388384
connectedCallback() {
389385
this.innerHTML = `

0 commit comments

Comments
 (0)