Skip to content
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
6 changes: 6 additions & 0 deletions .changeset/fix-svelte-prerender-node.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@astrojs/cloudflare': patch
'@astrojs/svelte': patch
---

Fixes `.svelte` files in `node_modules` failing with `Unknown file extension ".svelte"` when using the Cloudflare adapter with `prerenderEnvironment: 'node'`
1 change: 0 additions & 1 deletion packages/integrations/cloudflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,6 @@ export default function createIntegration({
if (conf.ssr) {
// Cloudflare does not support externalizing modules in server environments
conf.ssr.external = undefined;
conf.ssr.noExternal = true;
}
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@ export function createNodePrerenderPlugin(): vite.Plugin {
};
},

// Disable dep optimization for the `prerender` environment so dependencies
// are loaded via native import() with correct import.meta.url semantics.
configEnvironment(environmentName) {
if (environmentName === 'prerender') {
return { optimizeDeps: { noDiscovery: true, include: [] } };
}
},

configureServer(server) {
(server as any)[devPrerenderMiddlewareSymbol] = true;
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { defineConfig } from 'astro/config';
import svelte from '@astrojs/svelte';

export default defineConfig({});
export default defineConfig({
integrations: [svelte()],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "fake-svelte-pkg",
"version": "1.0.0",
"type": "module",
"exports": {
".": "./src/index.js"
},
"peerDependencies": {
"svelte": "^5.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>Hello from fake svelte component</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as FakeComponent } from './FakeComponent.svelte';
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"private": true,
"dependencies": {
"@astrojs/cloudflare": "workspace:*",
"astro": "workspace:*"
"@astrojs/svelte": "workspace:*",
"astro": "workspace:*",
"svelte": "^5.0.0",
"fake-svelte-pkg": "file:./fake-svelte-pkg"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
import { FakeComponent } from 'fake-svelte-pkg';
</script>

<div id="svelte-wrapper">
<FakeComponent />
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
export const prerender = true;

import SvelteWrapper from '../components/SvelteWrapper.svelte';
---

<html lang="en">
<head>
<meta charset="utf-8" />
<title>Svelte Prerender Test</title>
</head>
<body>
<SvelteWrapper />
</body>
</html>
14 changes: 14 additions & 0 deletions packages/integrations/cloudflare/test/prerender-node-env.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ describe('prerenderEnvironment: node', () => {
);
});

it('renders svelte component that imports .svelte files from node_modules', async () => {
const res = await fixture.fetch('/svelte');
assert.equal(res.status, 200);
const html = await res.text();
assert.ok(
html.includes('id="svelte-wrapper"'),
'Expected the prerendered page to contain the svelte wrapper',
);
assert.ok(
html.includes('Hello from fake svelte component'),
'Expected the fake svelte component to be rendered',
);
});

it('includes styles in prerendered page', async () => {
const res = await fixture.fetch('/');
const html = await res.text();
Expand Down
3 changes: 2 additions & 1 deletion packages/integrations/svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"dependencies": {
"@sveltejs/vite-plugin-svelte": "^6.2.4",
"svelte2tsx": "^0.7.52",
"vite": "^7.3.1"
"vite": "^7.3.1",
"vitefu": "^1.1.2"
},
"devDependencies": {
"astro": "workspace:*",
Expand Down
45 changes: 41 additions & 4 deletions packages/integrations/svelte/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { Options } from '@sveltejs/vite-plugin-svelte';
import { svelte, vitePreprocess } from '@sveltejs/vite-plugin-svelte';
import type { AstroIntegration, AstroRenderer } from 'astro';
import { fileURLToPath } from 'node:url';
import type { Plugin } from 'vite';
import { crawlFrameworkPkgs } from 'vitefu';
import { createSvelteOptimizeEsbuildPlugins } from './optimize-esbuild-plugins.js';

function getRenderer(): AstroRenderer {
Expand All @@ -18,28 +20,63 @@ export default function svelteIntegration(options?: Options): AstroIntegration {
return {
name: '@astrojs/svelte',
hooks: {
'astro:config:setup': async ({ updateConfig, addRenderer }) => {
'astro:config:setup': async ({ config, updateConfig, addRenderer }) => {
addRenderer(getRenderer());

const sveltePackages = await crawlFrameworkPkgs({
root: fileURLToPath(config.root),
isBuild: false,
isFrameworkPkgByJson(pkgJson) {
return !!pkgJson.peerDependencies?.svelte;
},
});

updateConfig({
vite: {
plugins: [svelte(options), configEnvironmentPlugin()],
plugins: [svelte(options), configEnvironmentPlugin(sveltePackages.ssr.noExternal)],
},
});
},
},
};
}

function configEnvironmentPlugin(): Plugin {
function configEnvironmentPlugin(svelteNoExternal: string[]): Plugin {
return {
name: '@astrojs/svelte:config-environment',
configEnvironment(environmentName, options) {
const isServer = environmentName !== 'client';

if (isServer && svelteNoExternal.length > 0) {
// Add svelte framework packages to noExternal so they go through
// Vite's transform pipeline (Node can't import .svelte files natively).
const result: any = {
resolve: {
noExternal: svelteNoExternal,
},
};

if (
(environmentName === 'ssr' || environmentName === 'prerender') &&
options.optimizeDeps?.noDiscovery === false
) {
result.optimizeDeps = {
include: ['svelte/server', 'svelte/internal/server'],
exclude: ['@astrojs/svelte/server.js'],
esbuildOptions: {
plugins: createSvelteOptimizeEsbuildPlugins('server'),
},
};
}

return result;
}

if (
environmentName === 'client' ||
((environmentName === 'ssr' || environmentName === 'prerender') &&
options.optimizeDeps?.noDiscovery === false)
) {
const isServer = environmentName !== 'client';
return {
optimizeDeps: {
include: isServer
Expand Down
Loading
Loading