-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
325a57c
commit f430225
Showing
10 changed files
with
165 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const VIRTUAL_MODULE_ID = 'astro:link'; | ||
export const RESOLVED_VIRTUAL_MODULE_ID = '\0' + VIRTUAL_MODULE_ID; | ||
|
||
const PKG_BASE = new URL('../../', import.meta.url); | ||
export const MODULE_TEMPLATE_URL = new URL('templates/typed-links/module.mjs', PKG_BASE); | ||
export const TYPES_TEMPLATE_URL = new URL('templates/typed-links/types.d.ts', PKG_BASE); | ||
export const TYPES_FILE = 'typed-links.d.ts'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { readFileSync } from 'node:fs'; | ||
import type { AstroSettings, ManifestData } from '../types/astro.js'; | ||
import { TYPES_FILE, TYPES_TEMPLATE_URL } from './constants.js'; | ||
import { removeTrailingForwardSlash, appendForwardSlash } from '@astrojs/internal-helpers/path'; | ||
|
||
export function syncTypedLinks(settings: AstroSettings, manifest: ManifestData): void { | ||
if (!settings.config.experimental.typedLinks) { | ||
return; | ||
} | ||
|
||
const data: Array<{ route: string; params: Array<string> }> = []; | ||
|
||
const { base, trailingSlash } = settings.config; | ||
for (const { route: _route, params } of manifest.routes) { | ||
const route = `${removeTrailingForwardSlash(base)}${_route}`; | ||
if (trailingSlash === 'always') { | ||
data.push({ route: appendForwardSlash(route), params }); | ||
} else if (trailingSlash === 'never') { | ||
data.push({ route, params }); | ||
} else { | ||
const r = appendForwardSlash(route); | ||
data.push({ route, params }); | ||
if (route !== r) { | ||
data.push({ route: r, params }); | ||
} | ||
} | ||
} | ||
|
||
let types = ''; | ||
for (let i = 0; i < data.length; i++) { | ||
const { route, params } = data[i]; | ||
|
||
if (i > 0) { | ||
types += ' '; | ||
} | ||
|
||
types += `"${route}": ${ | ||
params.length === 0 | ||
? 'never' | ||
: `{${params | ||
.map((key) => `"${key}": ${key.startsWith('...') ? 'string | undefined' : 'string'}`) | ||
.join('; ')}}` | ||
};`; | ||
|
||
if (i !== data.length - 1) { | ||
types += '\n'; | ||
} | ||
} | ||
|
||
const content = readFileSync(TYPES_TEMPLATE_URL, 'utf-8').replace('// @@LINKS@@', types); | ||
settings.injectedTypes.push({ | ||
filename: TYPES_FILE, | ||
content, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { Plugin } from 'vite'; | ||
import type { AstroSettings } from '../types/astro.js'; | ||
import { MODULE_TEMPLATE_URL, RESOLVED_VIRTUAL_MODULE_ID, VIRTUAL_MODULE_ID } from './constants.js'; | ||
import { readFileSync } from 'node:fs'; | ||
|
||
interface TypedLinksPluginParams { | ||
settings: AstroSettings; | ||
} | ||
|
||
export function astroTypedLinks({ settings }: TypedLinksPluginParams): Plugin | undefined { | ||
if (!settings.config.experimental.typedLinks) { | ||
return; | ||
} | ||
|
||
const module = readFileSync(MODULE_TEMPLATE_URL, 'utf-8'); | ||
|
||
return { | ||
name: 'astro-typed-links-plugin', | ||
enforce: 'pre', | ||
resolveId(id) { | ||
if (id === VIRTUAL_MODULE_ID) { | ||
return RESOLVED_VIRTUAL_MODULE_ID; | ||
} | ||
}, | ||
load(id) { | ||
if (id === RESOLVED_VIRTUAL_MODULE_ID) { | ||
return module; | ||
} | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// @ts-check | ||
|
||
/** | ||
* @param {string} path | ||
* @param {{ params?: Record<string, string | undefined>; searchParams?: Record<string, string> | URLSearchParams; hash?: string; }} opts | ||
*/ | ||
export const link = (path, opts = {}) => { | ||
let newPath = path; | ||
if (opts.params) { | ||
for (const [key, value] of Object.entries(opts.params)) { | ||
newPath = newPath.replace(`[${key}]`, value ?? ''); | ||
} | ||
} | ||
if (opts.searchParams) { | ||
const searchParams = | ||
opts.searchParams instanceof URLSearchParams | ||
? opts.searchParams | ||
: new URLSearchParams(opts.searchParams); | ||
newPath += `?${searchParams.toString()}`; | ||
} | ||
if (opts.hash) { | ||
newPath += `#${opts.hash}`; | ||
} | ||
return newPath; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
declare module 'astro:link' { | ||
interface Links { | ||
// @@LINKS@@ | ||
} | ||
|
||
type Prettify<T> = { | ||
[K in keyof T]: T[K]; | ||
} & {}; | ||
|
||
type Opts<T> = Prettify< | ||
([T] extends [never] | ||
? { | ||
params?: never; | ||
} | ||
: { | ||
params: T; | ||
}) & { | ||
searchParams?: Record<string, string> | URLSearchParams; | ||
hash?: string; | ||
} | ||
>; | ||
|
||
export function link<TPath extends keyof Links>( | ||
path: TPath, | ||
...[opts]: Links[TPath] extends never ? [opts?: Opts<Links[TPath]>] : [opts: Opts<Links[TPath]>] | ||
): string; | ||
} | ||
|
||
// TODO: routePattern |