Fix astro preview not working on Windows (#5480)

This commit is contained in:
翠 / green 2022-11-28 16:45:28 +09:00 committed by GitHub
parent 5fa1285239
commit c137752797
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 69 additions and 8 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix astro preview not working on Windows

View file

@ -1,5 +1,6 @@
import type { AstroTelemetry } from '@astrojs/telemetry';
import { createRequire } from 'module';
import { pathToFileURL } from 'url';
import type { AstroSettings, PreviewModule, PreviewServer } from '../../@types/astro';
import { runHookConfigDone, runHookConfigSetup } from '../../integrations/index.js';
import type { LogOptions } from '../logger/core';
@ -41,9 +42,11 @@ export default async function preview(
// don't treat this as a dependency of Astro itself. This correctly resolves the
// preview entrypoint of the integration package, relative to the user's project root.
const require = createRequire(settings.config.root);
const previewEntrypoint = require.resolve(settings.adapter.previewEntrypoint);
const previewEntrypointUrl = pathToFileURL(
require.resolve(settings.adapter.previewEntrypoint)
).href;
const previewModule = (await import(previewEntrypoint)) as Partial<PreviewModule>;
const previewModule = (await import(previewEntrypointUrl)) as Partial<PreviewModule>;
if (typeof previewModule.default !== 'function') {
throw new Error(`[preview] ${settings.adapter.name} cannot preview your app.`);
}

View file

@ -0,0 +1,8 @@
{
"name": "@test/ssr-preview",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}

View file

@ -0,0 +1,8 @@
export default () => {
// noop
return {
port: 3000,
closed() {},
stop() {}
}
}

View file

@ -0,0 +1,8 @@
<html lang="en">
<head>
<title>Astro</title>
</head>
<body>
Hello
</body>
</html>

View file

@ -0,0 +1,22 @@
import { loadFixture } from './test-utils.js';
import testAdapter from './test-adapter.js';
describe('SSR Preview', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/ssr-preview/',
output: 'server',
adapter: testAdapter({ extendAdapter: { previewEntrypoint: './preview.mjs' } }),
});
await fixture.build();
});
it('preview server works', async () => {
/** @type {import('./test-utils').PreviewServer} */
const previewServer = await fixture.preview();
await previewServer.stop();
});
});

View file

@ -4,7 +4,7 @@ import { viteID } from '../dist/core/util.js';
*
* @returns {import('../src/@types/astro').AstroIntegration}
*/
export default function ({ provideAddress } = { provideAddress: true }) {
export default function ({ provideAddress = true, extendAdapter } = { provideAddress: true }) {
return {
name: 'my-ssr-adapter',
hooks: {
@ -66,6 +66,7 @@ export default function ({ provideAddress } = { provideAddress: true }) {
name: 'my-ssr-adapter',
serverEntrypoint: '@my-ssr',
exports: ['manifest', 'createApp'],
...extendAdapter,
});
},
},

View file

@ -2276,6 +2276,12 @@ importers:
'@astrojs/partytown': link:../../../../integrations/partytown
astro: link:../../..
packages/astro/test/fixtures/ssr-preview:
specifiers:
astro: workspace:*
dependencies:
astro: link:../../..
packages/astro/test/fixtures/ssr-redirect:
specifiers:
astro: workspace:*
@ -3831,7 +3837,7 @@ packages:
'@astro-community/astro-embed-twitter': 0.1.3_astro@packages+astro
'@astro-community/astro-embed-vimeo': 0.1.1_astro@packages+astro
'@astro-community/astro-embed-youtube': 0.2.1_astro@packages+astro
astro: link:packages/astro
astro: link:packages\astro
unist-util-select: 4.0.1
dev: false
@ -3841,7 +3847,7 @@ packages:
astro: ^1.0.0-beta.10
dependencies:
'@astro-community/astro-embed-utils': 0.0.3
astro: link:packages/astro
astro: link:packages\astro
dev: false
/@astro-community/astro-embed-utils/0.0.3:
@ -3853,7 +3859,7 @@ packages:
peerDependencies:
astro: ^1.0.0-beta.10
dependencies:
astro: link:packages/astro
astro: link:packages\astro
lite-vimeo-embed: 0.1.0
dev: false
@ -3862,7 +3868,7 @@ packages:
peerDependencies:
astro: ^1.0.0-beta.10
dependencies:
astro: link:packages/astro
astro: link:packages\astro
lite-youtube-embed: 0.2.0
dev: false
@ -10594,7 +10600,7 @@ packages:
'@astro-community/astro-embed-twitter': 0.1.3_astro@packages+astro
'@astro-community/astro-embed-vimeo': 0.1.1_astro@packages+astro
'@astro-community/astro-embed-youtube': 0.2.1_astro@packages+astro
astro: link:packages/astro
astro: link:packages\astro
dev: false
/async-sema/3.1.1: