Skip to content

Commit

Permalink
Merge pull request #2 from AthennaIO/develop
Browse files Browse the repository at this point in the history
chore(edge): add global attributes to style/script tag
  • Loading branch information
jlenon7 authored Jan 2, 2025
2 parents 52fb06b + 1965cd9 commit cbacca7
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 13 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.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/vite",
"version": "5.1.0",
"version": "5.2.0",
"description": "Vite plugin for Athenna Framework.",
"license": "MIT",
"author": "João Lenon <[email protected]>",
Expand Down
7 changes: 4 additions & 3 deletions src/fastify/FastifyVite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ export class FastifyVite {
* Don't start vite server under production.
*/
if (this.options.dev) {
debug('vite server creation bypassed because app is in production mode.')
return
} else {
const { createServer, loadConfigFromFile } = await import('vite')

const defaultConfig = {
Expand All @@ -90,6 +87,10 @@ export class FastifyVite {
await this.scope.register(fastifyMiddie, { hook: 'onRequest' })

this.scope.use(this.devServer.middlewares)

this.scope.addHook('onClose', () => this.devServer.close())
} else {
debug('vite server creation bypassed because app is in production mode.')
}

const vite = new Vite(this.options, this.devServer)
Expand Down
52 changes: 45 additions & 7 deletions src/vite/Vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import path from 'node:path'

import { File } from '@athenna/common'
import type { FastifyViteOptions } from '#src/types'
import type { FastifyViteOptions, SetAttributes } from '#src/types'
import type { Manifest, ModuleNode, ViteDevServer } from 'vite'

const styleFileRegex = /\.(css|less|sass|scss|styl|stylus|pcss|postcss)($|\?)/
Expand Down Expand Up @@ -148,23 +148,61 @@ export class Vite {
return false
}

/**
* Unwrap attributes from the user defined function or return
* the attributes as it is.
*/
public unwrapAttributes(
src: string,
url: string,
attributes?: SetAttributes
) {
if (typeof attributes === 'function') {
return attributes({ src, url })
}

return attributes
}

/**
* Create a style tag for the given path.
*/
public makeStyleTag(url: string, attributes?: any) {
public makeStyleTag(src: string, url: string, attributes?: any) {
const customAttributes = this.unwrapAttributes(
src,
url,
this.options?.styleAttributes
)

return this.generateElement({
tag: 'link',
attributes: { rel: 'stylesheet', href: url, ...attributes }
attributes: {
rel: 'stylesheet',
...customAttributes,
...attributes,
href: url
}
})
}

/**
* Create a script tag for the given path
*/
public makeScriptTag(url: string, attributes?: any) {
public makeScriptTag(src: string, url: string, attributes?: any) {
const customAttributes = this.unwrapAttributes(
src,
url,
this.options?.scriptAttributes
)

return this.generateElement({
tag: 'script',
attributes: { type: 'module', src: url, ...attributes },
attributes: {
type: 'module',
...customAttributes,
...attributes,
src: url
},
children: []
})
}
Expand All @@ -182,10 +220,10 @@ export class Vite {
}

if (this.isCssPath(asset)) {
return this.makeStyleTag(url, attributes)
return this.makeStyleTag(asset, url, attributes)
}

return this.makeScriptTag(url, attributes)
return this.makeScriptTag(asset, url, attributes)
}

/**
Expand Down

0 comments on commit cbacca7

Please sign in to comment.