From a9c22994e41f92a586d8946988d29e3c62148778 Mon Sep 17 00:00:00 2001 From: wulinsheng123 <409187100@qq.com> Date: Thu, 13 Apr 2023 02:26:53 +0800 Subject: [PATCH] fix #6420 (#6544) * fix #6420 * add test * add test * fix an error that parsed path * fix path error when in Windows environment * fix a path error * update comment --------- Co-authored-by: wuls --- .changeset/moody-points-reflect.md | 5 +++ packages/astro/src/core/build/static-build.ts | 8 +++-- .../astro/test/custom-assets-name.test.js | 21 ++++++++++++ .../custom-assets-name/astro.config.mjs | 34 +++++++++++++++++++ .../fixtures/custom-assets-name/package.json | 9 +++++ .../custom-assets-name/src/pages/index.astro | 18 ++++++++++ pnpm-lock.yaml | 8 +++++ 7 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 .changeset/moody-points-reflect.md create mode 100644 packages/astro/test/custom-assets-name.test.js create mode 100644 packages/astro/test/fixtures/custom-assets-name/astro.config.mjs create mode 100644 packages/astro/test/fixtures/custom-assets-name/package.json create mode 100644 packages/astro/test/fixtures/custom-assets-name/src/pages/index.astro diff --git a/.changeset/moody-points-reflect.md b/.changeset/moody-points-reflect.md new file mode 100644 index 000000000..44b16a18f --- /dev/null +++ b/.changeset/moody-points-reflect.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Correctly generate directories for assets when users customise the output via rollup options. diff --git a/packages/astro/src/core/build/static-build.ts b/packages/astro/src/core/build/static-build.ts index cb11a5fa0..b99890b82 100644 --- a/packages/astro/src/core/build/static-build.ts +++ b/packages/astro/src/core/build/static-build.ts @@ -3,6 +3,7 @@ import * as eslexer from 'es-module-lexer'; import glob from 'fast-glob'; import fs from 'fs'; import { bgGreen, bgMagenta, black, dim } from 'kleur/colors'; +import path from 'path'; import { fileURLToPath } from 'url'; import * as vite from 'vite'; import { @@ -384,12 +385,15 @@ async function ssrMoveAssets(opts: StaticBuildOptions) { }); if (files.length > 0) { - // Make the directory - await fs.promises.mkdir(clientAssets, { recursive: true }); + await Promise.all( files.map(async (filename) => { const currentUrl = new URL(filename, appendForwardSlash(serverAssets.toString())); const clientUrl = new URL(filename, appendForwardSlash(clientAssets.toString())); + const dir = new URL(path.parse(clientUrl.href).dir) + // It can't find this file because the user defines a custom path + // that includes the folder paths in `assetFileNames + if(!fs.existsSync(dir)) await fs.promises.mkdir(dir, { recursive: true }); return fs.promises.rename(currentUrl, clientUrl); }) ); diff --git a/packages/astro/test/custom-assets-name.test.js b/packages/astro/test/custom-assets-name.test.js new file mode 100644 index 000000000..345a9d2a7 --- /dev/null +++ b/packages/astro/test/custom-assets-name.test.js @@ -0,0 +1,21 @@ +import { expect } from 'chai'; +import { loadFixture } from './test-utils.js'; + +describe('custom the assets name function', () => { + /** @type {import('./test-utils').Fixture} */ + let fixture; + + before(async () => { + fixture = await loadFixture({ + root: './fixtures/custom-assets-name/', + output: 'server', + }); + await fixture.build(); + }); + + it('It cant find this file cause the node throws an error if the users custom a path that includes the folder path', async () => { + const csslength = await fixture.readFile('client/assets/css/a.css') + /** @type {Set} */ + expect(!!csslength).to.equal(true); + }); +}); diff --git a/packages/astro/test/fixtures/custom-assets-name/astro.config.mjs b/packages/astro/test/fixtures/custom-assets-name/astro.config.mjs new file mode 100644 index 000000000..c4a75e4eb --- /dev/null +++ b/packages/astro/test/fixtures/custom-assets-name/astro.config.mjs @@ -0,0 +1,34 @@ +import { defineConfig } from 'astro/config'; +import path from "path"; + +// https://astro.build/config +import node from "@astrojs/node"; + +// https://astro.build/config +export default defineConfig({ + vite: { + build: { + cssCodeSplit: false, + assetsInlineLimit: 0, + rollupOptions: { + output: { + + entryFileNames: 'assets/script/a.[hash].js', + assetFileNames: (option) => { + const { ext, dir, base } = path.parse(option.name); + + if (ext == ".css") return path.join(dir, "assets/css", 'a.css'); + return "assets/img/[name].[ext]"; + } + } + } + } + }, + build: { + assets: 'assets' + }, + output: "server", + adapter: node({ + mode: "standalone" + }) +}); diff --git a/packages/astro/test/fixtures/custom-assets-name/package.json b/packages/astro/test/fixtures/custom-assets-name/package.json new file mode 100644 index 000000000..00237fbdd --- /dev/null +++ b/packages/astro/test/fixtures/custom-assets-name/package.json @@ -0,0 +1,9 @@ +{ + "name": "@test/custom-assets-name", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*", + "@astrojs/node": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/custom-assets-name/src/pages/index.astro b/packages/astro/test/fixtures/custom-assets-name/src/pages/index.astro new file mode 100644 index 000000000..250233e1e --- /dev/null +++ b/packages/astro/test/fixtures/custom-assets-name/src/pages/index.astro @@ -0,0 +1,18 @@ +--- +const title = 'My App'; +--- + + + + {title} + + +

{title}

+ + + + diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 93a17b1de..5f29189d1 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2450,6 +2450,14 @@ importers: specifier: workspace:* version: link:../../.. + packages/astro/test/fixtures/custom-assets-name: + specifiers: + '@astrojs/node': workspace:* + astro: workspace:* + dependencies: + '@astrojs/node': link:../../../../integrations/node + astro: link:../../.. + packages/astro/test/fixtures/custom-elements: dependencies: '@test/custom-element-renderer':