From 48efa4beda6a858c0b0937f6a24bf9921d7f97d0 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Mon, 13 Feb 2023 09:47:44 -0500 Subject: [PATCH] Remove some more containerisms --- packages/astro/src/core/app/dev.ts | 26 ++++++++++- packages/astro/test/units/dev/dev.test.js | 56 +++++++++++++---------- 2 files changed, 55 insertions(+), 27 deletions(-) diff --git a/packages/astro/src/core/app/dev.ts b/packages/astro/src/core/app/dev.ts index eb2e1defc..8aa22513b 100644 --- a/packages/astro/src/core/app/dev.ts +++ b/packages/astro/src/core/app/dev.ts @@ -1,5 +1,7 @@ import type { ComponentInstance, ManifestData, RouteData } from '../../@types/astro'; import type { SSRManifest as Manifest } from './types'; +import { posix } from 'node:path'; +import { fileURLToPath } from 'node:url'; import { createContainer, type CreateContainerParams } from '../dev/index.js'; import { createViteLoader } from '../module-loader/index.js'; import { createRouteManifest } from '../routing/index.js'; @@ -10,10 +12,9 @@ import { type DevelopmentEnvironment } from '../render/dev/index.js'; import { - createRenderContext, renderPage as coreRenderPage } from '../render/index.js'; -import { App, MatchOptions } from './index.js'; +import { App } from './index.js'; import { RenderContext, Environment } from '../render'; export type DevAppParams = Partial & { @@ -53,6 +54,14 @@ export class DevApp extends App { return !!this.#container; } + url(pathname: string): string | undefined { + if(!this.loaded) { + return undefined; + } + const { host, port } = this.#container!.settings.config.server + return new URL(pathname, `http://${host}:${port}`).toString(); + } + async load() { if(this.loaded) { await this.close(); @@ -86,6 +95,19 @@ export class DevApp extends App { await this.#container?.close(); } + fileChanged(path: string) { + const container = this.#container!; + const fs = this.#createContainerParams.fs!; + const root = fileURLToPath(this.#root); + const fullPath = posix.join(root, path); + container.viteServer.watcher.emit('change', fullPath); + + if (!fileURLToPath(container.settings.config.root).startsWith('/')) { + const drive = fileURLToPath(container.settings.config.root).slice(0, 2); + container.viteServer.watcher.emit('change', drive + fullPath); + } + } + async render(request: Request, route?: RouteData | undefined): Promise { if(!this.loaded) { await this.load(); diff --git a/packages/astro/test/units/dev/dev.test.js b/packages/astro/test/units/dev/dev.test.js index eddc14c5d..3af1f25d9 100644 --- a/packages/astro/test/units/dev/dev.test.js +++ b/packages/astro/test/units/dev/dev.test.js @@ -1,6 +1,7 @@ import { expect } from 'chai'; import * as cheerio from 'cheerio'; +import { DevApp } from '../../../dist/core/app/dev.js'; import { runInContainer } from '../../../dist/core/dev/index.js'; import { createFs, createRequestAndResponse, triggerFSEvent } from '../test-utils.js'; @@ -25,17 +26,21 @@ describe('dev container', () => { root ); - await runInContainer({ fs, root }, async (container) => { - const { req, res, text } = createRequestAndResponse({ - method: 'GET', - url: '/', - }); - container.handle(req, res); - const html = await text(); + const app = new DevApp({ fs, root }); + try { + await app.load(); + const request = new Request(app.url('/')); + + const response = await app.render(request); + expect(response.status).to.equal(200); + + const html = await response.text(); const $ = cheerio.load(html); - expect(res.statusCode).to.equal(200); + expect($('h1')).to.have.a.lengthOf(1); - }); + } finally { + await app.close(); + } }); it('HMR only short circuits on previously cached modules', async () => { @@ -60,13 +65,14 @@ describe('dev container', () => { root ); - await runInContainer({ fs, root }, async (container) => { - let r = createRequestAndResponse({ - method: 'GET', - url: '/', - }); - container.handle(r.req, r.res); - let html = await r.text(); + const app = new DevApp({ fs, root }); + try { + await app.load(); + + let request = new Request(app.url('/')); + let response = await app.render(request); + + let html = await response.text(); let $ = cheerio.load(html); expect($('body.one')).to.have.a.lengthOf(1); @@ -76,7 +82,7 @@ describe('dev container', () => {

{Astro.props.title}

` ); - triggerFSEvent(container, fs, '/src/components/Header.astro', 'change'); + app.fileChanged('/src/components/Header.astro'); fs.writeFileFromRootSync( '/src/pages/index.astro', @@ -93,18 +99,18 @@ describe('dev container', () => { ` ); - triggerFSEvent(container, fs, '/src/pages/index.astro', 'change'); + app.fileChanged('/src/pages/index.astro'); - r = createRequestAndResponse({ - method: 'GET', - url: '/', - }); - container.handle(r.req, r.res); - html = await r.text(); + request = new Request(app.url('/')); + response = await app.render(request); + html = await response.text(); $ = cheerio.load(html); expect($('body.one')).to.have.a.lengthOf(0); expect($('body.two')).to.have.a.lengthOf(1); - }); + + } finally { + await app.close(); + } }); it('Allows dynamic segments in injected routes', async () => {