Skip to content

Commit

Permalink
feat(cloudflare): support "compile-time" only image config for astro:…
Browse files Browse the repository at this point in the history
…assets
  • Loading branch information
alexanderniebuhr committed Jan 4, 2024
1 parent c365926 commit d30a95e
Show file tree
Hide file tree
Showing 12 changed files with 1,299 additions and 975 deletions.
5 changes: 5 additions & 0 deletions .changeset/curvy-falcons-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/cloudflare': minor
---

TBA
1 change: 1 addition & 0 deletions packages/cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"./entrypoints/server.advanced.js": "./dist/entrypoints/server.advanced.js",
"./entrypoints/server.directory.js": "./dist/entrypoints/server.directory.js",
"./image-service": "./dist/entrypoints/image-service.js",
"./image-endpoint": "./dist/entrypoints/image-endpoint.js",
"./package.json": "./package.json"
},
"files": [
Expand Down
Empty file.
2 changes: 1 addition & 1 deletion packages/cloudflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export type { DirectoryRuntime } from './entrypoints/server.directory.js';
export type Options = {
mode?: 'directory' | 'advanced';
functionPerRoute?: boolean;
imageService?: 'passthrough' | 'cloudflare';
imageService?: 'passthrough' | 'cloudflare' | 'compile';
/** Configure automatic `routes.json` generation */
routes?: {
/** Strategy for generating `include` and `exclude` patterns
Expand Down
7 changes: 7 additions & 0 deletions packages/cloudflare/src/utils/image-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ export function prepareImageConfig(
: { entrypoint: '@astrojs/cloudflare/image-service' },
};

case 'compile':
return {
...config,
service: sharpImageService(),
endpoint: command === 'dev' ? undefined : '@astrojs/cloudflare/image-endpoint',
};

default:
if (
config.service.entrypoint === 'astro/assets/services/sharp' ||
Expand Down
15 changes: 15 additions & 0 deletions packages/cloudflare/test/assets-compile-time.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { expect } from 'chai';
import { readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import { astroCli } from './_test-utils.js';

const root = new URL('./fixtures/assets-compile-time/', import.meta.url);

describe('AssetsCompileTime', () => {
it('has correct image service', async () => {
await astroCli(fileURLToPath(root), 'build');

const outFileToCheck = readFileSync(fileURLToPath(new URL('dist/_worker.js', root)), 'utf-8');
expect(outFileToCheck).to.not.include('sharp')
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { defineConfig } from 'astro/config';
import cloudflare from '@astrojs/cloudflare';

export default defineConfig({
output: 'server',
adapter: cloudflare({
imageService: 'compile',
}),
});

// imageService: "compile", is equivalent to:
// {
// service: { entrypoint: 'astro/assets/services/sharp', config: {} },
// domains: [],
// remotePatterns: [],
// endpoint: '@astrojs/cloudflare/image-endpoint'
// }
12 changes: 12 additions & 0 deletions packages/cloudflare/test/fixtures/assets-compile-time/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@test/astro-cloudflare-assets-compile-time",
"version": "0.0.0",
"private": true,
"dependencies": {
"@astrojs/cloudflare": "workspace:*",
"astro": "^3.5.6"
},
"devDependencies": {
"sharp": "^0.32.6"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// <reference path="../.astro/types.d.ts" />
/// <reference types="astro/client" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
export const prerender = true;
import { Image } from "astro:assets";
import lighthouse from "../assets/lighthouse.jpg";
---
<Image src={lighthouse} width="512" quality="100" alt="Lighthouse" />
Loading

0 comments on commit d30a95e

Please sign in to comment.