Implements import.meta.request (#46)
This adds `import.meta.request` to pages (not components).
This commit is contained in:
parent
d5b15a3851
commit
7c10d563f2
4 changed files with 37 additions and 4 deletions
|
@ -1,12 +1,12 @@
|
|||
import type { LogOptions } from '../logger.js';
|
||||
import type { AstroConfig } from '../@types/astro';
|
||||
import type { AstroConfig, CompileResult, TransformResult } from '../@types/astro';
|
||||
|
||||
import path from 'path';
|
||||
import micromark from 'micromark';
|
||||
import gfmSyntax from 'micromark-extension-gfm';
|
||||
import matter from 'gray-matter';
|
||||
import gfmHtml from 'micromark-extension-gfm/html.js';
|
||||
import { CompileResult, TransformResult } from '../@types/astro';
|
||||
|
||||
import { parse } from '../parser/index.js';
|
||||
import { createMarkdownHeadersCollector } from '../micromark-collect-headers.js';
|
||||
import { encodeMarkdown } from '../micromark-encode.js';
|
||||
|
@ -130,13 +130,12 @@ export default __render;
|
|||
// triggered by loading a component directly by URL.
|
||||
export async function __renderPage({request, children, props}) {
|
||||
const currentChild = {
|
||||
setup: typeof setup === 'undefined' ? (passthrough) => passthrough : setup,
|
||||
layout: typeof __layout === 'undefined' ? undefined : __layout,
|
||||
content: typeof __content === 'undefined' ? undefined : __content,
|
||||
__render,
|
||||
};
|
||||
|
||||
await currentChild.setup({request});
|
||||
import.meta.request = request;
|
||||
const childBodyResult = await currentChild.__render(props, children);
|
||||
|
||||
// find layout, if one was given.
|
||||
|
|
19
test/astro-request.test.js
Normal file
19
test/astro-request.test.js
Normal file
|
@ -0,0 +1,19 @@
|
|||
import { suite } from 'uvu';
|
||||
import * as assert from 'uvu/assert';
|
||||
import { doc } from './test-utils.js';
|
||||
import { setup } from './helpers.js';
|
||||
|
||||
const Request = suite('import.meta.request');
|
||||
|
||||
setup(Request, './fixtures/astro-request');
|
||||
|
||||
Request('import.meta.request available', async (context) => {
|
||||
const result = await context.runtime.load('/');
|
||||
|
||||
assert.equal(result.statusCode, 200);
|
||||
|
||||
const $ = doc(result.contents);
|
||||
assert.equal($('h1').text(), '/');
|
||||
});
|
||||
|
||||
Request.run();
|
5
test/fixtures/astro-request/astro.config.mjs
vendored
Normal file
5
test/fixtures/astro-request/astro.config.mjs
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
export default {
|
||||
projectRoot: '.',
|
||||
astroRoot: './astro',
|
||||
dist: './_site'
|
||||
};
|
10
test/fixtures/astro-request/astro/pages/index.astro
vendored
Normal file
10
test/fixtures/astro-request/astro/pages/index.astro
vendored
Normal file
|
@ -0,0 +1,10 @@
|
|||
---
|
||||
let path = import.meta.request.path;
|
||||
---
|
||||
|
||||
<html>
|
||||
<head><title>Test</title></head>
|
||||
<body>
|
||||
<h1>{path}</h1>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in a new issue