Skip to content

Commit

Permalink
improves style
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderniebuhr committed Mar 22, 2024
1 parent 9fb3912 commit 52abd09
Show file tree
Hide file tree
Showing 6 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'`
2 changes: 1 addition & 1 deletion packages/cloudflare/src/entrypoints/server.advanced.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {
Request as CLOUDFLARE_REQUEST,
CacheStorage as CLOUDFLARE_CACHESTORAGE,
Request as CLOUDFLARE_REQUEST,
ExecutionContext,
} from '@cloudflare/workers-types';
import type { SSRManifest } from 'astro';
Expand Down
21 changes: 0 additions & 21 deletions packages/cloudflare/src/getAdapter.ts

This file was deleted.

51 changes: 31 additions & 20 deletions packages/cloudflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import { createRedirectsFromAstroRoutes } from '@astrojs/underscore-redirects';
import { AstroError } from 'astro/errors';
import glob from 'tiny-glob';
import { getPlatformProxy } from 'wrangler';
import { getAdapter } from './getAdapter.js';
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';

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 52abd09

Please sign in to comment.