nuxt-site-config 0.0.3 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ import { S as SiteConfigInput, a as SiteConfig } from './type-8a5f4ec7.js';
2
+ export { b as MaybeComputedRef, c as MaybeComputedRefEntries, M as MaybeReadonlyRef } from './type-8a5f4ec7.js';
3
+ import 'vue';
4
+
5
+ declare function useSiteConfig(overrides?: SiteConfigInput): Promise<SiteConfig>;
6
+
7
+ export { SiteConfig, SiteConfigInput, useSiteConfig };
package/dist/build.mjs ADDED
@@ -0,0 +1,102 @@
1
+ import { tryUseNuxt } from '@nuxt/kit';
2
+ import { readPackageJSON } from 'pkg-types';
3
+ import { withHttp, withHttps } from 'ufo';
4
+ import defu from 'defu';
5
+
6
+ let _siteConfigOverrides = [];
7
+ const processShim = typeof process !== "undefined" ? process : {};
8
+ const envShim = processShim.env || {};
9
+ const SITE_CONFIG_ENV = {
10
+ name: envShim.NUXT_PUBLIC_SITE_NAME,
11
+ url: envShim.NUXT_PUBLIC_SITE_URL,
12
+ description: envShim.NUXT_PUBLIC_SITE_DESCRIPTION,
13
+ image: envShim.NUXT_PUBLIC_SITE_IMAGE,
14
+ index: envShim.NUXT_PUBLIC_SITE_INDEX,
15
+ titleSeparator: envShim.NUXT_PUBLIC_SITE_TITLE_SEPARATOR,
16
+ trailingSlash: envShim.NUXT_PUBLIC_SITE_TRAILING_SLASH,
17
+ language: envShim.NUXT_PUBLIC_SITE_LANGUAGE
18
+ };
19
+ const NITRO_ENV_URL = [
20
+ envShim.NUXT_PUBLIC_VERCEL_URL,
21
+ // vercel
22
+ envShim.NUXT_PUBLIC_URL,
23
+ // netlify
24
+ envShim.NUXT_PUBLIC_CF_PAGES_URL
25
+ // cloudflare pages
26
+ ];
27
+ let overrideCount = 0;
28
+ function stackSiteConfigOverrides(overrides = {}) {
29
+ const id = overrideCount++;
30
+ _siteConfigOverrides.push({
31
+ id,
32
+ ...overrides
33
+ });
34
+ return () => {
35
+ _siteConfigOverrides = _siteConfigOverrides.filter((o) => o.id !== id);
36
+ };
37
+ }
38
+ function createSiteConfigContainer() {
39
+ const cleanUps = [];
40
+ function setOverrides(overrides) {
41
+ if (Object.keys(overrides || {}).length > 0)
42
+ cleanUps.push(stackSiteConfigOverrides(overrides));
43
+ }
44
+ function compute(input) {
45
+ const nitroEnvConfig = {};
46
+ const nitroUrl = NITRO_ENV_URL.find((k) => Boolean(k));
47
+ if (nitroUrl)
48
+ nitroEnvConfig.url = nitroUrl;
49
+ for (const k of Object.keys(SITE_CONFIG_ENV)) {
50
+ if (SITE_CONFIG_ENV[k])
51
+ nitroEnvConfig[k] = SITE_CONFIG_ENV[k];
52
+ }
53
+ const finalOverrides = {};
54
+ for (const o of _siteConfigOverrides)
55
+ Object.assign(finalOverrides, o);
56
+ const config = defu(finalOverrides, nitroEnvConfig, input.runtimeConfig || {}, input.contextConfig || {});
57
+ if (typeof config.index !== "undefined")
58
+ config.index = String(config.index) !== "false";
59
+ else
60
+ config.index = envShim.PROD;
61
+ if (typeof config.trailingSlash !== "undefined")
62
+ config.trailingSlash = String(config.trailingSlash) !== "false";
63
+ if (config.url && !config.url.startsWith("http"))
64
+ config.url = config.url.includes("localhost") ? withHttp(config.url) : withHttps(config.url);
65
+ return config;
66
+ }
67
+ return {
68
+ setOverrides,
69
+ cleanUp() {
70
+ cleanUps.forEach((c) => c());
71
+ },
72
+ compute
73
+ };
74
+ }
75
+
76
+ async function useSiteConfig(overrides = {}) {
77
+ const container = createSiteConfigContainer();
78
+ const runtimeConfig = {};
79
+ const nuxt = tryUseNuxt();
80
+ if (nuxt) {
81
+ for (const k of Object.keys(SITE_CONFIG_ENV)) {
82
+ const env = runtimeConfig[k] = nuxt?.options.runtimeConfig.public.site[k];
83
+ if (env)
84
+ runtimeConfig[k] = env;
85
+ }
86
+ }
87
+ const contextConfig = {};
88
+ const pkgJson = await readPackageJSON(void 0, { startingFrom: nuxt?.options.rootDir });
89
+ if (pkgJson) {
90
+ if (pkgJson.name)
91
+ contextConfig.name = pkgJson.name;
92
+ if (pkgJson.description)
93
+ contextConfig.description = pkgJson.description;
94
+ }
95
+ if (!contextConfig.name) {
96
+ contextConfig.name = nuxt?.options.rootDir.split("/").pop();
97
+ }
98
+ container.setOverrides(overrides);
99
+ return container.compute({ runtimeConfig, contextConfig });
100
+ }
101
+
102
+ export { useSiteConfig };
@@ -0,0 +1,5 @@
1
+ module.exports = function(...args) {
2
+ return import('./module.mjs').then(m => m.default.call(this, ...args))
3
+ }
4
+ const _meta = module.exports.meta = require('./module.json')
5
+ module.exports.getMeta = () => Promise.resolve(_meta)
@@ -0,0 +1,20 @@
1
+ import * as _nuxt_schema from '@nuxt/schema';
2
+ import { a as SiteConfig } from './type-8a5f4ec7.js';
3
+ import 'vue';
4
+
5
+ interface ModuleOptions extends SiteConfig {
6
+ }
7
+ interface ModulePublicRuntimeConfig {
8
+ site: Partial<SiteConfig>;
9
+ }
10
+ declare module '@nuxt/schema' {
11
+ interface RuntimeNuxtHooks {
12
+ 'site-config:resolve': (siteConfig: SiteConfig) => void;
13
+ }
14
+ }
15
+ interface ModuleHooks {
16
+ 'site-config:resolve': (siteConfig: SiteConfig) => void;
17
+ }
18
+ declare const _default: _nuxt_schema.NuxtModule<ModuleOptions>;
19
+
20
+ export { ModuleHooks, ModuleOptions, ModulePublicRuntimeConfig, _default as default };
@@ -0,0 +1,9 @@
1
+ {
2
+ "name": "nuxt-site-config",
3
+ "compatibility": {
4
+ "nuxt": "^3.5.0",
5
+ "bridge": false
6
+ },
7
+ "configKey": "site",
8
+ "version": "0.1.1"
9
+ }
@@ -0,0 +1,38 @@
1
+ import { defineNuxtModule, createResolver, addImports, addComponent } from '@nuxt/kit';
2
+ import defu from 'defu';
3
+ import { useSiteConfig } from './build.mjs';
4
+ import 'pkg-types';
5
+ import 'ufo';
6
+
7
+ const module = defineNuxtModule({
8
+ meta: {
9
+ name: "nuxt-site-config",
10
+ compatibility: {
11
+ nuxt: "^3.5.0",
12
+ bridge: false
13
+ },
14
+ configKey: "site"
15
+ },
16
+ async setup(config, nuxt) {
17
+ nuxt.options.runtimeConfig.public.site = defu(config, nuxt.options.runtimeConfig.public.site || {});
18
+ const { resolve } = createResolver(import.meta.url);
19
+ const composables = ["useSiteConfig", "createInternalLinkResolver", "resolveAbsoluteInternalLink", "resolveTrailingSlash"];
20
+ composables.forEach((c) => {
21
+ addImports({
22
+ from: resolve("./runtime/composables"),
23
+ name: c
24
+ });
25
+ });
26
+ await addComponent({
27
+ filePath: resolve("./runtime/component/SiteLink.vue"),
28
+ name: "SiteLink"
29
+ });
30
+ const shared = resolve("./runtime/shared");
31
+ nuxt.options.build.transpile.push(shared);
32
+ nuxt.hook("modules:done", async () => {
33
+ nuxt.options.runtimeConfig.public.site = await useSiteConfig(nuxt.options.runtimeConfig.public.site);
34
+ });
35
+ }
36
+ });
37
+
38
+ export { module as default };
@@ -0,0 +1,33 @@
1
+ <script lang="ts" setup>
2
+ // we need to intercept the `to` property and make sure that we have the right trailing slash
3
+ import { computed, createInternalLinkResolver, defineProps } from '#imports'
4
+
5
+ const props = defineProps({
6
+ to: {
7
+ type: String,
8
+ required: true,
9
+ },
10
+ })
11
+
12
+ const linkResolver = createInternalLinkResolver()
13
+
14
+ const to = computed(() => {
15
+ // only for relative links
16
+ if (props.to !== '/' && props.to.startsWith('/'))
17
+ return linkResolver(props.to)
18
+ return props.to
19
+ })
20
+
21
+ const attrs = computed(() => {
22
+ return {
23
+ ...props,
24
+ to: to.value,
25
+ }
26
+ })
27
+ </script>
28
+
29
+ <template>
30
+ <NuxtLink v-bind="{ ...$attrs, ...attrs }">
31
+ <slot />
32
+ </NuxtLink>
33
+ </template>
@@ -0,0 +1,2 @@
1
+ export * from './useSiteConfig';
2
+ export * from './utils';
@@ -0,0 +1,2 @@
1
+ export * from "./useSiteConfig.mjs";
2
+ export * from "./utils.mjs";
@@ -0,0 +1,2 @@
1
+ import type { MaybeComputedRefEntries, SiteConfigInput } from '../../type';
2
+ export declare function useSiteConfig(overrides?: MaybeComputedRefEntries<SiteConfigInput>): any;
@@ -0,0 +1,60 @@
1
+ import { joinURL } from "ufo";
2
+ import { getRequestURL } from "h3";
3
+ import { SITE_CONFIG_ENV, createSiteConfigContainer } from "../shared.mjs";
4
+ import {
5
+ onBeforeUnmount,
6
+ onMounted,
7
+ ref,
8
+ unref,
9
+ useAppConfig,
10
+ useRequestEvent,
11
+ useRuntimeConfig,
12
+ watch,
13
+ watchEffect
14
+ } from "#imports";
15
+ function useRequestURL() {
16
+ if (process.server) {
17
+ const { baseURL } = useRuntimeConfig().app;
18
+ const url = getRequestURL(useRequestEvent());
19
+ url.pathname = joinURL(baseURL, url.pathname);
20
+ return url;
21
+ }
22
+ return new URL(window.location.href);
23
+ }
24
+ export function useSiteConfig(overrides = {}) {
25
+ const siteConfig = ref({});
26
+ const siteConfigInput = ref({});
27
+ const container = createSiteConfigContainer();
28
+ watch(() => overrides, () => {
29
+ const tmpOverridesInput = {};
30
+ const unrefdOverrides = unref(overrides);
31
+ for (const k of Object.keys(unrefdOverrides)) {
32
+ tmpOverridesInput[k] = unref(unrefdOverrides[k]);
33
+ }
34
+ container.setOverrides(tmpOverridesInput);
35
+ siteConfig.value = container.compute(siteConfigInput.value);
36
+ }, {
37
+ deep: true,
38
+ immediate: true
39
+ });
40
+ function computeConfig() {
41
+ const { baseURL } = useRuntimeConfig().app;
42
+ const contextConfig = {
43
+ url: joinURL(useRequestURL().origin, baseURL)
44
+ };
45
+ const { public: publicRuntimeConfig } = useRuntimeConfig();
46
+ const appConfig = useAppConfig();
47
+ const runtimeConfig = {};
48
+ for (const k of Object.keys(SITE_CONFIG_ENV)) {
49
+ runtimeConfig[k] = appConfig.site?.[k] || publicRuntimeConfig.site?.[k];
50
+ }
51
+ siteConfigInput.value = { runtimeConfig, contextConfig };
52
+ siteConfig.value = container.compute(siteConfigInput.value);
53
+ }
54
+ watchEffect(computeConfig);
55
+ onMounted(computeConfig);
56
+ onBeforeUnmount(() => {
57
+ container.cleanUp && container.cleanUp();
58
+ });
59
+ return siteConfig;
60
+ }
@@ -0,0 +1,3 @@
1
+ export declare function resolveTrailingSlash(path: string): any;
2
+ export declare function resolveAbsoluteInternalLink(relativeInternalLink: string): any;
3
+ export declare function createInternalLinkResolver(): (path: string) => string;
@@ -0,0 +1,21 @@
1
+ import { withBase, withTrailingSlash, withoutTrailingSlash } from "ufo";
2
+ import { computed, useSiteConfig } from "#imports";
3
+ export function resolveTrailingSlash(path) {
4
+ const siteConfig = useSiteConfig();
5
+ return computed(() => {
6
+ return siteConfig.value.trailingSlash ? withTrailingSlash(path) : withoutTrailingSlash(path);
7
+ });
8
+ }
9
+ export function resolveAbsoluteInternalLink(relativeInternalLink) {
10
+ const siteConfig = useSiteConfig();
11
+ const slashes = resolveTrailingSlash(relativeInternalLink);
12
+ return computed(() => {
13
+ return withBase(slashes.value, siteConfig.value.url || "/");
14
+ });
15
+ }
16
+ export function createInternalLinkResolver() {
17
+ const siteConfig = useSiteConfig();
18
+ return (path) => {
19
+ return withBase(siteConfig.value.trailingSlash ? withTrailingSlash(path) : withoutTrailingSlash(path), siteConfig.value.url || "/");
20
+ };
21
+ }
@@ -0,0 +1,11 @@
1
+ import type { SiteConfig, SiteConfigInput } from '../type';
2
+ export declare const SITE_CONFIG_ENV: SiteConfigInput;
3
+ export declare function stackSiteConfigOverrides(overrides?: SiteConfigInput): () => void;
4
+ export declare function createSiteConfigContainer(): {
5
+ setOverrides: (overrides?: SiteConfigInput) => void;
6
+ cleanUp(): void;
7
+ compute: (input: {
8
+ runtimeConfig?: SiteConfigInput;
9
+ contextConfig?: SiteConfigInput;
10
+ }) => SiteConfig;
11
+ };
@@ -0,0 +1,71 @@
1
+ import { withHttp, withHttps } from "ufo";
2
+ import defu from "defu";
3
+ let _siteConfigOverrides = [];
4
+ const processShim = typeof process !== "undefined" ? process : {};
5
+ const envShim = processShim.env || {};
6
+ export const SITE_CONFIG_ENV = {
7
+ name: envShim.NUXT_PUBLIC_SITE_NAME,
8
+ url: envShim.NUXT_PUBLIC_SITE_URL,
9
+ description: envShim.NUXT_PUBLIC_SITE_DESCRIPTION,
10
+ image: envShim.NUXT_PUBLIC_SITE_IMAGE,
11
+ index: envShim.NUXT_PUBLIC_SITE_INDEX,
12
+ titleSeparator: envShim.NUXT_PUBLIC_SITE_TITLE_SEPARATOR,
13
+ trailingSlash: envShim.NUXT_PUBLIC_SITE_TRAILING_SLASH,
14
+ language: envShim.NUXT_PUBLIC_SITE_LANGUAGE
15
+ };
16
+ const NITRO_ENV_URL = [
17
+ envShim.NUXT_PUBLIC_VERCEL_URL,
18
+ // vercel
19
+ envShim.NUXT_PUBLIC_URL,
20
+ // netlify
21
+ envShim.NUXT_PUBLIC_CF_PAGES_URL
22
+ // cloudflare pages
23
+ ];
24
+ let overrideCount = 0;
25
+ export function stackSiteConfigOverrides(overrides = {}) {
26
+ const id = overrideCount++;
27
+ _siteConfigOverrides.push({
28
+ id,
29
+ ...overrides
30
+ });
31
+ return () => {
32
+ _siteConfigOverrides = _siteConfigOverrides.filter((o) => o.id !== id);
33
+ };
34
+ }
35
+ export function createSiteConfigContainer() {
36
+ const cleanUps = [];
37
+ function setOverrides(overrides) {
38
+ if (Object.keys(overrides || {}).length > 0)
39
+ cleanUps.push(stackSiteConfigOverrides(overrides));
40
+ }
41
+ function compute(input) {
42
+ const nitroEnvConfig = {};
43
+ const nitroUrl = NITRO_ENV_URL.find((k) => Boolean(k));
44
+ if (nitroUrl)
45
+ nitroEnvConfig.url = nitroUrl;
46
+ for (const k of Object.keys(SITE_CONFIG_ENV)) {
47
+ if (SITE_CONFIG_ENV[k])
48
+ nitroEnvConfig[k] = SITE_CONFIG_ENV[k];
49
+ }
50
+ const finalOverrides = {};
51
+ for (const o of _siteConfigOverrides)
52
+ Object.assign(finalOverrides, o);
53
+ const config = defu(finalOverrides, nitroEnvConfig, input.runtimeConfig || {}, input.contextConfig || {});
54
+ if (typeof config.index !== "undefined")
55
+ config.index = String(config.index) !== "false";
56
+ else
57
+ config.index = envShim.PROD;
58
+ if (typeof config.trailingSlash !== "undefined")
59
+ config.trailingSlash = String(config.trailingSlash) !== "false";
60
+ if (config.url && !config.url.startsWith("http"))
61
+ config.url = config.url.includes("localhost") ? withHttp(config.url) : withHttps(config.url);
62
+ return config;
63
+ }
64
+ return {
65
+ setOverrides,
66
+ cleanUp() {
67
+ cleanUps.forEach((c) => c());
68
+ },
69
+ compute
70
+ };
71
+ }
@@ -0,0 +1,43 @@
1
+ import { ComputedRef, Ref } from 'vue';
2
+
3
+ interface SiteConfig {
4
+ /**
5
+ * The canonical Site URL.
6
+ * @default `process.env.NUXT_PUBLIC_SITE_URL`
7
+ * Fallback options are:
8
+ * - SSR: Inferred from request headers
9
+ * - SPA: Inferred from `window.location`
10
+ * - Prerendered: Inferred from CI environment
11
+ */
12
+ url: string;
13
+ name: string;
14
+ description: string;
15
+ image: string;
16
+ titleSeparator: string;
17
+ language: string;
18
+ index: boolean;
19
+ trailingSlash: boolean;
20
+ }
21
+ interface SiteConfigInput {
22
+ url?: string;
23
+ name?: string;
24
+ description?: string;
25
+ image?: string;
26
+ titleSeparator?: string;
27
+ language?: string;
28
+ index?: boolean | string;
29
+ trailingSlash?: boolean | string;
30
+ }
31
+ declare module 'nuxt/schema' {
32
+ interface AppConfigInput {
33
+ /** Theme configuration */
34
+ site?: SiteConfigInput;
35
+ }
36
+ }
37
+ type MaybeReadonlyRef<T> = (() => T) | ComputedRef<T>;
38
+ type MaybeComputedRef<T> = T | MaybeReadonlyRef<T> | Ref<T>;
39
+ type MaybeComputedRefEntries<T> = MaybeComputedRef<T> | {
40
+ [key in keyof T]?: MaybeComputedRef<T[key]>;
41
+ };
42
+
43
+ export { MaybeReadonlyRef as M, SiteConfigInput as S, SiteConfig as a, MaybeComputedRef as b, MaybeComputedRefEntries as c };
@@ -0,0 +1,19 @@
1
+
2
+ import { ModuleOptions, ModuleHooks, ModulePublicRuntimeConfig } from './module'
3
+
4
+ declare module '@nuxt/schema' {
5
+ interface NuxtConfig { ['site']?: Partial<ModuleOptions> }
6
+ interface NuxtOptions { ['site']?: ModuleOptions }
7
+ interface NuxtHooks extends ModuleHooks {}
8
+ interface PublicRuntimeConfig extends ModulePublicRuntimeConfig {}
9
+ }
10
+
11
+ declare module 'nuxt/schema' {
12
+ interface NuxtConfig { ['site']?: Partial<ModuleOptions> }
13
+ interface NuxtOptions { ['site']?: ModuleOptions }
14
+ interface NuxtHooks extends ModuleHooks {}
15
+ interface PublicRuntimeConfig extends ModulePublicRuntimeConfig {}
16
+ }
17
+
18
+
19
+ export { ModuleHooks, ModuleOptions, ModulePublicRuntimeConfig, default } from './module'
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nuxt-site-config",
3
3
  "type": "module",
4
- "version": "0.0.3",
4
+ "version": "0.1.1",
5
5
  "packageManager": "pnpm@8.5.0",
6
6
  "description": "Shared site configuration for Nuxt 3 modules.",
7
7
  "license": "MIT",