Skip to content

Commit

Permalink
Merge pull request #197 from AthennaIO/develop
Browse files Browse the repository at this point in the history
feat(vite): export athenna plugin
  • Loading branch information
jlenon7 authored Dec 31, 2024
2 parents e84ebf2 + 9a5cd15 commit fc75fc1
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 41 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/http",
"version": "5.10.0",
"version": "5.11.0",
"description": "The Athenna Http server. Built on top of fastify.",
"license": "MIT",
"author": "João Lenon <[email protected]>",
Expand Down Expand Up @@ -51,7 +51,7 @@
"./types": "./src/types/index.js",
"./package": "./package.json",
"./package.json": "./package.json",
"./vite": "./src/vite/index.js",
"./vite/plugin": "./src/vite/plugin.js",
"./testing/plugins": "./src/testing/plugins/index.js",
"./kernels/HttpKernel": "./src/kernels/HttpKernel.js",
"./handlers/HttpExceptionHandler": "./src/handlers/HttpExceptionHandler.js",
Expand Down
37 changes: 37 additions & 0 deletions src/types/vite/PluginOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @athenna/http
*
* (c) João Lenon <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

export interface PluginOptions {
/**
* The URL where the assets will be served. This is particularly
* useful if you are using a CDN to deploy your assets.
*
* @default ''
*/
assetsUrl?: string

/**
* Files that should trigger a page reload when changed.
*
* @default ['./src/resources/views/** /*.edge']
*/
reload?: string[]

/**
* Paths to the entrypoints files
*/
entrypoints: string[]

/**
* Public directory where the assets will be compiled.
*
* @default 'public/assets'
*/
buildDirectory?: string
}
77 changes: 77 additions & 0 deletions src/vite/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* @athenna/http
*
* (c) João Lenon <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import { join } from 'node:path'
import type { ConfigEnv, Plugin, UserConfig } from 'vite'
import type { PluginOptions } from '#src/types/vite/PluginOptions'

/**
* Resolve the `config.base` value
*/
export function resolveBase(
config: UserConfig,
options: Required<PluginOptions>,
command: 'build' | 'serve'
): string {
if (config.base) return config.base
if (command === 'build') {
return options.assetsUrl.endsWith('/')
? options.assetsUrl
: options.assetsUrl + '/'
}

return '/'
}

/**
* Vite config hook
*/
export function configHook(
options: Required<PluginOptions>,
userConfig: UserConfig,
{ command }: ConfigEnv
): UserConfig {
const config: UserConfig = {
publicDir: userConfig.publicDir ?? false,
base: resolveBase(userConfig, options, command),

/**
* Disable the vite dev server cors handling. Otherwise, it will
* override the cors settings defined by @fastify/cors.
*/
server: { cors: userConfig.server?.cors ?? false },

build: {
assetsDir: '',
emptyOutDir: true,
manifest: userConfig.build?.manifest ?? true,
outDir: userConfig.build?.outDir ?? options.buildDirectory,
assetsInlineLimit: userConfig.build?.assetsInlineLimit ?? 0,

rollupOptions: {
input: options.entrypoints.map(entrypoint =>
join(userConfig.root || '', entrypoint)
)
}
}
}

return config
}

/**
* Update the user vite config to match Athenna requirements.
*/
export const config = (options: Required<PluginOptions>): Plugin => {
return {
name: 'vite-plugin-athenna:config',
enforce: 'post',
config: configHook.bind(null, options)
}
}
37 changes: 0 additions & 37 deletions src/vite/index.ts

This file was deleted.

34 changes: 34 additions & 0 deletions src/vite/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @athenna/http
*
* (c) João Lenon <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import PluginRestart from 'vite-plugin-restart'

import { config } from '#src/vite/config'
import type { PluginOption } from 'vite'
import type { PluginOptions } from '#src/types/vite/PluginOptions'

export default function athenna(options: PluginOptions): PluginOption[] {
const fullOptions = Object.assign(
{
assetsUrl: '/assets',
buildDirectory: 'public/assets',
reload: ['./src/resources/views/**/*.edge'],
css: {
preprocessorOptions: {
scss: {
api: 'modern'
}
}
}
},
options
)

return [PluginRestart({ reload: fullOptions.reload }), config(fullOptions)]
}

0 comments on commit fc75fc1

Please sign in to comment.