From ce01225a700aff5b437d46f21161e5f557050e12 Mon Sep 17 00:00:00 2001 From: Erika <3019731+Princesseuh@users.noreply.github.com> Date: Mon, 17 Oct 2022 11:29:39 -0300 Subject: [PATCH 1/5] Fix types not working properly when using `moduleResolution: 'node16'` (#5108) * Fix imports failling under `moduleResolution: node16` * Add changeset * Revert comment about Vite 4 --- .changeset/olive-jeans-shout.md | 5 +++++ packages/astro/astro-jsx.d.ts | 10 +++++----- packages/astro/config.d.ts | 2 +- packages/astro/env.d.ts | 2 +- packages/astro/package.json | 5 ++++- 5 files changed, 16 insertions(+), 8 deletions(-) create mode 100644 .changeset/olive-jeans-shout.md diff --git a/.changeset/olive-jeans-shout.md b/.changeset/olive-jeans-shout.md new file mode 100644 index 0000000000000..423b11e00fab5 --- /dev/null +++ b/.changeset/olive-jeans-shout.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix types not working properly when using `moduleResolution: 'node16'` diff --git a/packages/astro/astro-jsx.d.ts b/packages/astro/astro-jsx.d.ts index 893b92b2f78a5..aa7c0a5730ddd 100644 --- a/packages/astro/astro-jsx.d.ts +++ b/packages/astro/astro-jsx.d.ts @@ -23,12 +23,12 @@ declare namespace astroHTML.JSX { children?: Children; } - type AstroBuiltinProps = import('./dist/@types/astro').AstroBuiltinProps; - type AstroBuiltinAttributes = import('./dist/@types/astro').AstroBuiltinAttributes; - type AstroDefineVarsAttribute = import('./dist/@types/astro').AstroDefineVarsAttribute; - type AstroScriptAttributes = import('./dist/@types/astro').AstroScriptAttributes & + type AstroBuiltinProps = import('./dist/@types/astro.js').AstroBuiltinProps; + type AstroBuiltinAttributes = import('./dist/@types/astro.js').AstroBuiltinAttributes; + type AstroDefineVarsAttribute = import('./dist/@types/astro.js').AstroDefineVarsAttribute; + type AstroScriptAttributes = import('./dist/@types/astro.js').AstroScriptAttributes & AstroDefineVarsAttribute; - type AstroStyleAttributes = import('./dist/@types/astro').AstroStyleAttributes & + type AstroStyleAttributes = import('./dist/@types/astro.js').AstroStyleAttributes & AstroDefineVarsAttribute; // This is an unfortunate use of `any`, but unfortunately we can't make a type that works for every framework diff --git a/packages/astro/config.d.ts b/packages/astro/config.d.ts index fab3fe946fd3e..6e081a22429c0 100644 --- a/packages/astro/config.d.ts +++ b/packages/astro/config.d.ts @@ -1,5 +1,5 @@ type ViteUserConfig = import('vite').UserConfig; -type AstroUserConfig = import('./dist/@types/astro').AstroUserConfig; +type AstroUserConfig = import('./dist/@types/astro.js').AstroUserConfig; /** * See the full Astro Configuration API Documentation diff --git a/packages/astro/env.d.ts b/packages/astro/env.d.ts index 10a61b06f0f17..659f2972b8d05 100644 --- a/packages/astro/env.d.ts +++ b/packages/astro/env.d.ts @@ -4,7 +4,7 @@ // As such, if the typings you're trying to add should be available inside ex: React components, they should instead // be inside `client-base.d.ts` -type Astro = import('./dist/@types/astro').AstroGlobal; +type Astro = import('./dist/@types/astro.js').AstroGlobal; // We have to duplicate the description here because editors won't show the JSDoc comment from the imported type // However, they will for its properties, ex: Astro.request will show the AstroGlobal.request description diff --git a/packages/astro/package.json b/packages/astro/package.json index 805f92ac19a8f..c3549db85988d 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -37,7 +37,10 @@ "./tsconfigs/*": "./tsconfigs/*.json", "./jsx/*": "./dist/jsx/*", "./jsx-runtime": "./dist/jsx-runtime/index.js", - "./config": "./config.mjs", + "./config": { + "types": "./config.d.ts", + "default": "./config.mjs" + }, "./app": "./dist/core/app/index.js", "./app/node": "./dist/core/app/node.js", "./client/*": "./dist/runtime/client/*", From bf01c8bf67cf9e8fa6e772e9cde1919be1db28d8 Mon Sep 17 00:00:00 2001 From: Sujaykumar Date: Mon, 17 Oct 2022 19:59:53 +0530 Subject: [PATCH 2/5] Fix missing image src in config for examples docs (#5109) --- examples/docs/src/config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/docs/src/config.ts b/examples/docs/src/config.ts index b8258b1f7e6ad..8ca1b577cfaa6 100644 --- a/examples/docs/src/config.ts +++ b/examples/docs/src/config.ts @@ -6,7 +6,7 @@ export const SITE = { export const OPEN_GRAPH = { image: { - src: 'https://github.com/withastro/astro/blob/main/assets/social/banner.jpg?raw=true', + src: 'https://github.com/withastro/astro/blob/main/assets/social/banner-minimal.png?raw=true', alt: 'astro logo on a starry expanse of space,' + ' with a purple saturn-like planet floating in the right foreground', From 0db9c08a8dcfdd9fc37c247b198918557d0bd12b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8A=B1=E6=9E=9C=E5=B1=B1=E5=A4=A7=E5=9C=A3?= <316783812@qq.com> Date: Mon, 17 Oct 2022 22:32:20 +0800 Subject: [PATCH 3/5] chore: add eslint cache (#5101) --- .gitignore | 1 + package.json | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index a6fb809056205..8ce8095bcad44 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ scripts/memory/project/src/pages/ *.log package-lock.json .turbo/ +.eslintcache # ignore top-level vscode settings /.vscode/settings.json diff --git a/package.json b/package.json index 57007bd93eef8..ded8ac0779788 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "test:e2e": "cd packages/astro && pnpm playwright install && pnpm run test:e2e", "test:e2e:match": "cd packages/astro && pnpm playwright install && pnpm run test:e2e:match", "benchmark": "pnpm --filter @benchmark/simple run build && pnpm dlx concurrently -k -s first --raw \"node packages/astro/test/benchmark/simple/server.mjs\" \"pnpm dlx autocannon -c 100 -d 30 -p 10 localhost:3002/\"", - "lint": "eslint .", + "lint": "eslint --cache .", "version": "changeset version && pnpm install --no-frozen-lockfile && pnpm run format" }, "workspaces": [ From 0edfdd325932b0b493b2228e3a121d217c38a727 Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Tue, 18 Oct 2022 13:10:16 +0800 Subject: [PATCH 4/5] Ensure CLI flags override function-style server config (#5110) --- .changeset/eighty-planes-kneel.md | 5 +++++ packages/astro/src/core/config/schema.ts | 14 ++++++++++++-- .../test/fixtures/astro-basic/astro.config.mjs | 2 ++ 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 .changeset/eighty-planes-kneel.md diff --git a/.changeset/eighty-planes-kneel.md b/.changeset/eighty-planes-kneel.md new file mode 100644 index 0000000000000..5f3eec5769812 --- /dev/null +++ b/.changeset/eighty-planes-kneel.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Ensure CLI flags override function-style server config diff --git a/packages/astro/src/core/config/schema.ts b/packages/astro/src/core/config/schema.ts index 794c756cec36a..f9cddbf40623d 100644 --- a/packages/astro/src/core/config/schema.ts +++ b/packages/astro/src/core/config/schema.ts @@ -271,8 +271,18 @@ export function createRelativeSchema(cmd: string, fileProtocolRoot: URL) { .default({}), server: z.preprocess( // preprocess - (val) => - typeof val === 'function' ? val({ command: cmd === 'dev' ? 'dev' : 'preview' }) : val, + (val) => { + if (typeof val === 'function') { + const result = val({ command: cmd === 'dev' ? 'dev' : 'preview' }); + // @ts-expect-error revive attached prop added from CLI flags + if (val.port) result.port = val.port; + // @ts-expect-error revive attached prop added from CLI flags + if (val.host) result.host = val.host; + return result; + } else { + return val; + } + }, // validate z .object({ diff --git a/packages/astro/test/fixtures/astro-basic/astro.config.mjs b/packages/astro/test/fixtures/astro-basic/astro.config.mjs index 08916b1fea784..b7b7dafe6b789 100644 --- a/packages/astro/test/fixtures/astro-basic/astro.config.mjs +++ b/packages/astro/test/fixtures/astro-basic/astro.config.mjs @@ -4,4 +4,6 @@ import preact from '@astrojs/preact'; // https://astro.build/config export default defineConfig({ integrations: [preact()], + // make sure CLI flags have precedence + server: () => ({ port: 3000 }) }); From ef0c5431631665c9f6648ee5daee65a3ebf8e9ee Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Tue, 18 Oct 2022 13:16:24 +0800 Subject: [PATCH 5/5] Support spread parameters for server endpoints (#5106) --- .changeset/gold-roses-argue.md | 5 ++ .../astro/src/core/routing/manifest/create.ts | 46 ++++++++------ .../src/core/routing/manifest/generator.ts | 37 ++++++------ .../src/pages/api/catch/[...slug].json.ts | 13 ++++ .../src/pages/api/catch/[foo]-[bar].json.ts | 14 +++++ packages/astro/test/routing-priority.test.js | 60 ++++++++++++++++--- 6 files changed, 133 insertions(+), 42 deletions(-) create mode 100644 .changeset/gold-roses-argue.md create mode 100644 packages/astro/test/fixtures/routing-priority/src/pages/api/catch/[...slug].json.ts create mode 100644 packages/astro/test/fixtures/routing-priority/src/pages/api/catch/[foo]-[bar].json.ts diff --git a/.changeset/gold-roses-argue.md b/.changeset/gold-roses-argue.md new file mode 100644 index 0000000000000..05c93789fc0fd --- /dev/null +++ b/.changeset/gold-roses-argue.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Support spread parameters for server endpoints diff --git a/packages/astro/src/core/routing/manifest/create.ts b/packages/astro/src/core/routing/manifest/create.ts index c5b19bf1a5a30..6a28b33d2b7e2 100644 --- a/packages/astro/src/core/routing/manifest/create.ts +++ b/packages/astro/src/core/routing/manifest/create.ts @@ -63,23 +63,30 @@ function getParts(part: string, file: string) { function getPattern(segments: RoutePart[][], addTrailingSlash: AstroConfig['trailingSlash']) { const pathname = segments .map((segment) => { - return segment[0].spread - ? '(?:\\/(.*?))?' - : '\\/' + - segment - .map((part) => { - if (part) - return part.dynamic - ? '([^/]+?)' - : part.content - .normalize() - .replace(/\?/g, '%3F') - .replace(/#/g, '%23') - .replace(/%5B/g, '[') - .replace(/%5D/g, ']') - .replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); - }) - .join(''); + if (segment.length === 1 && segment[0].spread) { + return '(?:\\/(.*?))?'; + } else { + return ( + '\\/' + + segment + .map((part) => { + if (part.spread) { + return '(.*?)'; + } else if (part.dynamic) { + return '([^/]+?)'; + } else { + return part.content + .normalize() + .replace(/\?/g, '%3F') + .replace(/#/g, '%23') + .replace(/%5B/g, '[') + .replace(/%5D/g, ']') + .replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); + } + }) + .join('') + ); + } }) .join(''); @@ -117,7 +124,10 @@ function validateSegment(segment: string, file = '') { if (countOccurrences('[', segment) !== countOccurrences(']', segment)) { throw new Error(`Invalid route ${file} \u2014 brackets are unbalanced`); } - if (/.+\[\.\.\.[^\]]+\]/.test(segment) || /\[\.\.\.[^\]]+\].+/.test(segment)) { + if ( + (/.+\[\.\.\.[^\]]+\]/.test(segment) || /\[\.\.\.[^\]]+\].+/.test(segment)) && + file.endsWith('.astro') + ) { throw new Error(`Invalid route ${file} \u2014 rest parameter must be a standalone segment`); } } diff --git a/packages/astro/src/core/routing/manifest/generator.ts b/packages/astro/src/core/routing/manifest/generator.ts index 6df4806fd6fa1..4945ea9f13aef 100644 --- a/packages/astro/src/core/routing/manifest/generator.ts +++ b/packages/astro/src/core/routing/manifest/generator.ts @@ -8,23 +8,26 @@ export function getRouteGenerator( ) { const template = segments .map((segment) => { - return segment[0].spread - ? `/:${segment[0].content.slice(3)}(.*)?` - : '/' + - segment - .map((part) => { - if (part) - return part.dynamic - ? `:${part.content}` - : part.content - .normalize() - .replace(/\?/g, '%3F') - .replace(/#/g, '%23') - .replace(/%5B/g, '[') - .replace(/%5D/g, ']') - .replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); - }) - .join(''); + return ( + '/' + + segment + .map((part) => { + if (part.spread) { + return `:${part.content.slice(3)}(.*)?`; + } else if (part.dynamic) { + return `:${part.content}`; + } else { + return part.content + .normalize() + .replace(/\?/g, '%3F') + .replace(/#/g, '%23') + .replace(/%5B/g, '[') + .replace(/%5D/g, ']') + .replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); + } + }) + .join('') + ); }) .join(''); diff --git a/packages/astro/test/fixtures/routing-priority/src/pages/api/catch/[...slug].json.ts b/packages/astro/test/fixtures/routing-priority/src/pages/api/catch/[...slug].json.ts new file mode 100644 index 0000000000000..142b11711c0b6 --- /dev/null +++ b/packages/astro/test/fixtures/routing-priority/src/pages/api/catch/[...slug].json.ts @@ -0,0 +1,13 @@ +import type { APIRoute } from 'astro'; + +export const get: APIRoute = async ({ params }) => { + return { + body: JSON.stringify({ + path: params.slug, + }), + }; +}; + +export function getStaticPaths() { + return [{ params: { slug: 'a' } }, { params: { slug: 'b/c' } }]; +} diff --git a/packages/astro/test/fixtures/routing-priority/src/pages/api/catch/[foo]-[bar].json.ts b/packages/astro/test/fixtures/routing-priority/src/pages/api/catch/[foo]-[bar].json.ts new file mode 100644 index 0000000000000..2e66a22ae51a2 --- /dev/null +++ b/packages/astro/test/fixtures/routing-priority/src/pages/api/catch/[foo]-[bar].json.ts @@ -0,0 +1,14 @@ +import type { APIRoute } from 'astro'; + +export const get: APIRoute = async ({ params }) => { + return { + body: JSON.stringify({ + foo: params.foo, + bar: params.bar, + }), + }; +}; + +export function getStaticPaths() { + return [{ params: { foo: 'a', bar: 'b' } }]; +} diff --git a/packages/astro/test/routing-priority.test.js b/packages/astro/test/routing-priority.test.js index dba4094a22b63..586978d4f4cde 100644 --- a/packages/astro/test/routing-priority.test.js +++ b/packages/astro/test/routing-priority.test.js @@ -106,6 +106,21 @@ const routes = [ url: '/empty-slug/undefined', fourOhFour: true, }, + { + description: 'matches /api/catch/a.json to api/catch/[...slug].json.ts', + url: '/api/catch/a.json', + htmlMatch: JSON.stringify({ path: 'a' }), + }, + { + description: 'matches /api/catch/b/c.json to api/catch/[...slug].json.ts', + url: '/api/catch/b/c.json', + htmlMatch: JSON.stringify({ path: 'b/c' }), + }, + { + description: 'matches /api/catch/a-b.json to api/catch/[foo]-[bar].json.ts', + url: '/api/catch/a-b.json', + htmlMatch: JSON.stringify({ foo: 'a', bar: 'b' }), + }, ]; function appendForwardSlash(path) { @@ -123,9 +138,11 @@ describe('Routing priority', () => { await fixture.build(); }); - routes.forEach(({ description, url, fourOhFour, h1, p }) => { + routes.forEach(({ description, url, fourOhFour, h1, p, htmlMatch }) => { + const isEndpoint = htmlMatch && !h1 && !p; + it(description, async () => { - const htmlFile = `${appendForwardSlash(url)}index.html`; + const htmlFile = isEndpoint ? url : `${appendForwardSlash(url)}index.html`; if (fourOhFour) { expect(fixture.pathExists(htmlFile)).to.be.false; @@ -135,11 +152,17 @@ describe('Routing priority', () => { const html = await fixture.readFile(htmlFile); const $ = cheerioLoad(html); - expect($('h1').text()).to.equal(h1); + if (h1) { + expect($('h1').text()).to.equal(h1); + } if (p) { expect($('p').text()).to.equal(p); } + + if (htmlMatch) { + expect(html).to.equal(htmlMatch); + } }); }); }); @@ -160,7 +183,9 @@ describe('Routing priority', () => { await devServer.stop(); }); - routes.forEach(({ description, url, fourOhFour, h1, p }) => { + routes.forEach(({ description, url, fourOhFour, h1, p, htmlMatch }) => { + const isEndpoint = htmlMatch && !h1 && !p; + // checks URLs as written above it(description, async () => { const html = await fixture.fetch(url).then((res) => res.text()); @@ -171,13 +196,22 @@ describe('Routing priority', () => { return; } - expect($('h1').text()).to.equal(h1); + if (h1) { + expect($('h1').text()).to.equal(h1); + } if (p) { expect($('p').text()).to.equal(p); } + + if (htmlMatch) { + expect(html).to.equal(htmlMatch); + } }); + // skip for endpoint page test + if (isEndpoint) return; + // checks with trailing slashes, ex: '/de/' instead of '/de' it(`${description} (trailing slash)`, async () => { const html = await fixture.fetch(appendForwardSlash(url)).then((res) => res.text()); @@ -188,11 +222,17 @@ describe('Routing priority', () => { return; } - expect($('h1').text()).to.equal(h1); + if (h1) { + expect($('h1').text()).to.equal(h1); + } if (p) { expect($('p').text()).to.equal(p); } + + if (htmlMatch) { + expect(html).to.equal(htmlMatch); + } }); // checks with index.html, ex: '/de/index.html' instead of '/de' @@ -207,11 +247,17 @@ describe('Routing priority', () => { return; } - expect($('h1').text()).to.equal(h1); + if (h1) { + expect($('h1').text()).to.equal(h1); + } if (p) { expect($('p').text()).to.equal(p); } + + if (htmlMatch) { + expect(html).to.equal(htmlMatch); + } }); }); });