Skip to content

Commit

Permalink
Close storage drivers when nuxt is closing (#65)
Browse files Browse the repository at this point in the history
Co-authored-by: Dave Stewart <dave@davestewart.co.uk>
  • Loading branch information
danielroe and davestewart committed Apr 9, 2024
1 parent d740c53 commit 00f3084
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 70 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nuxt-content-assets",
"version": "1.3.4",
"version": "1.3.5",
"description": "Enable locally-located assets in Nuxt Content",
"repository": "davestewart/nuxt-content-assets",
"license": "MIT",
Expand Down
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

0 comments on commit 00f3084

Please sign in to comment.