Remove some more containerisms
This commit is contained in:
parent
e12e1e61c1
commit
48efa4beda
2 changed files with 55 additions and 27 deletions
|
@ -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<CreateContainerParams> & {
|
||||
|
@ -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<Response> {
|
||||
if(!this.loaded) {
|
||||
await this.load();
|
||||
|
|
|
@ -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', () => {
|
|||
<h1>{Astro.props.title}</h1>
|
||||
`
|
||||
);
|
||||
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', () => {
|
|||
</html>
|
||||
`
|
||||
);
|
||||
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 () => {
|
||||
|
|
Loading…
Reference in a new issue