Skip to content

Commit

Permalink
Merge branch 'main' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
JerryWu1234 authored Nov 28, 2022
2 parents a9194f5 + c137752 commit f1ec8e7
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/swift-papayas-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix astro preview not working on Windows
7 changes: 5 additions & 2 deletions packages/astro/src/core/preview/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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.`);
}
Expand Down
8 changes: 8 additions & 0 deletions packages/astro/test/fixtures/ssr-preview/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/ssr-preview",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
8 changes: 8 additions & 0 deletions packages/astro/test/fixtures/ssr-preview/preview.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default () => {
// noop
return {
port: 3000,
closed() {},
stop() {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html lang="en">
<head>
<title>Astro</title>
</head>
<body>
Hello
</body>
</html>
22 changes: 22 additions & 0 deletions packages/astro/test/ssr-preview.test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
3 changes: 2 additions & 1 deletion packages/astro/test/test-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -66,6 +66,7 @@ export default function ({ provideAddress } = { provideAddress: true }) {
name: 'my-ssr-adapter',
serverEntrypoint: '@my-ssr',
exports: ['manifest', 'createApp'],
...extendAdapter,
});
},
},
Expand Down
16 changes: 11 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f1ec8e7

Please sign in to comment.