generated from AthennaIO/Template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #197 from AthennaIO/develop
feat(vite): export athenna plugin
- Loading branch information
Showing
6 changed files
with
152 additions
and
41 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]>", | ||
|
@@ -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", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)] | ||
} |