Skip to content
This repository has been archived by the owner on Feb 10, 2025. It is now read-only.

Commit

Permalink
Fix cloudflare ssr conditions setup (#465)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Dec 5, 2024
1 parent 2c36ee5 commit 70e0054
Show file tree
Hide file tree
Showing 12 changed files with 291 additions and 47 deletions.
5 changes: 5 additions & 0 deletions .changeset/shy-bananas-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/cloudflare': patch
---

Fixes setting custom `workerd` and `worker` conditions for the ssr environment only
9 changes: 0 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,6 @@
"packageManager": "[email protected]",
"pnpm": {
"peerDependencyRules": {
"ignoreMissing": [
"rollup",
"@babel/core",
"@babel/plugin-transform-react-jsx",
"vite",
"react",
"react-dom",
"@types/react"
],
"allowAny": ["astro", "vite"]
}
},
Expand Down
4 changes: 2 additions & 2 deletions packages/cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"magic-string": "^0.30.14",
"miniflare": "^3.20241106.1",
"tiny-glob": "^0.2.9",
"vite": "^6.0.2",
"wrangler": "^3.91.0"
},
"peerDependencies": {
Expand All @@ -50,8 +51,7 @@
"execa": "^8.0.1",
"fast-glob": "^3.3.2",
"rollup": "^4.27.4",
"strip-ansi": "^7.1.0",
"vite": "6.0.1"
"strip-ansi": "^7.1.0"
},
"publishConfig": {
"provenance": true
Expand Down
27 changes: 7 additions & 20 deletions packages/cloudflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { createRedirectsFromAstroRoutes } from '@astrojs/underscore-redirects';
import astroWhen from '@inox-tools/astro-when';
import { AstroError } from 'astro/errors';
import { defaultServerConditions } from 'vite';
import { type GetPlatformProxyOptions, getPlatformProxy } from 'wrangler';
import {
type CloudflareModulePluginExtra,
Expand Down Expand Up @@ -213,12 +214,13 @@ export default function createIntegration(args?: Options): AstroIntegration {
}
}

vite.resolve.conditions ||= [];
// We need those conditions, previous these conditions where applied at the esbuild step which we removed
// https://github.com/withastro/astro/pull/7092
vite.resolve.conditions.push('workerd', 'worker');

// Support `workerd` and `worker` conditions for the ssr environment
// (previously supported in esbuild instead: https://github.com/withastro/astro/pull/7092)
vite.ssr ||= {};
vite.ssr.resolve ||= {};
vite.ssr.resolve.conditions ||= [...defaultServerConditions];
vite.ssr.resolve.conditions.push('workerd', 'worker');

vite.ssr.target = 'webworker';
vite.ssr.noExternal = true;

Expand Down Expand Up @@ -250,21 +252,6 @@ export default function createIntegration(args?: Options): AstroIntegration {
...vite.define,
};
}
// we thought that vite config inside `if (target === 'server')` would not apply for client
// but it seems like the same `vite` reference is used for both
// so we need to reset the previous conflicting setting
// in the future we should look into a more robust solution
if (target === 'client') {
vite.resolve ||= {};
vite.resolve.conditions ||= [];
vite.resolve.conditions = vite.resolve.conditions.filter(
(c) => c !== 'workerd' && c !== 'worker'
);

vite.build ||= {};
vite.build.rollupOptions ||= {};
vite.build.rollupOptions.output ||= {};
}
},
'astro:build:done': async ({ pages, routes, dir, logger }) => {
await cloudflareModulePlugin.afterBuildCompleted(_config);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import cloudflare from '@astrojs/cloudflare';
import svelte from "@astrojs/svelte";
import { defineConfig } from 'astro/config';

export default defineConfig({
integrations: [svelte()],
adapter: cloudflare(),
output: 'server',
});
11 changes: 11 additions & 0 deletions packages/cloudflare/test/fixtures/with-svelte/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@test/astro-cloudflare-with-svelte",
"version": "0.0.0",
"private": true,
"dependencies": {
"@astrojs/cloudflare": "workspace:*",
"@astrojs/svelte": "^7.0.1",
"astro": "^5.0.0",
"svelte": "^5.6.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class="svelte">Svelte Content</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
import Component from '../components/Component.svelte';
---

<html>
<head>
<title>Testing</title>
</head>
<body>
<h1>Testing</h1>
<Component />
</body>
</html>
2 changes: 1 addition & 1 deletion packages/cloudflare/test/with-solid-js.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { astroCli, wranglerCli } from './_test-utils.js';

const root = new URL('./fixtures/with-solid-js/', import.meta.url);

describe('SolidJS', { skip: 'Figure out why the test fail.' }, () => {
describe('SolidJS', () => {
let wrangler;
before(async () => {
await astroCli(fileURLToPath(root), 'build');
Expand Down
37 changes: 37 additions & 0 deletions packages/cloudflare/test/with-svelte.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';
import { fileURLToPath } from 'node:url';
import * as cheerio from 'cheerio';
import { astroCli, wranglerCli } from './_test-utils.js';

const root = new URL('./fixtures/with-svelte/', import.meta.url);

describe('Svelte', () => {
let wrangler;
before(async () => {
await astroCli(fileURLToPath(root), 'build');

wrangler = wranglerCli(fileURLToPath(root));
await new Promise((resolve) => {
wrangler.stdout.on('data', (data) => {
// console.log('[stdout]', data.toString());
if (data.toString().includes('http://127.0.0.1:8788')) resolve();
});
wrangler.stderr.on('data', (data) => {
// console.log('[stderr]', data.toString());
});
});
});

after((done) => {
wrangler.kill();
});

it('renders the svelte component', async () => {
const res = await fetch('http://127.0.0.1:8788/');
assert.equal(res.status, 200);
const html = await res.text();
const $ = cheerio.load(html);
assert.equal($('.svelte').text(), 'Svelte Content');
});
});
2 changes: 1 addition & 1 deletion packages/netlify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"@netlify/functions": "^2.8.0",
"@vercel/nft": "^0.27.6",
"esbuild": "^0.24.0",
"vite": "6.0.1"
"vite": "^6.0.2"
},
"peerDependencies": {
"astro": "^5.0.0"
Expand Down
Loading

0 comments on commit 70e0054

Please sign in to comment.