1
1
import { existsSync , promises as fsp } from 'node:fs'
2
- import { fileURLToPath } from 'node:url'
2
+
3
+ import { defineNuxtModule , addComponent , addPlugin } from '@nuxt/kit'
3
4
import { join , resolve } from 'pathe'
4
5
import { readPackageJSON } from 'pkg-types'
5
6
import { defineUnimportPreset } from 'unimport'
6
- import {
7
- defineNuxtModule ,
8
- addComponent ,
9
- addPlugin ,
10
- installModule ,
11
- } from '@nuxt/kit'
12
- import { joinURL } from 'ufo'
13
- import type { NuxtPage } from '@nuxt/schema'
14
- import { provider } from 'std-env'
7
+
8
+ import { runtimeDir } from './utils'
9
+
10
+ import { useCSSSetup } from './parts/css'
11
+ import { setupMeta } from './parts/meta'
12
+ import { setupPWA } from './parts/pwa'
13
+ import { setupRouter } from './parts/router'
15
14
16
15
export interface ModuleOptions {
17
16
integrations ?: {
@@ -44,7 +43,6 @@ export default defineNuxtModule<ModuleOptions>({
44
43
} ,
45
44
} ,
46
45
async setup ( options , nuxt ) {
47
- const runtimeDir = fileURLToPath ( new URL ( './runtime' , import . meta. url ) )
48
46
nuxt . options . build . transpile . push ( runtimeDir )
49
47
nuxt . options . build . transpile . push ( / @ i o n i c / )
50
48
@@ -70,33 +68,6 @@ export default defineNuxtModule<ModuleOptions>({
70
68
// Set up Ionic Core
71
69
addPlugin ( resolve ( runtimeDir , 'ionic' ) )
72
70
73
- // Add Ionic Core CSS
74
- if ( options . css ?. core ) {
75
- // Core CSS required for Ionic components to work properly
76
- nuxt . options . css . push ( '@ionic/vue/css/core.css' )
77
- }
78
-
79
- if ( options . css ?. basic ) {
80
- // Basic CSS for apps built with Ionic
81
- nuxt . options . css . push (
82
- '@ionic/vue/css/normalize.css' ,
83
- '@ionic/vue/css/structure.css' ,
84
- '@ionic/vue/css/typography.css'
85
- )
86
- }
87
-
88
- if ( options . css ?. utilities ) {
89
- // Optional CSS utils that can be commented out
90
- nuxt . options . css . push (
91
- '@ionic/vue/css/padding.css' ,
92
- '@ionic/vue/css/float-elements.css' ,
93
- '@ionic/vue/css/text-alignment.css' ,
94
- '@ionic/vue/css/text-transformation.css' ,
95
- '@ionic/vue/css/flex-utils.css' ,
96
- '@ionic/vue/css/display.css'
97
- )
98
- }
99
-
100
71
// Add auto-imported components
101
72
IonicBuiltInComponents . map ( name =>
102
73
addComponent ( {
@@ -116,108 +87,32 @@ export default defineNuxtModule<ModuleOptions>({
116
87
)
117
88
} )
118
89
90
+ const { setupBasic, setupCore, setupUtilities } = useCSSSetup ( )
91
+
92
+ // Add Ionic Core CSS
93
+ if ( options . css ?. core ) {
94
+ await setupCore ( )
95
+ }
96
+
97
+ if ( options . css ?. basic ) {
98
+ await setupBasic ( )
99
+ }
100
+
101
+ if ( options . css ?. utilities ) {
102
+ await setupUtilities ( )
103
+ }
104
+
119
105
if ( options . integrations ?. meta ) {
120
- nuxt . options . app . head . meta = nuxt . options . app . head . meta || [ ]
121
- for ( const meta of metaDefaults ) {
122
- if ( ! nuxt . options . app . head . meta . some ( i => i . name === meta . name ) ) {
123
- nuxt . options . app . head . meta . unshift ( meta )
124
- }
125
- }
126
- nuxt . options . app . head . viewport =
127
- 'viewport-fit: cover, width: device-width, initial-scale: 1.0, minimum-scale: 1.0, maximum-scale: 1.0, user-scalable: no'
106
+ await setupMeta ( )
128
107
}
129
108
130
109
if ( options . integrations ?. pwa ) {
131
- if ( provider === 'stackblitz' ) {
132
- ; ( nuxt . options . pwa as any ) = ( nuxt . options . pwa as any ) || { }
133
- ; ( nuxt . options . pwa as any ) . icon = false
134
- console . warn (
135
- 'Disabling PWA icon generation as `sharp` is not currently supported on StackBlitz.'
136
- )
137
- }
138
- await installModule ( '@kevinmarrec/nuxt-pwa' )
110
+ await setupPWA ( )
139
111
}
140
112
141
113
// Set up Ionic Router integration
142
114
if ( options . integrations ?. router ) {
143
- const pagesDirs = nuxt . options . _layers . map ( layer =>
144
- resolve ( layer . config . srcDir , layer . config . dir ?. pages || 'pages' )
145
- )
146
-
147
- // Disable module (and use universal router) if pages dir do not exists or user has disabled it
148
- if (
149
- nuxt . options . pages === false ||
150
- ( nuxt . options . pages !== true && ! pagesDirs . some ( dir => existsSync ( dir ) ) )
151
- ) {
152
- console . warn (
153
- 'Disabling Ionic Router integration as pages dir does not exist.'
154
- )
155
- return
156
- }
157
-
158
- addPlugin ( resolve ( runtimeDir , 'router' ) )
159
- nuxt . options . vite . optimizeDeps = nuxt . options . vite . optimizeDeps || { }
160
- nuxt . options . vite . optimizeDeps . include =
161
- nuxt . options . vite . optimizeDeps . include || [ ]
162
- nuxt . options . vite . optimizeDeps . include . push ( '@ionic/vue-router' )
163
-
164
- nuxt . hook ( 'modules:done' , ( ) => {
165
- nuxt . options . plugins = nuxt . options . plugins . filter (
166
- p =>
167
- ! ( typeof p === 'string' ? p : p . src ) . endsWith (
168
- 'nuxt/dist/pages/runtime/router'
169
- ) &&
170
- ! ( typeof p === 'string' ? p : p . src ) . endsWith (
171
- 'nuxt/dist/app/plugins/router'
172
- )
173
- )
174
-
175
- // Add all pages to be prerendered
176
- const routes : string [ ] = [ ]
177
-
178
- nuxt . hook ( 'pages:extend' , pages => {
179
- routes . length = 0
180
- routes . push (
181
- '/' ,
182
- ...( ( nuxt . options . nitro . prerender ?. routes || [ ] ) as string [ ] )
183
- )
184
- function processPages ( pages : NuxtPage [ ] , currentPath = '' ) {
185
- for ( const page of pages ) {
186
- if ( page . path . includes ( ':' ) ) continue
187
-
188
- const path = joinURL ( currentPath , page . path )
189
- routes . push ( path )
190
- if ( page . children ) processPages ( page . children , path )
191
- }
192
- }
193
- processPages ( pages )
194
- } )
195
-
196
- nuxt . hook ( 'nitro:build:before' , nitro => {
197
- nitro . options . prerender . routes = routes
198
- } )
199
- } )
200
-
201
- // Remove vue-router types
202
- nuxt . hook ( 'prepare:types' , ( { references } ) => {
203
- const index = references . findIndex (
204
- i => 'types' in i && i . types === 'vue-router'
205
- )
206
- if ( index !== - 1 ) {
207
- references . splice ( index , 1 )
208
- }
209
- } )
210
-
211
- // Add default ionic root layout
212
- nuxt . hook ( 'app:resolve' , app => {
213
- if (
214
- ! app . mainComponent ||
215
- app . mainComponent . includes ( '@nuxt/ui-templates' ) ||
216
- app . mainComponent . match ( / n u x t 3 ? \/ d i s t / )
217
- ) {
218
- app . mainComponent = join ( runtimeDir , 'app.vue' )
219
- }
220
- } )
115
+ await setupRouter ( )
221
116
}
222
117
} ,
223
118
} )
@@ -328,13 +223,3 @@ const IonicBuiltInComponents = [
328
223
'IonModal' ,
329
224
'IonPopover' ,
330
225
]
331
-
332
- const metaDefaults = [
333
- { name : 'color-scheme' , content : 'light dark' } ,
334
- { name : 'format-detection' , content : 'telephone: no' } ,
335
- { name : 'msapplication-tap-highlight' , content : 'no' } ,
336
- // add to homescreen for ios
337
- { name : 'apple-mobile-web-app-capable' , content : 'yes' } ,
338
- { name : 'apple-mobile-web-app-title' , content : 'Ionic App' } ,
339
- { name : 'apple-mobile-web-app-status-bar-style' , content : 'black' } ,
340
- ]
0 commit comments