Skip to content

feat: integrate with capacitor config #770

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ coverage
*.iml
.idea

# Zed
.zed

# OSX
.DS_Store
.AppleDouble
Expand Down
2 changes: 1 addition & 1 deletion lint-staged.config.cjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
'*.{js,ts,mjs,cjs,json}': ['pnpm lint:eslint'],
'*.{js,ts,mjs,cjs,json}': ['pnpm lint'],
}
1 change: 0 additions & 1 deletion playground/capacitor.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const config: CapacitorConfig = {
appId: 'io.ionic.starter',
appName: 'nuxt-ionic-playground',
webDir: 'dist',
bundledWebRuntime: false,
}

export default config
13 changes: 13 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { useCSSSetup } from './parts/css'
import { setupIcons } from './parts/icons'
import { setupMeta } from './parts/meta'
import { setupRouter } from './parts/router'
import { useCapacitor } from './parts/capacitor'

export interface ModuleOptions {
integrations?: {
Expand Down Expand Up @@ -138,6 +139,18 @@ export default defineNuxtModule<ModuleOptions>({
nuxt.options.typescript.hoist ||= []
nuxt.options.typescript.hoist.push('@ionic/vue')

// add capacitor integration
const { excludeNativeFolders, findCapacitorConfig, parseCapacitorConfig }
= useCapacitor()

// add the `android` and `ios` folders to the TypeScript config exclude list if capacitor is enabled
// this is to prevent TypeScript from trying to resolve the Capacitor native code
const capacitorConfigPath = await findCapacitorConfig()
if (capacitorConfigPath) {
const { androidPath, iosPath } = await parseCapacitorConfig(capacitorConfigPath)
excludeNativeFolders(androidPath, iosPath)
}

// Add auto-imported components
IonicBuiltInComponents.map(name =>
addComponent({
Expand Down
56 changes: 56 additions & 0 deletions src/parts/capacitor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import type { CapacitorConfig } from '@capacitor/cli'
import { findPath, useNuxt } from '@nuxt/kit'
import { join } from 'pathe'

export const useCapacitor = () => {
const nuxt = useNuxt()

Check warning on line 6 in src/parts/capacitor.ts

View check run for this annotation

Codecov / codecov/patch

src/parts/capacitor.ts#L5-L6

Added lines #L5 - L6 were not covered by tests

/** Find the path to capacitor configuration file (if it exists) */
const findCapacitorConfig = async () => {

Check warning on line 9 in src/parts/capacitor.ts

View check run for this annotation

Codecov / codecov/patch

src/parts/capacitor.ts#L9

Added line #L9 was not covered by tests
const path = await findPath(
['capacitor.config.ts', 'capacitor.config.json'],
{

Check warning on line 12 in src/parts/capacitor.ts

View check run for this annotation

Codecov / codecov/patch

src/parts/capacitor.ts#L12

Added line #L12 was not covered by tests
extensions: ['ts', 'json'],
virtual: false,

Check warning on line 14 in src/parts/capacitor.ts

View check run for this annotation

Codecov / codecov/patch

src/parts/capacitor.ts#L14

Added line #L14 was not covered by tests
},
'file',
)

return path
}

const parseCapacitorConfig = async (path: string | null): Promise<{
androidPath: string | null
iosPath: string | null
}> => {
if (!path) {
return {
androidPath: null,
iosPath: null,
}
}

const capacitorConfig = (await import(path)) as CapacitorConfig

Check warning on line 33 in src/parts/capacitor.ts

View check run for this annotation

Codecov / codecov/patch

src/parts/capacitor.ts#L33

Added line #L33 was not covered by tests

return {

Check warning on line 35 in src/parts/capacitor.ts

View check run for this annotation

Codecov / codecov/patch

src/parts/capacitor.ts#L35

Added line #L35 was not covered by tests
androidPath: capacitorConfig.android?.path || null,
iosPath: capacitorConfig.ios?.path || null,
}
}

/** Exclude native folder paths from type checking by excluding them in tsconfig */
const excludeNativeFolders = (androidPath: string | null, iosPath: string | null) => {
nuxt.options.typescript.tsConfig ||= {}
nuxt.options.typescript.tsConfig.exclude ||= []
nuxt.options.typescript.tsConfig.exclude.push(
join('../', androidPath ?? '/android'),
join('../', iosPath ?? '/ios'),
)
}

return {
excludeNativeFolders,
findCapacitorConfig,
parseCapacitorConfig,
}
}
131 changes: 131 additions & 0 deletions test/unit/capacitor.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { useNuxt, findPath } from '@nuxt/kit'
import { useCapacitor } from '../../src/parts/capacitor'

// Mock @nuxt/kit
vi.mock('@nuxt/kit', () => ({
findPath: vi.fn(),
useNuxt: vi.fn(),
}))

describe('useCapacitor', () => {
const mockNuxt = {
options: {
typescript: {
tsConfig: {
exclude: [],
},
},
},
}

beforeEach(() => {
vi.clearAllMocks()
vi.mocked(useNuxt).mockReturnValue(mockNuxt as any)
mockNuxt.options.typescript.tsConfig.exclude = []
})

describe('findCapacitorConfig', () => {
it('should find capacitor.config.ts', async () => {
const mockPath = '/project/capacitor.config.ts'
vi.mocked(findPath).mockResolvedValue(mockPath)

const { findCapacitorConfig } = useCapacitor()
const result = await findCapacitorConfig()

expect(result).toBe(mockPath)
})

it('should return null when no config found', async () => {
vi.mocked(findPath).mockResolvedValue(null)

const { findCapacitorConfig } = useCapacitor()
const result = await findCapacitorConfig()

expect(result).toBeNull()
})
})

describe('parseCapacitorConfig', () => {
it('should return null paths when no config path provided', async () => {
const { parseCapacitorConfig } = useCapacitor()
const result = await parseCapacitorConfig(null)

expect(result).toEqual({
androidPath: null,
iosPath: null,
})
})

it('should parse capacitor config with custom paths', async () => {
const configPath = './capacitor.config.ts'
const mockConfig = {
android: { path: 'custom-android' },
ios: { path: 'custom-ios' },
}

vi.doMock(configPath, () => ({
default: mockConfig,
...mockConfig,
}))

const { parseCapacitorConfig } = useCapacitor()
const result = await parseCapacitorConfig(configPath)

expect(result).toEqual({
androidPath: 'custom-android',
iosPath: 'custom-ios',
})
})

it('should handle config without android/ios paths', async () => {
const configPath = './capacitor.config.ts'
const mockConfig = {
android: undefined,
ios: undefined,
}

vi.doMock(configPath, () => ({
default: mockConfig,
...mockConfig,
}))

const { parseCapacitorConfig } = useCapacitor()
const result = await parseCapacitorConfig(configPath)

expect(result).toEqual({
androidPath: null,
iosPath: null,
})
})
})

describe('excludeNativeFolders', () => {
it('should add native folders to typescript exclude', () => {
const { excludeNativeFolders } = useCapacitor()
excludeNativeFolders('android', 'ios')

expect(mockNuxt.options.typescript.tsConfig.exclude).toContain('../android')
expect(mockNuxt.options.typescript.tsConfig.exclude).toContain('../ios')
})

it('should handle null paths with defaults', () => {
const { excludeNativeFolders } = useCapacitor()
excludeNativeFolders(null, null)

expect(mockNuxt.options.typescript.tsConfig.exclude).toContain('../android')
expect(mockNuxt.options.typescript.tsConfig.exclude).toContain('../ios')
})

it('should initialize tsConfig if not present', () => {
// @ts-expect-error should not be undefined
mockNuxt.options.typescript.tsConfig = undefined

const { excludeNativeFolders } = useCapacitor()
excludeNativeFolders('android', 'ios')

expect(mockNuxt.options.typescript.tsConfig).toBeDefined()
expect(mockNuxt.options.typescript.tsConfig.exclude).toContain('../android')
})
})
})