Skip to content

Commit

Permalink
improves style
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderniebuhr committed Mar 21, 2024
1 parent 6612f4c commit e8dde0c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 44 deletions.
2 changes: 2 additions & 0 deletions packages/cloudflare/src/entrypoints/image-endpoint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// NOTE: this file is empty on purpose
// it allows use to offer `imageService: 'compile'`
21 changes: 0 additions & 21 deletions packages/cloudflare/src/getAdapter.ts

This file was deleted.

53 changes: 32 additions & 21 deletions packages/cloudflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import { fileURLToPath } from 'node:url';
import { createRedirectsFromAstroRoutes } from '@astrojs/underscore-redirects';
import { AstroError } from 'astro/errors';
import glob from 'tiny-glob';
import { getAdapter } from './getAdapter.js';
import { getPlatformProxy } from 'wrangler';
import { deduplicatePatterns } from './utils/deduplicatePatterns.js';
import { prepareImageConfig } from './utils/image-config.js';
import { setImageConfig } from './utils/image-config.js';
import { prependForwardSlash } from './utils/prependForwardSlash.js';
import { wasmModuleLoader } from './utils/wasm-module-loader.js';
import { getPlatformProxy } from 'wrangler';

export type { AdvancedRuntime } from './entrypoints/server.advanced.js';

export type Options = {
/** Options for handling images. */
imageService?: 'passthrough' | 'cloudflare' | 'compile';
/** Configure automatic `routes.json` generation */
routes?: {
Expand Down Expand Up @@ -43,28 +43,20 @@ export type Options = {
/** Configuration persistence settings. Default '.wrangler/state/v3' */
persist?: boolean | { path: string };
};
/** Enable WebAssembly support */
wasmModuleImports?: boolean;
};

interface BuildConfig {
server: URL;
client: URL;
assets: string;
serverEntry: string;
split?: boolean;
}

export default function createIntegration(args?: Options): AstroIntegration {
let _config: AstroConfig;
let _buildConfig: BuildConfig;

return {
name: '@astrojs/cloudflare',
hooks: {
'astro:config:setup': ({ command, config, updateConfig, logger }) => {
updateConfig({
build: {
client: new URL(`.${config.base}`, config.outDir),
client: new URL(`.${config.base}/`, config.outDir),
server: new URL('./_worker.js/', config.outDir),
serverEntry: 'index.js',
redirects: false,
Expand All @@ -77,19 +69,38 @@ export default function createIntegration(args?: Options): AstroIntegration {
}),
],
},
image: prepareImageConfig(args?.imageService ?? 'DEFAULT', config.image, command, logger),
image: setImageConfig(args?.imageService ?? 'DEFAULT', config.image, command, logger),
});
},
'astro:config:done': ({ setAdapter, config }) => {
setAdapter(getAdapter());
_config = config;
_buildConfig = config.build;

if (_config.output === 'static') {
if (config.output === 'static') {
throw new AstroError(
'[@astrojs/cloudflare] `output: "server"` or `output: "hybrid"` is required to use this adapter. Otherwise, this adapter is not necessary to deploy a static site to Cloudflare.'
);
}

setAdapter({
name: '@astrojs/cloudflare',
serverEntrypoint: '@astrojs/cloudflare/entrypoints/server.advanced.js',
exports: ['default'],
adapterFeatures: {
functionPerRoute: false,
edgeMiddleware: false,
},
supportedAstroFeatures: {
serverOutput: 'stable',
hybridOutput: 'stable',
staticOutput: 'unsupported',
i18nDomains: 'experimental',
assets: {
supportKind: 'stable',
isSharpCompatible: false,
isSquooshCompatible: false,
},
},
});
},
'astro:server:setup': async ({ server, logger }) => {
if (args?.platformProxy?.enabled === true) {
Expand Down Expand Up @@ -137,15 +148,15 @@ export default function createIntegration(args?: Options): AstroIntegration {
vite.ssr ||= {};
vite.ssr.target = 'webworker';
vite.ssr.noExternal = true;
vite.ssr.external = _config.vite?.ssr?.external ?? [];
vite.ssr.external = _config.vite.ssr?.external ?? [];

vite.build ||= {};
vite.build.rollupOptions ||= {};
vite.build.rollupOptions.output ||= {};
// @ts-ignore
vite.build.rollupOptions.output.banner ||= 'globalThis.process ??= {};';

vite.build.rollupOptions.external = _config.vite?.build?.rollupOptions?.external ?? [];
vite.build.rollupOptions.external = _config.vite.build?.rollupOptions?.external ?? [];
}
},
'astro:build:done': async ({ pages, routes, dir }) => {
Expand All @@ -156,7 +167,7 @@ export default function createIntegration(args?: Options): AstroIntegration {
for (const file of cloudflareSpecialFiles) {
try {
await fs.promises.rename(
new URL(file, _buildConfig.client),
new URL(file, _config.build.client),
new URL(file, _config.outDir)
);
} catch (e) {
Expand Down Expand Up @@ -203,7 +214,7 @@ export default function createIntegration(args?: Options): AstroIntegration {
});

const staticPathList: Array<string> = (
await glob(`${fileURLToPath(_buildConfig.client)}/**/*`, {
await glob(`${fileURLToPath(_config.build.client)}/**/*`, {
cwd: fileURLToPath(_config.outDir),
filesOnly: true,
dot: true,
Expand Down
2 changes: 1 addition & 1 deletion packages/cloudflare/src/utils/image-config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { AstroConfig, AstroIntegrationLogger } from 'astro';
import { passthroughImageService, sharpImageService } from 'astro/config';

export function prepareImageConfig(
export function setImageConfig(
service: string,
config: AstroConfig['image'],
command: 'dev' | 'build' | 'preview',
Expand Down
2 changes: 1 addition & 1 deletion packages/cloudflare/test/fixtures/wasm/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ export default defineConfig({
adapter: cloudflare({
wasmModuleImports: true
}),
output: 'server'
output: 'hybrid'
});

0 comments on commit e8dde0c

Please sign in to comment.