Skip to content

Commit

Permalink
feat: forward fast glob options, add additional patterns option for f…
Browse files Browse the repository at this point in the history
…iner control
  • Loading branch information
JulianCataldo committed Dec 22, 2024
1 parent 48bd143 commit c5f465d
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
"fast-glob": "^3.3.2",
"parse5": "^7.2.1",
"picocolors": "^1.1.1",
"satori": "^0.10.13",
"satori": "^0.10.14",
"satori-html": "^0.3.2"
},
"peerDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

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

7 changes: 6 additions & 1 deletion src/collect.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ export function extractMetadataFromHtml(fileContent) {
* @property {string} [base]
* @property {string} [out]
* @property {string} [json]
* @property {string[]} [additionalPatterns]
* @property {import('fast-glob').Options} [globber]
*
* @typedef {Required<PathsOptions>} CollectOptions
*/
Expand All @@ -94,7 +96,10 @@ export function extractMetadataFromHtml(fileContent) {
export async function collectHtmlPages(options) {
console.log(c.bold(c.yellow('Collecting HTML pages…')));

const files = await fastGlob(path.join(options.base, '**/*.html'));
const files = await fastGlob(
[path.join(options.base, '**/*.html'), ...options.additionalPatterns],
options.globber,
);

/** @type {Page[]} */
const pages = [];
Expand Down
6 changes: 3 additions & 3 deletions src/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,16 @@ export async function save(renderedImages, out) {
* @return {Promise<void>}
* */
export async function generateOgImages(options) {
const config = await loadUserConfig();

const optionsOrDefaults = {
base: options?.base || './dist',
out: options?.out || './dist/og',
json: options?.json || './dist/og/index.json',
additionalPatterns: options?.additionalPatterns || [],
globber: options?.globber || {},
};

const pages = await collectHtmlPages(optionsOrDefaults);

const config = await loadUserConfig();
const renderedImages = await renderAllPagesOg(pages, config);

await save(renderedImages, optionsOrDefaults.out);
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/vite-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ export const applyViteDevServerMiddleware = async (server) => {

/**
* @param {import("../collect").PathsOptions} [options]
* @returns {import('vite').Plugin}
* @returns {any}
*/
export function viteOgImagesGenerator(options) {
// @ts-expect-error Some incompatible types between Vite and Rollup plugin.
return {
// HACK: Returns as any to prevent Vite typings mismatches.
return /** @type {import('vite').Plugin} */ ({
...rollupOgImagesGenerator(options),

configureServer: (server) => applyViteDevServerMiddleware(server),
};
});
}
7 changes: 5 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import * as api from '../src/api.js';
import { hash } from './_utils.js';
// import { writeFile } from 'node:fs/promises';

/** @type {import('../src/collect.js').CollectOptions} */
const options = {
base: process.cwd() + '/test/__fixtures__/pages',
out: process.cwd() + '/test/__fixtures__/.test-outputs',
json: process.cwd() + '/test/__fixtures__/.test-outputs/index.json',
additionalPatterns: [],
globber: {},
};

test('Collect HTML pages metadata', async (t) => {
Expand Down Expand Up @@ -46,7 +49,7 @@ test('Generate single image', async (t) => {
},
});

assert.equal(hash(image), 'ad6a8e499e03a59a1c29e490e7d3f424');
assert.equal(hash(image), '951fcd6c24e7c6527f2caa3d8cbf32e6');
});

test('Generate single image with async. template', async (t) => {
Expand All @@ -68,5 +71,5 @@ test('Generate single image with async. template', async (t) => {
// image,
// );

assert.equal(hash(image), '62b3609ee5fd7ed967ae24a6682ccdbe');
assert.equal(hash(image), '0dc1e02ff1f555c937543540ec8acdfc');
});

0 comments on commit c5f465d

Please sign in to comment.