From 7b0fbd731e760283469d95f9b61c76dc31eb4482 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Mon, 11 Apr 2022 02:23:17 +0100 Subject: [PATCH] Fix SSR static build public file copying. fixes #3016 (#3037) * Fix SSR static build public file copying * chore: update lockfile * remove dirname and use URL constructor * Cleanup test and actually test what it says it tests Co-authored-by: Nate Moore --- .changeset/metal-grapes-tickle.md | 5 +++++ packages/astro/src/core/build/static-build.ts | 9 ++++----- .../test/fixtures/static build SSR/astro.config.mjs | 6 ++++++ .../test/fixtures/static build SSR/package.json | 9 +++++++++ .../test/fixtures/static build SSR/public/asset.txt | 1 + .../static build SSR/public/nested/asset2.txt | 1 + packages/astro/test/static-build.test.js | 12 ++++++++++++ pnpm-lock.yaml | 10 ++++++++++ 8 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 .changeset/metal-grapes-tickle.md create mode 100644 packages/astro/test/fixtures/static build SSR/astro.config.mjs create mode 100644 packages/astro/test/fixtures/static build SSR/package.json create mode 100644 packages/astro/test/fixtures/static build SSR/public/asset.txt create mode 100644 packages/astro/test/fixtures/static build SSR/public/nested/asset2.txt diff --git a/.changeset/metal-grapes-tickle.md b/.changeset/metal-grapes-tickle.md new file mode 100644 index 000000000..101dc5052 --- /dev/null +++ b/.changeset/metal-grapes-tickle.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix SSR static build public files copying diff --git a/packages/astro/src/core/build/static-build.ts b/packages/astro/src/core/build/static-build.ts index bddf4ceca..5a49c5792 100644 --- a/packages/astro/src/core/build/static-build.ts +++ b/packages/astro/src/core/build/static-build.ts @@ -253,15 +253,14 @@ async function copyFiles(fromFolder: URL, toFolder: URL) { cwd: fileURLToPath(fromFolder), }); - // Make the directory - await fs.promises.mkdir(toFolder, { recursive: true }); - await Promise.all( files.map(async (filename) => { const from = new URL(filename, fromFolder); const to = new URL(filename, toFolder); - return fs.promises.copyFile(from, to); - }) + const lastFolder = new URL('./', to) + return fs.promises.mkdir(lastFolder, {recursive: true}) + .then(()=>fs.promises.copyFile(from, to) ) + }) ); } diff --git a/packages/astro/test/fixtures/static build SSR/astro.config.mjs b/packages/astro/test/fixtures/static build SSR/astro.config.mjs new file mode 100644 index 000000000..cf1980887 --- /dev/null +++ b/packages/astro/test/fixtures/static build SSR/astro.config.mjs @@ -0,0 +1,6 @@ +import { defineConfig } from 'astro/config'; +import nodejs from '@astrojs/node'; + +export default defineConfig({ + adapter: nodejs(), +}); diff --git a/packages/astro/test/fixtures/static build SSR/package.json b/packages/astro/test/fixtures/static build SSR/package.json new file mode 100644 index 000000000..658d40fa9 --- /dev/null +++ b/packages/astro/test/fixtures/static build SSR/package.json @@ -0,0 +1,9 @@ +{ + "name": "@test/static-build", + "version": "0.0.0", + "dependencies": { + "@astrojs/node": "workspace:*", + "@test/static-build-pkg": "workspace:*", + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/static build SSR/public/asset.txt b/packages/astro/test/fixtures/static build SSR/public/asset.txt new file mode 100644 index 000000000..7d40b559f --- /dev/null +++ b/packages/astro/test/fixtures/static build SSR/public/asset.txt @@ -0,0 +1 @@ +test asset diff --git a/packages/astro/test/fixtures/static build SSR/public/nested/asset2.txt b/packages/astro/test/fixtures/static build SSR/public/nested/asset2.txt new file mode 100644 index 000000000..7d40b559f --- /dev/null +++ b/packages/astro/test/fixtures/static build SSR/public/nested/asset2.txt @@ -0,0 +1 @@ +test asset diff --git a/packages/astro/test/static-build.test.js b/packages/astro/test/static-build.test.js index cc6436138..994658787 100644 --- a/packages/astro/test/static-build.test.js +++ b/packages/astro/test/static-build.test.js @@ -168,3 +168,15 @@ describe('Static build', () => { expect(found).to.equal(true, 'Found the log message'); }); }); + +describe('Static build SSR', () => { + + it('Copies public files', async () => { + const fixture = await loadFixture({ + root: './fixtures/static build SSR/', + }); + await fixture.build() + const asset = await fixture.readFile('/client/nested/asset2.txt'); + }); + +}); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ee4bb073c..190872d7d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1102,6 +1102,16 @@ importers: '@test/static-build-pkg': link:pkg astro: link:../../.. + packages/astro/test/fixtures/static build SSR: + specifiers: + '@astrojs/node': workspace:* + '@test/static-build-pkg': workspace:* + astro: workspace:* + dependencies: + '@astrojs/node': link:../../../../integrations/node + '@test/static-build-pkg': link:../static build/pkg + astro: link:../../.. + packages/astro/test/fixtures/static build/pkg: specifiers: {}