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

chore(edge): add global attributes to style/script tag #2

Merged
merged 1 commit into from
Jan 2, 2025
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.

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
Loading