-
Notifications
You must be signed in to change notification settings - Fork 12
✨ feat(font): add assetsPrefix support for CDN deployments #30
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,10 +1,21 @@ | ||||||||||
| --- | ||||||||||
| import type { Props } from './dist/utils' | ||||||||||
| import type { Config } from './dist/utils' | ||||||||||
| import { generateFonts, getPreloadType, createPreloads, createBaseCSS, createFontCSS } from './dist/utils' | ||||||||||
|
|
||||||||||
| const { config } = Astro.props as Props | ||||||||||
| interface Props { | ||||||||||
| config: Config[]; | ||||||||||
| assetsPrefix?: string; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| const resolvedConfig = await generateFonts(config) | ||||||||||
| const { config: originalConfig, assetsPrefix } = Astro.props as Props | ||||||||||
|
|
||||||||||
| const configWithPrefix: Config[] = originalConfig.map(conf => ({ | ||||||||||
| ...conf, | ||||||||||
| // Add the assetsPrefix passed to the component to each font config | ||||||||||
| assetsPrefix: assetsPrefix, | ||||||||||
|
Comment on lines
+14
to
+15
|
||||||||||
| // Add the assetsPrefix passed to the component to each font config | |
| assetsPrefix: assetsPrefix, | |
| // Add the assetsPrefix passed to the component to each font config, only when defined | |
| ...(assetsPrefix ? { assetsPrefix } : {}), |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -57,6 +57,7 @@ interface Config { | |||||||||||||||||||||||||||||||||||||||
| fallback: 'serif' | 'sans-serif' | 'monospace' | ||||||||||||||||||||||||||||||||||||||||
| // https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face/font-display | ||||||||||||||||||||||||||||||||||||||||
| display: 'auto' | 'block' | 'swap' | 'fallback' | 'optional' | (string & {}) | ||||||||||||||||||||||||||||||||||||||||
| assetsPrefix?: string | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| export interface Props { | ||||||||||||||||||||||||||||||||||||||||
|
|
@@ -72,7 +73,25 @@ const extToPreload = { | |||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| function getBasePath(src?: string) { | ||||||||||||||||||||||||||||||||||||||||
| return src || './public' | ||||||||||||||||||||||||||||||||||||||||
| return src || './public'; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| export function generatePublicUrl(localPath: string, basePath: string, assetsPrefix?: string): string { | ||||||||||||||||||||||||||||||||||||||||
| if (localPath.startsWith('https:') || localPath.startsWith('http:')) { | ||||||||||||||||||||||||||||||||||||||||
| return localPath; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+80
to
+82
|
||||||||||||||||||||||||||||||||||||||||
| // Calculate relative path using the logic similar to the original getRelativePath | ||||||||||||||||||||||||||||||||||||||||
| const pathRelativeToPublic = relative(basePath, localPath); | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| if (assetsPrefix) { | ||||||||||||||||||||||||||||||||||||||||
| // Ensure prefix ends with a slash and relative path doesn't start with one | ||||||||||||||||||||||||||||||||||||||||
| const normalizedPrefix = assetsPrefix.endsWith('/') ? assetsPrefix : assetsPrefix + '/'; | ||||||||||||||||||||||||||||||||||||||||
| const normalizedRelative = pathRelativeToPublic.startsWith('/') ? pathRelativeToPublic.substring(1) : pathRelativeToPublic; | ||||||||||||||||||||||||||||||||||||||||
| return normalizedPrefix + normalizedRelative; | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+86
to
+90
|
||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||
| // Original behavior: return root-relative path, ensuring it starts with '/' | ||||||||||||||||||||||||||||||||||||||||
| return '/' + (pathRelativeToPublic.startsWith('/') ? pathRelativeToPublic.substring(1) : pathRelativeToPublic); | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+85
to
+93
|
||||||||||||||||||||||||||||||||||||||||
| if (assetsPrefix) { | |
| // Ensure prefix ends with a slash and relative path doesn't start with one | |
| const normalizedPrefix = assetsPrefix.endsWith('/') ? assetsPrefix : assetsPrefix + '/'; | |
| const normalizedRelative = pathRelativeToPublic.startsWith('/') ? pathRelativeToPublic.substring(1) : pathRelativeToPublic; | |
| return normalizedPrefix + normalizedRelative; | |
| } else { | |
| // Original behavior: return root-relative path, ensuring it starts with '/' | |
| return '/' + (pathRelativeToPublic.startsWith('/') ? pathRelativeToPublic.substring(1) : pathRelativeToPublic); | |
| // Normalize path separators and remove any leading slash for consistent URL construction | |
| const normalizedRelative = pathRelativeToPublic.replace(/\\/g, '/').replace(/^\//, ''); | |
| if (assetsPrefix) { | |
| // Ensure prefix ends with a slash and relative path doesn't start with one | |
| const normalizedPrefix = assetsPrefix.endsWith('/') ? assetsPrefix : assetsPrefix + '/'; | |
| return normalizedPrefix + normalizedRelative; | |
| } else { | |
| // Original behavior: return root-relative path, ensuring it starts with '/' | |
| return '/' + normalizedRelative; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
Propsinterface is redefined locally in this file instead of extending the existingPropsinterface from utils.ts. This creates a discrepancy where the utils.ts exportsPropswith only aconfigproperty, but this file defines a differentPropsinterface with bothconfigandassetsPrefix. This could cause confusion and type inconsistencies. Consider either updating the exportedPropsinterface in utils.ts to include the optionalassetsPrefixproperty, or using a different name for this local interface (e.g.,AstroFontProps) to avoid shadowing the exported type.