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

Close storage drivers when nuxt is closing #65

Merged
merged 4 commits into from
Apr 9, 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
11 changes: 10 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ export default defineNuxtModule<ModuleOptions>({
/**
* Assets manager
*/
const assets = makeAssetsManager(publicPath)
const assets = makeAssetsManager(publicPath, nuxt.options.dev)

nuxt.hooks.hook('close', () => assets.dispose())

/**
* Callback for when assets change
Expand Down Expand Up @@ -185,6 +187,13 @@ export default defineNuxtModule<ModuleOptions>({
managers[key] = makeSourceManager(key, source, publicPath, onAssetChange)
}

nuxt.hook('close', async () => {
for (const key in managers) {
await managers[key].storage.unwatch()
await managers[key].storage.dispose()
}
})

// ---------------------------------------------------------------------------------------------------------------------
// build hook
// ---------------------------------------------------------------------------------------------------------------------
Expand Down
18 changes: 12 additions & 6 deletions src/runtime/assets/public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { isImage, warn, log } from '../utils'
/**
* Manages the public assets
*/
export function makeAssetsManager (publicPath: string) {
export function makeAssetsManager (publicPath: string, shouldWatch = true) {

// ---------------------------------------------------------------------------------------------------------------------
// storage - updates asset index, watches for changes from other processes
Expand All @@ -20,11 +20,13 @@ export function makeAssetsManager (publicPath: string) {

// storage
const storage = makeSourceStorage(Path.join(publicPath, '..'))
void storage.watch(async (event: string, key: string) => {
if (event === 'update' && key === indexKey) {
await load()
}
})
if (shouldWatch) {
void storage.watch(async (event: string, key: string) => {
if (event === 'update' && key === indexKey) {
await load()
}
})
}

// assets
const assets: Record<string, AssetConfig> = {}
Expand Down Expand Up @@ -130,6 +132,10 @@ export function makeAssetsManager (publicPath: string) {
getAsset,
removeAsset,
resolveAsset,
dispose: async () => {
await storage.unwatch()
await storage.dispose()
}
}
}

Expand Down
128 changes: 66 additions & 62 deletions src/runtime/content/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,83 +5,85 @@ import { makeAssetsManager } from '../assets/public'
// @ts-ignore – options injected via module.ts
import { debug, imageSizes, publicPath } from '#nuxt-content-assets'

/**
* Walk the parsed frontmatter and check properties as paths
*/
function processMeta (content: ParsedContent, imageSizes: ImageSize = [], updated: string[] = []) {
walkMeta(content, (value, parent, key) => {
if (isValidAsset(value)) {
const { srcAttr, width, height } = resolveAsset(content, removeQuery(value), true)
if (srcAttr) {
const query = width && height && (imageSizes.includes('src') || imageSizes.includes('url'))
? `width=${width}&height=${height}`
: ''
const srcUrl = query
? buildQuery(srcAttr, parseQuery(value), query)
: srcAttr
parent[key] = srcUrl
updated.push(`meta: ${key} to "${srcUrl}"`)
const plugin: NitroAppPlugin = async (nitro: NitroApp) => {
/**
* Walk the parsed frontmatter and check properties as paths
*/
function processMeta (content: ParsedContent, imageSizes: ImageSize = [], updated: string[] = []) {
walkMeta(content, (value, parent, key) => {
if (isValidAsset(value)) {
const { srcAttr, width, height } = resolveAsset(content, removeQuery(value), true)
if (srcAttr) {
const query = width && height && (imageSizes.includes('src') || imageSizes.includes('url'))
? `width=${width}&height=${height}`
: ''
const srcUrl = query
? buildQuery(srcAttr, parseQuery(value), query)
: srcAttr
parent[key] = srcUrl
updated.push(`meta: ${key} to "${srcUrl}"`)
}
}
}
})
}
})
}

/**
* Walk the parsed content and check potential attributes as paths
*/
function processBody (content: ParsedContent, imageSizes: ImageSize = [], updated: string[] = []) {
walkBody(content, function (node: any) {
const { tag, props } = node
for (const [prop, value] of Object.entries(props)) {
// only process strings
if (typeof value !== 'string') {
return
}
/**
* Walk the parsed content and check potential attributes as paths
*/
function processBody (content: ParsedContent, imageSizes: ImageSize = [], updated: string[] = []) {
walkBody(content, function (node: any) {
const { tag, props } = node
for (const [prop, value] of Object.entries(props)) {
// only process strings
if (typeof value !== 'string') {
return
}

// parse value
const { srcAttr, width, height } = resolveAsset(content, value, true)
// parse value
const { srcAttr, width, height } = resolveAsset(content, value, true)

// if we resolved an asset
if (srcAttr) {
// assign src
node.props[prop] = srcAttr
// if we resolved an asset
if (srcAttr) {
// assign src
node.props[prop] = srcAttr

// assign size
if (node.tag === 'img' || node.tag === 'nuxt-img') {
if (width && height) {
if (imageSizes.includes('attrs')) {
node.props.width = width
node.props.height = height
}
if (imageSizes.includes('style')) {
const ratio = `${width}/${height}`
if (typeof node.props.style === 'string') {
node.props.style = buildStyle(node.props.style, `aspect-ratio: ${ratio}`)
// assign size
if (node.tag === 'img' || node.tag === 'nuxt-img') {
if (width && height) {
if (imageSizes.includes('attrs')) {
node.props.width = width
node.props.height = height
}
else {
node.props.style ||= {}
node.props.style.aspectRatio = ratio
if (imageSizes.includes('style')) {
const ratio = `${width}/${height}`
if (typeof node.props.style === 'string') {
node.props.style = buildStyle(node.props.style, `aspect-ratio: ${ratio}`)
}
else {
node.props.style ||= {}
node.props.style.aspectRatio = ratio
}
}
}
}
}

// open links in new window
else if (node.tag === 'a') {
node.props.target ||= '_blank'
}
// open links in new window
else if (node.tag === 'a') {
node.props.target ||= '_blank'
}

// debug
updated.push(`page: ${tag}[${prop}] to "${srcAttr}"`)
// debug
updated.push(`page: ${tag}[${prop}] to "${srcAttr}"`)
}
}
}
})
}
})
}

const { resolveAsset } = makeAssetsManager(publicPath)

const { resolveAsset, dispose } = makeAssetsManager(publicPath, import.meta.dev)

const plugin: NitroAppPlugin = async (nitro: NitroApp) => {
// @ts-ignore
// @ts-ignore hook name
nitro.hooks.hook('content:file:afterParse', function (content: ParsedContent) {
if (content._extension === 'md') {
const updated: string[] = []
Expand All @@ -93,6 +95,8 @@ const plugin: NitroAppPlugin = async (nitro: NitroApp) => {
}
}
})

nitro.hooks.hook('close', dispose)
}

export default plugin