Skip to content
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

feat(vite): export athenna plugin #197

Merged
merged 1 commit into from
Dec 31, 2024
Merged
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
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 <lenon@athenna.io>",
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 <lenon@athenna.io>
*
* 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 <lenon@athenna.io>
*
* 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 <lenon@athenna.io>
*
* 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)]
}
Loading