Skip to content

Commit

Permalink
feat(ipx): support nuxt layers (#1177)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aareksio authored Jan 17, 2024
1 parent 05621b8 commit 42bbf73
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 11 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"eslint": "8.56.0",
"globby": "^14.0.0",
"happy-dom": "^13.1.4",
"ipx": "^2.0.2",
"ipx": "^2.1.0",
"jiti": "1.21.0",
"nuxt": "^3.9.2",
"playwright-core": "^1.41.0",
Expand All @@ -63,7 +63,7 @@
"vue-tsc": "^1.8.27"
},
"optionalDependencies": {
"ipx": "^2.0.2"
"ipx": "^2.1.0"
},
"packageManager": "[email protected]",
"resolutions": {
Expand Down
1 change: 1 addition & 0 deletions playground/layers/example/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default defineNuxtConfig({})
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions playground/layouts/default.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
<NuxtLink to="/events">
Events
</NuxtLink>
<NuxtLink to="/ipx-layers">
IPX Layers
</NuxtLink>
</li>
<li>
<ProviderSelector />
Expand Down
1 change: 1 addition & 0 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { defineNuxtConfig } from 'nuxt/config'

export default defineNuxtConfig({
modules: ['@nuxt/image'],
extends: ['./layers/example'],
image: {
inject: true,
domains: [
Expand Down
28 changes: 28 additions & 0 deletions playground/pages/ipx-layers.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<template>
<div>
<h1>Original</h1>
<nuxt-img
src="/images/colors.jpg"
width="500"
height="500"
@load="isLoadedRoot = true"
/>
<p>Received onLoad event: {{ isLoadedRoot }}</p>

<h1>Layer</h1>
<nuxt-img
src="/images/colors-layer.jpg"
width="500"
height="500"
@load="isLoadedLayer = true"
/>
<p>Received onLoad event: {{ isLoadedLayer }}</p>
</div>
</template>

<script setup lang="ts">
import { ref } from '#imports'
const isLoadedRoot = ref(false)
const isLoadedLayer = ref(false)
</script>
82 changes: 77 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions src/ipx.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { existsSync } from 'node:fs'
import { relative, resolve } from 'pathe'
import { useNuxt, createResolver, useNitro } from '@nuxt/kit'
import type { NitroEventHandler } from 'nitropack'
Expand Down Expand Up @@ -27,7 +28,13 @@ export const ipxSetup: IPXSetupT = setupOptions => (providerOptions, moduleOptio
}

// Options
const absoluteDir = resolve(nuxt.options.srcDir, moduleOptions.dir || nuxt.options.dir.public)
const publicDirs = nuxt.options._layers.map((layer) => {
const isRootLayer = layer.config.rootDir === nuxt.options.rootDir
const layerOptions = isRootLayer ? nuxt.options : layer.config
const path = isRootLayer ? moduleOptions.dir : layerOptions.dir?.public || 'public'

return resolve(layerOptions.srcDir, path)
}).filter(dir => existsSync(dir))
const relativeDir = relative(nitro.options.output.serverDir, nitro.options.output.publicDir)
const ipxOptions: IPXRuntimeConfig = {
...providerOptions.options,
Expand All @@ -37,7 +44,7 @@ export const ipxSetup: IPXSetupT = setupOptions => (providerOptions, moduleOptio
...providerOptions.options?.alias
},
fs: (providerOptions.options?.fs !== false) && {
dir: nuxt.options.dev ? absoluteDir : relativeDir,
dir: nuxt.options.dev ? publicDirs : relativeDir,
...providerOptions.options?.fs
},
http: (providerOptions.options?.http !== false) && {
Expand All @@ -61,7 +68,7 @@ export const ipxSetup: IPXSetupT = setupOptions => (providerOptions, moduleOptio

// Prerenderer
if (!nitro.options.dev) {
nitro.options._config.runtimeConfig.ipx = defu({ fs: { dir: absoluteDir } }, ipxOptions)
nitro.options._config.runtimeConfig.ipx = defu({ fs: { dir: publicDirs } }, ipxOptions)
nitro.options._config.handlers!.push(ipxHandler)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/ipx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default lazyEventHandler(() => {
const opts = useRuntimeConfig().ipx as NitroRuntimeConfig['ipx'] || {} as Record<string, never>

// TODO: Migrate to unstorage layer
const fsDir = opts.fs?.dir ? isAbsolute(opts.fs.dir) ? opts.fs.dir : fileURLToPath(new URL(opts.fs.dir, import.meta.url)) : undefined
const fsDir = opts?.fs?.dir ? (Array.isArray(opts.fs.dir) ? opts.fs.dir : [opts.fs.dir]).map(dir => isAbsolute(dir) ? dir : fileURLToPath(new URL(dir, import.meta.url))) : undefined

const fsStorage = opts.fs?.dir ? ipxFSStorage({ ...opts.fs, dir: fsDir }) : undefined
const httpStorage = opts.http?.domains ? ipxHttpStorage({ ...opts.http }) : undefined
Expand Down

0 comments on commit 42bbf73

Please sign in to comment.