From c137752797a1af39b758d207af97bf234b3ff08d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Mon, 28 Nov 2022 16:45:28 +0900 Subject: [PATCH] Fix astro preview not working on Windows (#5480) --- .changeset/swift-papayas-kiss.md | 5 +++++ packages/astro/src/core/preview/index.ts | 7 ++++-- .../test/fixtures/ssr-preview/package.json | 8 +++++++ .../test/fixtures/ssr-preview/preview.mjs | 8 +++++++ .../ssr-preview/src/pages/index.astro | 8 +++++++ packages/astro/test/ssr-preview.test.js | 22 +++++++++++++++++++ packages/astro/test/test-adapter.js | 3 ++- pnpm-lock.yaml | 16 +++++++++----- 8 files changed, 69 insertions(+), 8 deletions(-) create mode 100644 .changeset/swift-papayas-kiss.md create mode 100644 packages/astro/test/fixtures/ssr-preview/package.json create mode 100644 packages/astro/test/fixtures/ssr-preview/preview.mjs create mode 100644 packages/astro/test/fixtures/ssr-preview/src/pages/index.astro create mode 100644 packages/astro/test/ssr-preview.test.js diff --git a/.changeset/swift-papayas-kiss.md b/.changeset/swift-papayas-kiss.md new file mode 100644 index 000000000000..d7681461df74 --- /dev/null +++ b/.changeset/swift-papayas-kiss.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix astro preview not working on Windows diff --git a/packages/astro/src/core/preview/index.ts b/packages/astro/src/core/preview/index.ts index 818ac90fdee0..ea8eef1e1a07 100644 --- a/packages/astro/src/core/preview/index.ts +++ b/packages/astro/src/core/preview/index.ts @@ -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; + const previewModule = (await import(previewEntrypointUrl)) as Partial; if (typeof previewModule.default !== 'function') { throw new Error(`[preview] ${settings.adapter.name} cannot preview your app.`); } diff --git a/packages/astro/test/fixtures/ssr-preview/package.json b/packages/astro/test/fixtures/ssr-preview/package.json new file mode 100644 index 000000000000..667d618768ee --- /dev/null +++ b/packages/astro/test/fixtures/ssr-preview/package.json @@ -0,0 +1,8 @@ +{ + "name": "@test/ssr-preview", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/ssr-preview/preview.mjs b/packages/astro/test/fixtures/ssr-preview/preview.mjs new file mode 100644 index 000000000000..745f226245df --- /dev/null +++ b/packages/astro/test/fixtures/ssr-preview/preview.mjs @@ -0,0 +1,8 @@ +export default () => { + // noop + return { + port: 3000, + closed() {}, + stop() {} + } +} diff --git a/packages/astro/test/fixtures/ssr-preview/src/pages/index.astro b/packages/astro/test/fixtures/ssr-preview/src/pages/index.astro new file mode 100644 index 000000000000..ec8925686b17 --- /dev/null +++ b/packages/astro/test/fixtures/ssr-preview/src/pages/index.astro @@ -0,0 +1,8 @@ + + + Astro + + + Hello + + diff --git a/packages/astro/test/ssr-preview.test.js b/packages/astro/test/ssr-preview.test.js new file mode 100644 index 000000000000..d4b63ea53361 --- /dev/null +++ b/packages/astro/test/ssr-preview.test.js @@ -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(); + }); +}); diff --git a/packages/astro/test/test-adapter.js b/packages/astro/test/test-adapter.js index 64bf853365f5..4faa9a7c69cb 100644 --- a/packages/astro/test/test-adapter.js +++ b/packages/astro/test/test-adapter.js @@ -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, }); }, }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f074dd23d97e..faf1e2c06305 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -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: