Skip to content

Commit

Permalink
feat: add files detector
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed Mar 3, 2024
1 parent ed601a3 commit fedbd0a
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"crc-32": "^1.2.2",
"edge-error": "^4.0.1",
"html-entities": "^2.4.0",
"locate-path": "^7.2.0",
"qs": "^6.11.2"
},
"peerDependencies": {
Expand Down
10 changes: 7 additions & 3 deletions src/define_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,30 @@ import { configProvider } from '@adonisjs/core'
import type { ConfigProvider } from '@adonisjs/core/types'

import { VersionCache } from './version_cache.js'
import { FilesDetector } from './files_detector.js'
import type { InertiaConfig, ResolvedConfig } from './types.js'

/**
* Define the Inertia configuration
*/
export function defineConfig(config: InertiaConfig): ConfigProvider<ResolvedConfig> {
return configProvider.create(async (app) => {
const detector = new FilesDetector(app)
const versionCache = new VersionCache(app.appRoot, config.assetsVersion)
await versionCache.computeVersion()

return {
versionCache,
rootView: config.rootView ?? 'root',
sharedData: config.sharedData || {},
entrypoint: config.entrypoint ?? app.makePath('resources/app.ts'),
entrypoint: config.entrypoint ?? (await detector.detectEntrypoint('resources/app.ts')),
ssr: {
enabled: config.ssr?.enabled ?? false,
pages: config.ssr?.pages,
entrypoint: config.ssr?.entrypoint ?? app.makePath('resources/ssr.ts'),
bundle: config.ssr?.bundle ?? app.makePath('ssr/ssr.js'),
entrypoint:
config.ssr?.entrypoint ?? (await detector.detectSsrEntrypoint('resources/ssr.ts')),

bundle: config.ssr?.bundle ?? (await detector.detectSsrBundle('ssr/ssr.js')),
},
}
})
Expand Down
66 changes: 66 additions & 0 deletions src/files_detector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* @adonisjs/inertia
*
* (c) AdonisJS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import { locatePath } from 'locate-path'
import { Application } from '@adonisjs/core/app'

export class FilesDetector {
constructor(protected app: Application<any>) {}

/**
* Try to locate the entrypoint file based
* on the conventional locations
*/
async detectEntrypoint(defaultPath: string) {
const possiblesLocations = [
'./resources/app.ts',
'./resources/app.tsx',
'./resources/application/app.ts',
'./resources/application/app.tsx',
'./resources/app.jsx',
'./resources/app.js',
'./resources/application/app.jsx',
'./resources/application/app.js',
]

const path = await locatePath(possiblesLocations, { cwd: this.app.appRoot })
return this.app.makePath(path || defaultPath)
}

/**
* Try to locate the SSR entrypoint file based
* on the conventional locations
*/
async detectSsrEntrypoint(defaultPath: string) {
const possiblesLocations = [
'./resources/ssr.ts',
'./resources/ssr.tsx',
'./resources/application/ssr.ts',
'./resources/application/ssr.tsx',
'./resources/ssr.jsx',
'./resources/ssr.js',
'./resources/application/ssr.jsx',
'./resources/application/ssr.js',
]

const path = await locatePath(possiblesLocations, { cwd: this.app.appRoot })
return this.app.makePath(path || defaultPath)
}

/**
* Try to locate the SSR bundle file based
* on the conventional locations
*/
async detectSsrBundle(defaultPath: string) {
const possiblesLocations = ['./ssr/ssr.js', './ssr/ssr.mjs']

const path = await locatePath(possiblesLocations, { cwd: this.app.appRoot })
return this.app.makePath(path || defaultPath)
}
}
51 changes: 51 additions & 0 deletions tests/files_detector.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { join } from 'node:path'
import { test } from '@japa/runner'
import { AppFactory } from '@adonisjs/core/factories/app'

import { FilesDetector } from '../src/files_detector.js'

test.group('Files detector', () => {
test('detect entrypoint', async ({ assert, fs }) => {
const app = new AppFactory().create(fs.baseUrl, () => {})
const detector = new FilesDetector(app)

await fs.create('resources/app.ts', '')

const entrypoint = await detector.detectEntrypoint('resources/foo.ts')

assert.deepEqual(entrypoint, join(fs.basePath, 'resources/app.ts'))
})

test('detect tsx entrypoint', async ({ assert, fs }) => {
const app = new AppFactory().create(fs.baseUrl, () => {})
const detector = new FilesDetector(app)

await fs.create('resources/app.tsx', '')

const entrypoint = await detector.detectEntrypoint('resources/foo.ts')

assert.deepEqual(entrypoint, join(fs.basePath, 'resources/app.tsx'))
})

test('detect ssr entrypoint', async ({ assert, fs }) => {
const app = new AppFactory().create(fs.baseUrl, () => {})
const detector = new FilesDetector(app)

await fs.create('resources/ssr.ts', '')

const entrypoint = await detector.detectSsrEntrypoint('resources/foo.ts')

assert.deepEqual(entrypoint, join(fs.basePath, 'resources/ssr.ts'))
})

test('detect ssr bundle', async ({ assert, fs }) => {
const app = new AppFactory().create(fs.baseUrl, () => {})
const detector = new FilesDetector(app)

await fs.create('ssr/ssr.js', '')

const entrypoint = await detector.detectSsrBundle('ssr/foo.js')

assert.deepEqual(entrypoint, join(fs.basePath, 'ssr/ssr.js'))
})
})

0 comments on commit fedbd0a

Please sign in to comment.