Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rare-lemons-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@blinkk/root': patch
---

feat: add `styles.entries` config for injecting global stylesheets (#929)
19 changes: 19 additions & 0 deletions packages/root/src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,25 @@ export interface RootUserConfig {
exclude?: RegExp[];
};

/**
* Config for manually injecting stylesheet entries as
* `<link rel="stylesheet">` tags.
*/
styles?: {
/**
* Project-root-relative stylesheet files to include on every rendered
* page, e.g. `styles/index.css`.
*
* Entries are normalized as URL paths, so both `styles/index.css` and
* `/styles/index.css` resolve to `/styles/index.css`.
*
* Manual entries are injected first, then auto-collected CSS
* dependencies are merged after. Duplicate URLs are de-duped so each
* stylesheet is only injected once.
*/
entries?: string[];
};

/**
* Config options for localization and internationalization.
*/
Expand Down
21 changes: 21 additions & 0 deletions packages/root/src/render/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,12 @@ export class Renderer {
});
}

// Merge user-configured stylesheet entries first. This allows global
// entries to appear before auto-collected stylesheets.
this.getConfiguredStyleEntries().forEach((styleEntry) => {
cssDeps.add(styleEntry);
});

// Parse the HTML for custom elements that are found within the project
// and automatically inject the script deps for them.
await this.collectElementDeps(mainHtml, jsDeps, cssDeps);
Expand Down Expand Up @@ -580,6 +586,16 @@ export class Renderer {
return orderedSitemap;
}


private getConfiguredStyleEntries() {
const styleEntries = this.rootConfig.styles?.entries || [];
const basePath = this.rootConfig.base || '/';
return styleEntries
.map((entry) => entry.trim())
.filter((entry) => entry)
.map((entry) => normalizeStyleEntry(entry, basePath));
}

private async renderHtml(html: string, options?: RenderHtmlOptions) {
const htmlAttrs = options?.htmlAttrs || {};
const headAttrs = options?.headAttrs || {};
Expand Down Expand Up @@ -860,6 +876,11 @@ function sortLocales(a: string, b: string) {
return a.localeCompare(b);
}

function normalizeStyleEntry(entry: string, basePath: string) {
const normalizedEntry = normalizeUrlPath(entry.replace(/^\.\//, ''));
return normalizeUrlPath(`${basePath}/${normalizedEntry}`);
}

function guessContentType(ext: string): string {
const normalized = ext.trim().toLowerCase().replace(/^\./, '');
return CONTENT_TYPES[normalized] || 'application/octet-stream';
Expand Down