Skip to content
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

feat(cloudflare): support "compile-time" only image config for astro:assets #58

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
17 changes: 17 additions & 0 deletions .changeset/curvy-falcons-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
'@astrojs/cloudflare': minor
---

Adds the option to only run image optimization on images during build-time. **Warning:** This mode does not work with on-demand (SSR) image optimization.

```diff
import {defineConfig} from "astro/config";
import cloudflare from '@astrojs/cloudflare';

export default defineConfig({
output: 'server'
adapter: cloudflare({
+ imageService: 'compile'
}),
})
```
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.
42 changes: 29 additions & 13 deletions packages/cloudflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ import { getLocalRuntime, getRuntimeConfig } from './utils/local-runtime.js';
import { prependForwardSlash } from './utils/prependForwardSlash.js';
import { rewriteWasmImportPath } from './utils/rewriteWasmImportPath.js';
import { wasmModuleLoader } from './utils/wasm-module-loader.js';
import { patchSharpBundle } from './utils/sharpBundlePatch.js';

export type { AdvancedRuntime } from './entrypoints/server.advanced.js';
export type { DirectoryRuntime } from './entrypoints/server.directory.js';
export type Options = {
mode?: 'directory' | 'advanced';
functionPerRoute?: boolean;
imageService?: 'passthrough' | 'cloudflare';
imageService?: 'passthrough' | 'cloudflare' | 'compile';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs a docs pr

/** Configure automatic `routes.json` generation */
routes?: {
/** Strategy for generating `include` and `exclude` patterns
Expand Down Expand Up @@ -226,10 +227,20 @@ export default function createIntegration(args?: Options): AstroIntegration {
relative(absolutePagesDirname, pathsGroup[0]),
functionsUrl
);

const esbuildPlugins = [];
if (args?.imageService === 'compile') {
esbuildPlugins.push(patchSharpBundle());
}

const relativePathToAssets = relative(
dirname(fileURLToPath(urlWithinFunctions)),
fileURLToPath(assetsUrl)
);
if (args?.wasmModuleImports) {
esbuildPlugins.push(rewriteWasmImportPath({ relativePathToAssets }));
}

await esbuild.build({
target: 'es2022',
platform: 'browser',
Expand Down Expand Up @@ -264,9 +275,7 @@ export default function createIntegration(args?: Options): AstroIntegration {
logOverride: {
'ignored-bare-import': 'silent',
},
plugins: !args?.wasmModuleImports
? []
: [rewriteWasmImportPath({ relativePathToAssets })],
plugins: esbuildPlugins,
});
}

Expand Down Expand Up @@ -313,6 +322,21 @@ export default function createIntegration(args?: Options): AstroIntegration {
// A URL for the final build path after renaming
const finalBuildUrl = pathToFileURL(buildPath.replace(/\.mjs$/, '.js'));

const esbuildPlugins = [];
if (args?.imageService === 'compile') {
esbuildPlugins.push(patchSharpBundle());
}

if (args?.wasmModuleImports) {
esbuildPlugins.push(
rewriteWasmImportPath({
relativePathToAssets: isModeDirectory
? relative(fileURLToPath(functionsUrl), fileURLToPath(assetsUrl))
: relative(fileURLToPath(_buildConfig.client), fileURLToPath(assetsUrl)),
})
);
}

await esbuild.build({
target: 'es2022',
platform: 'browser',
Expand Down Expand Up @@ -346,15 +370,7 @@ export default function createIntegration(args?: Options): AstroIntegration {
logOverride: {
'ignored-bare-import': 'silent',
},
plugins: !args?.wasmModuleImports
? []
: [
rewriteWasmImportPath({
relativePathToAssets: isModeDirectory
? relative(fileURLToPath(functionsUrl), fileURLToPath(assetsUrl))
: relative(fileURLToPath(_buildConfig.client), fileURLToPath(assetsUrl)),
}),
],
plugins: esbuildPlugins,
});

// Rename to worker.js
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
20 changes: 20 additions & 0 deletions packages/cloudflare/src/utils/sharpBundlePatch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import esbuild from 'esbuild';

export function patchSharpBundle(): esbuild.Plugin {
return {
name: 'sharp-patch',
setup(build) {
build.onResolve({ filter: /^sharp/ }, (args) => ({
path: args.path,
namespace: 'sharp-ns',
}));

build.onLoad({ filter: /.*/, namespace: 'sharp-ns' }, (a) => {
return {
contents: JSON.stringify(''),
loader: 'json',
};
});
},
};
}
Loading