Skip to content

Commit

Permalink
Merge branch 'main' into experimental-key-error
Browse files Browse the repository at this point in the history
  • Loading branch information
TheOtterlord authored May 15, 2023
2 parents 81163d1 + f994ebd commit 289f398
Show file tree
Hide file tree
Showing 61 changed files with 600 additions and 178 deletions.
5 changes: 0 additions & 5 deletions .changeset/clean-carpets-battle.md

This file was deleted.

5 changes: 5 additions & 0 deletions .changeset/eleven-hotels-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/rss': patch
---

Fix pubDate schema tranformation
5 changes: 5 additions & 0 deletions .changeset/fifty-guests-remember.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/vercel': patch
---

Correctly handle analytics id where present
5 changes: 5 additions & 0 deletions .changeset/fresh-baboons-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Escape closing script tag with `define:vars`
5 changes: 0 additions & 5 deletions .changeset/friendly-poets-breathe.md

This file was deleted.

5 changes: 0 additions & 5 deletions .changeset/hungry-spies-kick.md

This file was deleted.

5 changes: 4 additions & 1 deletion packages/astro-rss/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { z } from 'astro/zod';

export const rssSchema = z.object({
title: z.string(),
pubDate: z.union([z.string(), z.number(), z.date()]).transform((value) => new Date(value)),
pubDate: z
.union([z.string(), z.number(), z.date()])
.transform((value) => new Date(value))
.refine((value) => !isNaN(value.getTime())),
description: z.string().optional(),
customData: z.string().optional(),
draft: z.boolean().optional(),
Expand Down
13 changes: 13 additions & 0 deletions packages/astro-rss/test/rss.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import rss from '../dist/index.js';
import { rssSchema } from '../dist/schema.js';
import chai from 'chai';
import chaiPromises from 'chai-as-promised';
import chaiXml from 'chai-xml';
Expand Down Expand Up @@ -195,4 +196,16 @@ describe('rss', () => {

chai.expect(body).xml.to.equal(validXmlResult);
});

it('should fail when an invalid date string is provided', async () => {
const res = rssSchema.safeParse({
title: phpFeedItem.title,
pubDate: 'invalid date',
description: phpFeedItem.description,
link: phpFeedItem.link,
});

chai.expect(res.success).to.be.false;
chai.expect(res.error.issues[0].path[0]).to.equal('pubDate');
});
});
16 changes: 16 additions & 0 deletions packages/astro/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
# astro

## 2.4.5

### Patch Changes

- [#7000](https://github.com/withastro/astro/pull/7000) [`c87d42e76`](https://github.com/withastro/astro/commit/c87d42e766d02db5352671cbf074dd637bdb23e0) Thanks [@craigjennings11](https://github.com/craigjennings11)! - Remove 'paths' requirement for tsconfig path aliasing

- [#7055](https://github.com/withastro/astro/pull/7055) [`4f1073a6a`](https://github.com/withastro/astro/commit/4f1073a6a4f3e5a4fc9df96a2ae59f2e929703fe) Thanks [@Princesseuh](https://github.com/Princesseuh)! - Fix astro:assets interfering with SSR query params ending with image extensions

## 2.4.4

### Patch Changes

- [#7047](https://github.com/withastro/astro/pull/7047) [`48395c815`](https://github.com/withastro/astro/commit/48395c81522f7527126699c4f185f7b4488a4b9a) Thanks [@Princesseuh](https://github.com/Princesseuh)! - Fix `/_image` endpoint not being prefixed with the `base` path in build SSR

- [#6916](https://github.com/withastro/astro/pull/6916) [`630f8c8ef`](https://github.com/withastro/astro/commit/630f8c8ef68fedfa393899c13a072e50145895e8) Thanks [@bholmesdev](https://github.com/bholmesdev)! - Add fast lookups for content collection entries when using `getEntryBySlug()`. This generates a lookup map to ensure O(1) retrieval.

## 2.4.3

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "astro",
"version": "2.4.3",
"version": "2.4.5",
"description": "Astro is a modern site builder with web best practices, performance, and DX front-of-mind.",
"type": "module",
"author": "withastro",
Expand Down
10 changes: 8 additions & 2 deletions packages/astro/src/assets/vite-plugin-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import type * as vite from 'vite';
import { normalizePath } from 'vite';
import type { AstroPluginOptions, ImageTransform } from '../@types/astro';
import { error } from '../core/logger/core.js';
import { appendForwardSlash, joinPaths, prependForwardSlash } from '../core/path.js';
import {
appendForwardSlash,
joinPaths,
prependForwardSlash,
removeQueryString,
} from '../core/path.js';
import { VIRTUAL_MODULE_ID, VIRTUAL_SERVICE_ID } from './consts.js';
import { isESMImportedImage } from './internal.js';
import { isLocalService } from './services/service.js';
Expand Down Expand Up @@ -228,7 +233,8 @@ export default function assets({
resolvedConfig = viteConfig;
},
async load(id) {
if (/\.(jpeg|jpg|png|tiff|webp|gif|svg)$/.test(id)) {
const cleanedUrl = removeQueryString(id);
if (/\.(jpeg|jpg|png|tiff|webp|gif|svg)$/.test(cleanedUrl)) {
const meta = await emitESMImage(id, this.meta.watchMode, this.emitFile, settings);
return `export default ${JSON.stringify(meta)}`;
}
Expand Down
5 changes: 4 additions & 1 deletion packages/astro/src/runtime/server/render/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ export function defineScriptVars(vars: Record<any, any>) {
for (const [key, value] of Object.entries(vars)) {
// Use const instead of let as let global unsupported with Safari
// https://stackoverflow.com/questions/29194024/cant-use-let-keyword-in-safari-javascript
output += `const ${toIdent(key)} = ${JSON.stringify(value)};\n`;
output += `const ${toIdent(key)} = ${JSON.stringify(value).replace(
/<\/script>/g,
'\\x3C/script>'
)};\n`;
}
return markHTMLString(output);
}
Expand Down
44 changes: 23 additions & 21 deletions packages/astro/src/vite-plugin-config-alias/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,36 @@ const getConfigAlias = (settings: AstroSettings): Alias[] | null => {
if (!tsConfig || !tsConfigPath || !tsConfig.compilerOptions) return null;

const { baseUrl, paths } = tsConfig.compilerOptions;
if (!baseUrl || !paths) return null;
if (!baseUrl) return null;

// resolve the base url from the configuration file directory
const resolvedBaseUrl = path.resolve(path.dirname(tsConfigPath), baseUrl);

const aliases: Alias[] = [];

// compile any alias expressions and push them to the list
for (const [alias, values] of Object.entries(paths)) {
/** Regular Expression used to match a given path. */
const find = new RegExp(
`^${[...alias]
.map((segment) =>
segment === '*' ? '(.+)' : segment.replace(/[\\^$*+?.()|[\]{}]/, '\\$&')
)
.join('')}$`
);

/** Internal index used to calculate the matching id in a replacement. */
let matchId = 0;

for (const value of values) {
/** String used to replace a matched path. */
const replacement = [...normalizePath(path.resolve(resolvedBaseUrl, value))]
.map((segment) => (segment === '*' ? `$${++matchId}` : segment === '$' ? '$$' : segment))
.join('');

aliases.push({ find, replacement });
if (paths) {
for (const [alias, values] of Object.entries(paths)) {
/** Regular Expression used to match a given path. */
const find = new RegExp(
`^${[...alias]
.map((segment) =>
segment === '*' ? '(.+)' : segment.replace(/[\\^$*+?.()|[\]{}]/, '\\$&')
)
.join('')}$`
);

/** Internal index used to calculate the matching id in a replacement. */
let matchId = 0;

for (const value of values) {
/** String used to replace a matched path. */
const replacement = [...normalizePath(path.resolve(resolvedBaseUrl, value))]
.map((segment) => (segment === '*' ? `$${++matchId}` : segment === '$' ? '$$' : segment))
.join('');

aliases.push({ find, replacement });
}
}
}

Expand Down
123 changes: 123 additions & 0 deletions packages/astro/test/alias-tsconfig-baseurl-only.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { expect } from 'chai';
import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';

describe('Aliases with tsconfig.json - baseUrl only', () => {
let fixture;

/**
* @param {string} html
* @returns {string[]}
*/
function getLinks(html) {
let $ = cheerio.load(html);
let out = [];
$('link[rel=stylesheet]').each((i, el) => {
out.push($(el).attr('href'));
});
return out;
}

/**
* @param {string} href
* @returns {Promise<{ href: string; css: string; }>}
*/
async function getLinkContent(href, f = fixture) {
const css = await f.readFile(href);
return { href, css };
}

before(async () => {
fixture = await loadFixture({
root: './fixtures/alias-tsconfig-baseurl-only/',
});
});

describe('dev', () => {
let devServer;

before(async () => {
devServer = await fixture.startDevServer();
});

after(async () => {
await devServer.stop();
});

it('can load client components', async () => {
const html = await fixture.fetch('/').then((res) => res.text());
const $ = cheerio.load(html);

// Should render aliased element
expect($('#client').text()).to.equal('test');

const scripts = $('script').toArray();
expect(scripts.length).to.be.greaterThan(0);
});

it('can load via baseUrl', async () => {
const html = await fixture.fetch('/').then((res) => res.text());
const $ = cheerio.load(html);

expect($('#foo').text()).to.equal('foo');
expect($('#constants-foo').text()).to.equal('foo');
expect($('#constants-index').text()).to.equal('index');
});

it('works in css @import', async () => {
const html = await fixture.fetch('/').then((res) => res.text());
// imported css should be bundled
expect(html).to.include('#style-red');
expect(html).to.include('#style-blue');
});

it('works in components', async () => {
const html = await fixture.fetch('/').then((res) => res.text());
const $ = cheerio.load(html);

expect($('#alias').text()).to.equal('foo');
});
});

describe('build', () => {
before(async () => {
await fixture.build();
});

it('can load client components', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);

// Should render aliased element
expect($('#client').text()).to.equal('test');

const scripts = $('script').toArray();
expect(scripts.length).to.be.greaterThan(0);
});

it('can load via baseUrl', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);

expect($('#foo').text()).to.equal('foo');
expect($('#constants-foo').text()).to.equal('foo');
expect($('#constants-index').text()).to.equal('index');
});

it('works in css @import', async () => {
const html = await fixture.readFile('/index.html');
const content = await Promise.all(getLinks(html).map((href) => getLinkContent(href)));
const [{ css }] = content;
// imported css should be bundled
expect(css).to.include('#style-red');
expect(css).to.include('#style-blue');
});

it('works in components', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);

expect($('#alias').text()).to.equal('foo');
});
});
});
7 changes: 5 additions & 2 deletions packages/astro/test/astro-directives.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('Directives', async () => {
const html = await fixture.readFile('/define-vars/index.html');
const $ = cheerio.load(html);

expect($('script')).to.have.lengthOf(3);
expect($('script')).to.have.lengthOf(4);

let i = 0;
for (const script of $('script').toArray()) {
Expand All @@ -24,9 +24,12 @@ describe('Directives', async () => {
if (i < 2) {
// Inline defined variables
expect($(script).toString()).to.include('const foo = "bar"');
} else {
} else if (i < 3) {
// Convert invalid keys to valid identifiers
expect($(script).toString()).to.include('const dashCase = "bar"');
} else {
// Closing script tags in strings are escaped
expect($(script).toString()).to.include('const bar = "<script>bar\\x3C/script>"');
}
i++;
}
Expand Down
25 changes: 25 additions & 0 deletions packages/astro/test/core-image.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,31 @@ describe('astro:image', () => {
});
});

describe('dev ssr', () => {
let devServer;
before(async () => {
fixture = await loadFixture({
root: './fixtures/core-image-ssr/',
output: 'server',
adapter: testAdapter(),
experimental: {
assets: true,
},
});
devServer = await fixture.startDevServer();
});

after(async () => {
await devServer.stop();
});

it('does not interfere with query params', async () => {
let res = await fixture.fetch('/api?src=image.png');
const html = await res.text();
expect(html).to.equal('An image: "image.png"');
});
});

describe('prod ssr', () => {
before(async () => {
fixture = await loadFixture({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'astro/config';
import svelte from '@astrojs/svelte';

// https://astro.build/config
export default defineConfig({
integrations: [svelte()],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "@test/aliases-tsconfig-baseurl-only",
"version": "0.0.0",
"private": true,
"dependencies": {
"@astrojs/svelte": "workspace:*",
"astro": "workspace:*",
"svelte": "^3.48.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<script>
import { foo } from 'utils/constants';
</script>
<div id="alias">{foo}</div>
Loading

0 comments on commit 289f398

Please sign in to comment.