From 61c8c9d37cf6712de2dcf61f9b158a5e3f5c0e8f Mon Sep 17 00:00:00 2001 From: Moustapha HappyDev Date: Thu, 4 Jan 2024 09:59:10 +0000 Subject: [PATCH 01/10] chore: try new compiler version --- packages/astro/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/astro/package.json b/packages/astro/package.json index aa585b45ea3b..608c0197e852 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -114,7 +114,7 @@ "test:e2e:match": "playwright test -g" }, "dependencies": { - "@astrojs/compiler": "^2.3.4", + "@astrojs/compiler": "0.0.0-dynamic-slot-names-20240103232344", "@astrojs/internal-helpers": "workspace:*", "@astrojs/markdown-remark": "workspace:*", "@astrojs/telemetry": "workspace:*", From 50fdbec889277ceb8ae36b98e6734b384f0f8c6f Mon Sep 17 00:00:00 2001 From: Moustapha HappyDev Date: Thu, 4 Jan 2024 09:59:19 +0000 Subject: [PATCH 02/10] update mergeSlots function --- packages/astro/src/runtime/server/index.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/astro/src/runtime/server/index.ts b/packages/astro/src/runtime/server/index.ts index 81d05987adb8..ff76ab8e1a91 100644 --- a/packages/astro/src/runtime/server/index.ts +++ b/packages/astro/src/runtime/server/index.ts @@ -45,8 +45,14 @@ import { addAttribute, Renderer } from './render/index.js'; export function mergeSlots(...slotted: unknown[]) { const slots: Record any> = {}; - for (const slot of slotted) { + for (let i = 0; i < slotted.length; i++) { + const slot = slotted[i]; if (!slot) continue; + if (Array.isArray(slot)) { + slotted.splice(i, 1, ...slot); + i--; + continue; + } if (typeof slot === 'object') { Object.assign(slots, slot); } else if (typeof slot === 'function') { From 429c2ee382214766bad7b34ca7ba3d6dd67f1265 Mon Sep 17 00:00:00 2001 From: Moustapha HappyDev Date: Thu, 4 Jan 2024 10:02:35 +0000 Subject: [PATCH 03/10] chore: update lock file --- pnpm-lock.yaml | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7902ad244ac2..cf3dba2e476f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -489,8 +489,8 @@ importers: packages/astro: dependencies: '@astrojs/compiler': - specifier: ^2.3.4 - version: 2.3.4 + specifier: 0.0.0-dynamic-slot-names-20240103232344 + version: 0.0.0-dynamic-slot-names-20240103232344 '@astrojs/internal-helpers': specifier: workspace:* version: link:../internal-helpers @@ -5253,12 +5253,17 @@ packages: sisteransi: 1.0.5 dev: false + /@astrojs/compiler@0.0.0-dynamic-slot-names-20240103232344: + resolution: {integrity: sha512-NHSC4fHnq23VA4gu+mJ+rqF/EHCjw1EZR2zqzJXDk3Q8ThDD5TAZOI/jKxwKzDZKIk9jyWdqDMMSUw99SjhwyQ==} + dev: false + /@astrojs/compiler@1.8.2: resolution: {integrity: sha512-o/ObKgtMzl8SlpIdzaxFnt7SATKPxu4oIP/1NL+HDJRzxfJcAkOTAb/ZKMRyULbz4q+1t2/DAebs2Z1QairkZw==} dev: true /@astrojs/compiler@2.3.4: resolution: {integrity: sha512-33/YtWoBCE0cBUNy1kh78FCDXBoBANX87ShgATlAHECYbG2+buNTAgq4Xgz4t5NgnEHPN21GIBC2Mvvwisoutw==} + dev: true /@astrojs/language-server@2.5.5(prettier-plugin-astro@0.12.3)(prettier@3.1.1)(typescript@5.2.2): resolution: {integrity: sha512-hk7a8S7bpf//BOA6mMjiYqi/eiYtGPfUfw59eVXdutdRFdwDHtu4jcsLu43ZaId56pAcE8qFjIvJxySvzcxiUA==} From c9fabf5f3569e773f1990ae0dc960cc7f6fa488e Mon Sep 17 00:00:00 2001 From: Moustapha HappyDev Date: Thu, 4 Jan 2024 20:36:27 +0000 Subject: [PATCH 04/10] add tests --- packages/astro/test/astro-slots.test.js | 20 ++++++++++++++++ .../astro-slots/src/pages/dynamic-for.astro | 24 +++++++++++++++++++ .../astro-slots/src/pages/dynamic-map.astro | 17 +++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 packages/astro/test/fixtures/astro-slots/src/pages/dynamic-for.astro create mode 100644 packages/astro/test/fixtures/astro-slots/src/pages/dynamic-map.astro diff --git a/packages/astro/test/astro-slots.test.js b/packages/astro/test/astro-slots.test.js index 69a0025e1f27..7610e73059b9 100644 --- a/packages/astro/test/astro-slots.test.js +++ b/packages/astro/test/astro-slots.test.js @@ -30,6 +30,26 @@ describe('Slots', () => { expect($('#default').text().trim()).to.equal('Default'); }); + it('Dynamic named slots work with map work', async () => { + const html = await fixture.readFile('/dynamic-map/index.html'); + const $ = cheerio.load(html); + + expect($('#a').text().trim()).to.equal('A'); + expect($('#b').text().trim()).to.equal('B'); + expect($('#c').text().trim()).to.equal('C'); + expect($('#default').text().trim()).to.equal('Default'); + }); + + it('Dynamic named slots work with for loop', async () => { + const html = await fixture.readFile('/dynamic-for/index.html'); + const $ = cheerio.load(html); + + expect($('#a').text().trim()).to.equal('A'); + expect($('#b').text().trim()).to.equal('B'); + expect($('#c').text().trim()).to.equal('C'); + expect($('#default').text().trim()).to.equal('Default'); + }); + it('Conditional named slots work', async () => { const html = await fixture.readFile('/conditional/index.html'); const $ = cheerio.load(html); diff --git a/packages/astro/test/fixtures/astro-slots/src/pages/dynamic-for.astro b/packages/astro/test/fixtures/astro-slots/src/pages/dynamic-for.astro new file mode 100644 index 000000000000..f7e48735dfc5 --- /dev/null +++ b/packages/astro/test/fixtures/astro-slots/src/pages/dynamic-for.astro @@ -0,0 +1,24 @@ +--- +import Slotted from '../components/Slotted.astro'; + +const slotNames = ['a', 'b', 'c'] +--- + + + + + + + + {()=>{ + const slots:any[] = []; + for (const slotName of slotNames) { + slots.push({slotName.toUpperCase()}); + } + return slots; + } + } + Default + + + diff --git a/packages/astro/test/fixtures/astro-slots/src/pages/dynamic-map.astro b/packages/astro/test/fixtures/astro-slots/src/pages/dynamic-map.astro new file mode 100644 index 000000000000..3917d73dd860 --- /dev/null +++ b/packages/astro/test/fixtures/astro-slots/src/pages/dynamic-map.astro @@ -0,0 +1,17 @@ +--- +import Slotted from '../components/Slotted.astro'; + +const slots = ['a', 'b', 'c'] +--- + + + + + + + + {slots.map((slotName)=>{slotName.toUpperCase()})} + Default + + + From 538ce34cbf1b49b6a73064fc812a822a8b4ca55f Mon Sep 17 00:00:00 2001 From: Moustapha HappyDev Date: Tue, 9 Jan 2024 13:21:06 +0000 Subject: [PATCH 05/10] simplify `mergeSlots` code --- packages/astro/src/runtime/server/index.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/astro/src/runtime/server/index.ts b/packages/astro/src/runtime/server/index.ts index ff76ab8e1a91..e92f33c842a7 100644 --- a/packages/astro/src/runtime/server/index.ts +++ b/packages/astro/src/runtime/server/index.ts @@ -45,13 +45,10 @@ import { addAttribute, Renderer } from './render/index.js'; export function mergeSlots(...slotted: unknown[]) { const slots: Record any> = {}; - for (let i = 0; i < slotted.length; i++) { - const slot = slotted[i]; + for (let slot of slotted) { if (!slot) continue; if (Array.isArray(slot)) { - slotted.splice(i, 1, ...slot); - i--; - continue; + slot = mergeSlots(...slot); } if (typeof slot === 'object') { Object.assign(slots, slot); From c08d81f48bac1de96cb7a88ac08b2440d8ba1634 Mon Sep 17 00:00:00 2001 From: Moustapha HappyDev Date: Tue, 9 Jan 2024 19:45:19 +0000 Subject: [PATCH 06/10] update compiler version --- packages/astro/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/astro/package.json b/packages/astro/package.json index 0b3753bf0736..7fcceda43939 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -114,7 +114,7 @@ "test:e2e:match": "playwright test -g" }, "dependencies": { - "@astrojs/compiler": "0.0.0-dynamic-slot-names-20240103232344", + "@astrojs/compiler": "0.0.0-dynamic-slot-names-20240109194342", "@astrojs/internal-helpers": "workspace:*", "@astrojs/markdown-remark": "workspace:*", "@astrojs/telemetry": "workspace:*", From 04939ac8e31993284cc3086b7e4bec8759b4058c Mon Sep 17 00:00:00 2001 From: Moustapha HappyDev Date: Tue, 9 Jan 2024 19:45:51 +0000 Subject: [PATCH 07/10] update lock file --- pnpm-lock.yaml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c3539ad83a45..bebd4fb98df0 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -489,8 +489,8 @@ importers: packages/astro: dependencies: '@astrojs/compiler': - specifier: 0.0.0-dynamic-slot-names-20240103232344 - version: 0.0.0-dynamic-slot-names-20240103232344 + specifier: 0.0.0-dynamic-slot-names-20240109194342 + version: 0.0.0-dynamic-slot-names-20240109194342 '@astrojs/internal-helpers': specifier: workspace:* version: link:../internal-helpers @@ -5289,8 +5289,8 @@ packages: sisteransi: 1.0.5 dev: false - /@astrojs/compiler@0.0.0-dynamic-slot-names-20240103232344: - resolution: {integrity: sha512-NHSC4fHnq23VA4gu+mJ+rqF/EHCjw1EZR2zqzJXDk3Q8ThDD5TAZOI/jKxwKzDZKIk9jyWdqDMMSUw99SjhwyQ==} + /@astrojs/compiler@0.0.0-dynamic-slot-names-20240109194342: + resolution: {integrity: sha512-PpnzlE34xaFbcEuQhSUmqo0IMWXL1m1Z/0sX8isHbwidYUI4nYx57AHzuNQVSZMQ3Ccd3AyksO39P+FxFo7lfQ==} dev: false /@astrojs/compiler@1.8.2: From e84c01eb77089cd4ec4d7ef02f9c9c91133766c7 Mon Sep 17 00:00:00 2001 From: Moustapha HappyDev Date: Fri, 12 Jan 2024 09:20:31 +0000 Subject: [PATCH 08/10] fix failing test We were expecting the source code to produce an error, but in 2.4.0 of the compiler, that generates valid code --- packages/astro/e2e/errors.test.js | 2 +- .../e2e/fixtures/errors/src/pages/astro-syntax-error.astro | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/astro/e2e/errors.test.js b/packages/astro/e2e/errors.test.js index 4cb1b02fb5c3..825fc7a9224c 100644 --- a/packages/astro/e2e/errors.test.js +++ b/packages/astro/e2e/errors.test.js @@ -25,7 +25,7 @@ test.describe('Error display', () => { await page.goto(astro.resolveUrl('/astro-syntax-error'), { waitUntil: 'networkidle' }); const message = (await getErrorOverlayContent(page)).message; - expect(message).toMatch('Unexpected "}"'); + expect(message).toMatch('Unexpected "while"'); await Promise.all([ // Wait for page reload diff --git a/packages/astro/e2e/fixtures/errors/src/pages/astro-syntax-error.astro b/packages/astro/e2e/fixtures/errors/src/pages/astro-syntax-error.astro index 4fd7b10a2a74..9747c4f8c7cd 100644 --- a/packages/astro/e2e/fixtures/errors/src/pages/astro-syntax-error.astro +++ b/packages/astro/e2e/fixtures/errors/src/pages/astro-syntax-error.astro @@ -1 +1 @@ -

{// comment +

{while (true){}}

From 9ac9b06d9a5cb7bbf96dffc86eb2275cf8d62d14 Mon Sep 17 00:00:00 2001 From: Moustapha HappyDev Date: Fri, 12 Jan 2024 21:02:54 +0000 Subject: [PATCH 09/10] chore: changeset --- .changeset/red-carrots-fail.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/red-carrots-fail.md diff --git a/.changeset/red-carrots-fail.md b/.changeset/red-carrots-fail.md new file mode 100644 index 000000000000..caef47017deb --- /dev/null +++ b/.changeset/red-carrots-fail.md @@ -0,0 +1,5 @@ +--- +"astro": patch +--- + +Adds support for dynamic slot names From d2438bce1d6c1d2bdcf5d21e74d75211e41ef352 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Thu, 18 Jan 2024 13:28:37 -0600 Subject: [PATCH 10/10] chore: update deps --- packages/astro/package.json | 2 +- pnpm-lock.yaml | 15 +++++---------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/packages/astro/package.json b/packages/astro/package.json index d43409059981..aeadbc8893f8 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -114,7 +114,7 @@ "test:e2e:match": "playwright test -g" }, "dependencies": { - "@astrojs/compiler": "0.0.0-dynamic-slot-names-20240109194342", + "@astrojs/compiler": "^2.5.0", "@astrojs/internal-helpers": "workspace:*", "@astrojs/markdown-remark": "workspace:*", "@astrojs/telemetry": "workspace:*", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index adcb602887c4..521a110880b2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -489,8 +489,8 @@ importers: packages/astro: dependencies: '@astrojs/compiler': - specifier: 0.0.0-dynamic-slot-names-20240109194342 - version: 0.0.0-dynamic-slot-names-20240109194342 + specifier: ^2.5.0 + version: 2.5.0 '@astrojs/internal-helpers': specifier: workspace:* version: link:../internal-helpers @@ -5304,17 +5304,12 @@ packages: sisteransi: 1.0.5 dev: false - /@astrojs/compiler@0.0.0-dynamic-slot-names-20240109194342: - resolution: {integrity: sha512-PpnzlE34xaFbcEuQhSUmqo0IMWXL1m1Z/0sX8isHbwidYUI4nYx57AHzuNQVSZMQ3Ccd3AyksO39P+FxFo7lfQ==} - dev: false - /@astrojs/compiler@1.8.2: resolution: {integrity: sha512-o/ObKgtMzl8SlpIdzaxFnt7SATKPxu4oIP/1NL+HDJRzxfJcAkOTAb/ZKMRyULbz4q+1t2/DAebs2Z1QairkZw==} dev: true - /@astrojs/compiler@2.3.4: - resolution: {integrity: sha512-33/YtWoBCE0cBUNy1kh78FCDXBoBANX87ShgATlAHECYbG2+buNTAgq4Xgz4t5NgnEHPN21GIBC2Mvvwisoutw==} - dev: true + /@astrojs/compiler@2.5.0: + resolution: {integrity: sha512-ZDluNgMIJT+z+HJcZ6QEJ/KqaFkTkrb+Za6c6VZs8G/nb1LBErL14/iU5EVJ9yu25i4QCLweuBJ3m5df34gZJg==} /@astrojs/language-server@2.5.5(prettier-plugin-astro@0.12.3)(prettier@3.1.1)(typescript@5.2.2): resolution: {integrity: sha512-hk7a8S7bpf//BOA6mMjiYqi/eiYtGPfUfw59eVXdutdRFdwDHtu4jcsLu43ZaId56pAcE8qFjIvJxySvzcxiUA==} @@ -5328,7 +5323,7 @@ packages: prettier-plugin-astro: optional: true dependencies: - '@astrojs/compiler': 2.3.4 + '@astrojs/compiler': 2.5.0 '@jridgewell/sourcemap-codec': 1.4.15 '@volar/kit': 1.10.10(typescript@5.2.2) '@volar/language-core': 1.10.10