mirror of
https://github.com/withastro/astro.git
synced 2025-01-20 22:12:38 -05:00
Prevent externalized adapters from breaking build (#11709)
* Prevent externalized adapters from breaking build * Add a changeset * Implement in both types of build plugins * linting :(
This commit is contained in:
parent
69a7a5f132
commit
3d8ae767fd
3 changed files with 46 additions and 14 deletions
5
.changeset/new-boats-flash.md
Normal file
5
.changeset/new-boats-flash.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix adapter causing Netlify to break
|
|
@ -19,6 +19,26 @@ import { getComponentFromVirtualModulePageName, getVirtualModulePageName } from
|
||||||
export const SSR_VIRTUAL_MODULE_ID = '@astrojs-ssr-virtual-entry';
|
export const SSR_VIRTUAL_MODULE_ID = '@astrojs-ssr-virtual-entry';
|
||||||
export const RESOLVED_SSR_VIRTUAL_MODULE_ID = '\0' + SSR_VIRTUAL_MODULE_ID;
|
export const RESOLVED_SSR_VIRTUAL_MODULE_ID = '\0' + SSR_VIRTUAL_MODULE_ID;
|
||||||
|
|
||||||
|
const ADAPTER_VIRTUAL_MODULE_ID = '@astrojs-ssr-adapter';
|
||||||
|
const RESOLVED_ADAPTER_VIRTUAL_MODULE_ID = '\0' + ADAPTER_VIRTUAL_MODULE_ID;
|
||||||
|
|
||||||
|
function vitePluginAdapter(adapter: AstroAdapter): VitePlugin {
|
||||||
|
return {
|
||||||
|
name: '@astrojs/vite-plugin-astro-adapter',
|
||||||
|
enforce: 'post',
|
||||||
|
resolveId(id) {
|
||||||
|
if (id === ADAPTER_VIRTUAL_MODULE_ID) {
|
||||||
|
return RESOLVED_ADAPTER_VIRTUAL_MODULE_ID;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
async load(id) {
|
||||||
|
if(id === RESOLVED_ADAPTER_VIRTUAL_MODULE_ID) {
|
||||||
|
return `export * from '${adapter.serverEntrypoint}';`;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
function vitePluginSSR(
|
function vitePluginSSR(
|
||||||
internals: BuildInternals,
|
internals: BuildInternals,
|
||||||
adapter: AstroAdapter,
|
adapter: AstroAdapter,
|
||||||
|
@ -39,7 +59,7 @@ function vitePluginSSR(
|
||||||
|
|
||||||
const adapterServerEntrypoint = options.settings.adapter?.serverEntrypoint;
|
const adapterServerEntrypoint = options.settings.adapter?.serverEntrypoint;
|
||||||
if (adapterServerEntrypoint) {
|
if (adapterServerEntrypoint) {
|
||||||
inputs.add(adapterServerEntrypoint);
|
inputs.add(ADAPTER_VIRTUAL_MODULE_ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
inputs.add(SSR_VIRTUAL_MODULE_ID);
|
inputs.add(SSR_VIRTUAL_MODULE_ID);
|
||||||
|
@ -119,14 +139,19 @@ export function pluginSSR(
|
||||||
targets: ['server'],
|
targets: ['server'],
|
||||||
hooks: {
|
hooks: {
|
||||||
'build:before': () => {
|
'build:before': () => {
|
||||||
let vitePlugin =
|
const adapter = options.settings.adapter!;
|
||||||
|
let ssrPlugin =
|
||||||
ssr && functionPerRouteEnabled === false
|
ssr && functionPerRouteEnabled === false
|
||||||
? vitePluginSSR(internals, options.settings.adapter!, options)
|
? vitePluginSSR(internals, adapter, options)
|
||||||
: undefined;
|
: undefined;
|
||||||
|
const vitePlugin = [vitePluginAdapter(adapter)];
|
||||||
|
if(ssrPlugin) {
|
||||||
|
vitePlugin.unshift(ssrPlugin);
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
enforce: 'after-user-plugins',
|
enforce: 'after-user-plugins',
|
||||||
vitePlugin,
|
vitePlugin: vitePlugin,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
'build:post': async () => {
|
'build:post': async () => {
|
||||||
|
@ -231,10 +256,15 @@ export function pluginSSRSplit(
|
||||||
targets: ['server'],
|
targets: ['server'],
|
||||||
hooks: {
|
hooks: {
|
||||||
'build:before': () => {
|
'build:before': () => {
|
||||||
let vitePlugin =
|
const adapter = options.settings.adapter!;
|
||||||
|
let ssrPlugin =
|
||||||
ssr && functionPerRouteEnabled
|
ssr && functionPerRouteEnabled
|
||||||
? vitePluginSSRSplit(internals, options.settings.adapter!, options)
|
? vitePluginSSRSplit(internals, adapter, options)
|
||||||
: undefined;
|
: undefined;
|
||||||
|
const vitePlugin = [vitePluginAdapter(adapter)];
|
||||||
|
if(ssrPlugin) {
|
||||||
|
vitePlugin.unshift(ssrPlugin);
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
enforce: 'after-user-plugins',
|
enforce: 'after-user-plugins',
|
||||||
|
@ -251,7 +281,7 @@ function generateSSRCode(settings: AstroSettings, adapter: AstroAdapter, middlew
|
||||||
|
|
||||||
const imports = [
|
const imports = [
|
||||||
`import { renderers } from '${RENDERERS_MODULE_ID}';`,
|
`import { renderers } from '${RENDERERS_MODULE_ID}';`,
|
||||||
`import * as serverEntrypointModule from '${adapter.serverEntrypoint}';`,
|
`import * as serverEntrypointModule from '${ADAPTER_VIRTUAL_MODULE_ID}';`,
|
||||||
`import { manifest as defaultManifest } from '${SSR_MANIFEST_VIRTUAL_MODULE_ID}';`,
|
`import { manifest as defaultManifest } from '${SSR_MANIFEST_VIRTUAL_MODULE_ID}';`,
|
||||||
edgeMiddleware ? `` : `import { onRequest as middleware } from '${middlewareId}';`,
|
edgeMiddleware ? `` : `import { onRequest as middleware } from '${middlewareId}';`,
|
||||||
settings.config.experimental.serverIslands
|
settings.config.experimental.serverIslands
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import assert from 'node:assert/strict';
|
import assert from 'node:assert/strict';
|
||||||
import { existsSync, readFileSync } from 'node:fs';
|
import { existsSync, readFileSync } from 'node:fs';
|
||||||
import { resolve } from 'node:path';
|
|
||||||
import { before, describe, it } from 'node:test';
|
import { before, describe, it } from 'node:test';
|
||||||
import { fileURLToPath } from 'node:url';
|
import { fileURLToPath } from 'node:url';
|
||||||
import * as cheerio from 'cheerio';
|
import * as cheerio from 'cheerio';
|
||||||
|
@ -62,12 +61,10 @@ describe('astro:ssr-manifest, split', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should correctly emit the the pre render page', async () => {
|
it('should correctly emit the the pre render page', async () => {
|
||||||
const text = readFileSync(
|
const indexUrl = new URL('./fixtures/ssr-split-manifest/dist/client/prerender/index.html', import.meta.url);
|
||||||
resolve('./test/fixtures/ssr-split-manifest/dist/client/prerender/index.html'),
|
const text = readFileSync(indexUrl, {
|
||||||
{
|
encoding: 'utf8',
|
||||||
encoding: 'utf8',
|
});
|
||||||
},
|
|
||||||
);
|
|
||||||
assert.equal(text.includes('<title>Pre render me</title>'), true);
|
assert.equal(text.includes('<title>Pre render me</title>'), true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue