-
-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathmodule.ts
More file actions
247 lines (220 loc) · 6.25 KB
/
module.ts
File metadata and controls
247 lines (220 loc) · 6.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import { existsSync, promises as fsp } from 'node:fs'
import {
defineNuxtModule,
addComponent,
addPlugin,
addTemplate,
addImportsSources,
} from '@nuxt/kit'
import { join, resolve } from 'pathe'
import { readPackageJSON } from 'pkg-types'
import { defineUnimportPreset } from 'unimport'
import type { AnimationBuilder, SpinnerTypes, PlatformConfig } from '@ionic/vue'
import { runtimeDir } from './utils'
import { IonicBuiltInComponents, IonicHooks } from './imports'
import { setupUtilityComponents } from './parts/components'
import { useCSSSetup } from './parts/css'
import { setupIonIcons } from './parts/icons/ionicons'
import { setupIonicIconsSvg } from './parts/icons/ionic-icons-svg'
import { setupMeta } from './parts/meta'
import { setupPWA } from './parts/pwa'
import { setupRouter } from './parts/router'
export interface ModuleOptionIconSvg {
enable?: boolean
directoryAsNamespace?: boolean
}
export interface ModuleOptionIcon {
ionicons?: boolean
svg?: ModuleOptionIconSvg
}
export interface ModuleOptions {
integrations?: {
router?: boolean
pwa?: boolean
meta?: boolean
icons?: ModuleOptionIcon
}
css?: {
core?: boolean
basic?: boolean
utilities?: boolean
}
config?: {
actionSheetEnter?: AnimationBuilder
actionSheetLeave?: AnimationBuilder
alertEnter?: AnimationBuilder
alertLeave?: AnimationBuilder
animated?: boolean
backButtonDefaultHref?: string
backButtonIcon?: string
backButtonText?: string
innerHTMLTemplatesEnabled?: boolean
hardwareBackButton?: boolean
infiniteLoadingSpinner?: SpinnerTypes
loadingEnter?: AnimationBuilder
loadingLeave?: AnimationBuilder
loadingSpinner?: SpinnerTypes
menuIcon?: string
menuType?: string
modalEnter?: AnimationBuilder
modalLeave?: AnimationBuilder
mode?: 'ios' | 'md'
navAnimation?: AnimationBuilder
pickerEnter?: AnimationBuilder
pickerLeave?: AnimationBuilder
platform?: PlatformConfig
popoverEnter?: AnimationBuilder
popoverLeave?: AnimationBuilder
refreshingIcon?: string
refreshingSpinner?: SpinnerTypes
sanitizerEnabled?: boolean
spinner?: SpinnerTypes
statusTap?: boolean
swipeBackEnabled?: boolean
tabButtonLayout?:
| 'icon-top'
| 'icon-start'
| 'icon-end'
| 'icon-bottom'
| 'icon-hide'
| 'label-hide'
toastDuration?: number
toastEnter?: AnimationBuilder
toastLeave?: AnimationBuilder
toggleOnOffLabels?: boolean
}
}
export default defineNuxtModule<ModuleOptions>({
meta: {
name: '@nuxtjs/ionic',
configKey: 'ionic',
compatibility: {
nuxt: '^3.0.0-rc.12',
},
},
defaults: {
integrations: {
meta: true,
pwa: true,
router: true,
icons: {
ionicons: true,
svg: {
enable: false,
directoryAsNamespace: true,
},
},
},
css: {
core: true,
basic: true,
utilities: false,
},
config: {},
},
async setup(options, nuxt) {
nuxt.options.build.transpile.push(runtimeDir)
nuxt.options.build.transpile.push(/@ionic/, /@stencil/)
// Inject options for the Ionic Vue plugin as a virtual module
addTemplate({
filename: 'ionic/vue-config.mjs',
getContents: () => `export default ${JSON.stringify(options.config)}`,
})
// Create an Ionic config file if it doesn't exist yet
const ionicConfigPath = join(nuxt.options.rootDir, 'ionic.config.json')
if (!existsSync(ionicConfigPath)) {
await fsp.writeFile(
ionicConfigPath,
JSON.stringify(
{
name: await readPackageJSON(nuxt.options.rootDir).then(
({ name }) => name || 'nuxt-ionic-project',
),
integrations: {},
type: 'vue',
},
null,
2,
),
)
}
// Set up Ionic Core
addPlugin(resolve(runtimeDir, 'ionic'))
// Add Nuxt Vue custom utility components
setupUtilityComponents()
// Add auto-imported components
IonicBuiltInComponents.map(name =>
addComponent({
name,
export: name,
filePath: '@ionic/vue',
}),
)
// Add auto-imported composables
addImportsSources(
defineUnimportPreset({
from: '@ionic/vue',
imports: [...IonicHooks],
}),
)
if (nuxt.options._generate) {
nuxt.hook('nitro:config', async (config) => {
config.prerender ||= {}
config.prerender.routes ||= []
config.prerender.routes.push('/200.html')
config.output ||= {}
if (!config.output.publicDir) {
const distDir = resolve(nuxt.options.rootDir, 'dist')
const stats = await fsp.lstat(distDir).catch(() => null)
if (!stats || !stats.isSymbolicLink()) {
config.output.publicDir = distDir
if (!existsSync(distDir)) {
await fsp.mkdir(distDir, { recursive: true })
}
}
}
})
// Ensure there is an index.html file present when doing static file generation
let publicFolder: string
nuxt.hook('nitro:init', (nitro) => {
publicFolder = nitro.options.output.publicDir
})
nuxt.hook('close', async () => {
const indexFile = join(publicFolder, 'index.html')
const fallbackFile = join(publicFolder, '200.html')
if (!existsSync(indexFile) && existsSync(fallbackFile)) {
await fsp.copyFile(fallbackFile, indexFile)
}
})
}
const { setupBasic, setupCore, setupUtilities } = useCSSSetup()
// Add Ionic Core CSS
if (options.css?.core) {
await setupCore()
}
if (options.css?.basic) {
await setupBasic()
}
if (options.css?.utilities) {
await setupUtilities()
}
// Add auto-imported icons
if (options.integrations?.icons?.ionicons) {
await setupIonIcons()
}
// Add auto-imported custom icons
if (options.integrations?.icons?.svg?.enable) {
setupIonicIconsSvg(options.integrations?.icons?.svg)
}
if (options.integrations?.meta) {
await setupMeta()
}
if (options.integrations?.pwa) {
await setupPWA()
}
// Set up Ionic Router integration
if (options.integrations?.router) {
await setupRouter()
}
},
})