From 9b5be90d898679efe78f7214f1079ed328706875 Mon Sep 17 00:00:00 2001 From: wulinsheng123 <409187100@qq.com> Date: Tue, 14 Mar 2023 06:04:29 +0000 Subject: [PATCH 1/7] fix #6420 --- .changeset/moody-points-reflect.md | 5 +++++ packages/astro/src/core/build/static-build.ts | 7 ++++++- 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 .changeset/moody-points-reflect.md diff --git a/.changeset/moody-points-reflect.md b/.changeset/moody-points-reflect.md new file mode 100644 index 000000000000..9beb6399a30c --- /dev/null +++ b/.changeset/moody-points-reflect.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +It can't find this file cause the node throws an error if the users custom a path that includes the folder path diff --git a/packages/astro/src/core/build/static-build.ts b/packages/astro/src/core/build/static-build.ts index dae67b439898..742443c81e09 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 { @@ -381,11 +382,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 = path.parse(clientUrl.pathname).dir + // It can't find this file cause the node throws an error if the users custom a path that includes the folder path in `assetFileNames` + // fix bug https://github.com/withastro/astro/issues/6420 + if(!fs.existsSync(dir)) await fs.promises.mkdir(dir, { recursive: true }); return fs.promises.rename(currentUrl, clientUrl); }) ); From a21834a388b68585f9dfd641b72a0ebf93280a5b Mon Sep 17 00:00:00 2001 From: wuls Date: Tue, 14 Mar 2023 17:30:42 +0800 Subject: [PATCH 2/7] add test --- packages/astro/package.json | 2 ++ .../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 ++++++++++ 5 files changed, 84 insertions(+) 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/packages/astro/package.json b/packages/astro/package.json index 9bcd9cd1e65d..151c47a475a1 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -99,6 +99,8 @@ "dev": "astro-scripts dev --copy-wasm --prebuild \"src/runtime/server/astro-island.ts\" --prebuild \"src/runtime/client/{idle,load,media,only,visible}.ts\" \"src/**/*.ts\"", "postbuild": "astro-scripts copy \"src/**/*.astro\" && astro-scripts copy \"src/**/*.wasm\"", "test:unit": "mocha --exit --timeout 30000 ./test/units/**/*.test.js", + "test:1": "mocha --exit --timeout 30000 ./test/custom-assets-name.test.js", + "test:unit:match": "mocha --exit --timeout 30000 ./test/units/**/*.test.js -g", "test": "pnpm run test:unit && mocha --exit --timeout 20000 --ignore **/lit-element.test.js && mocha --timeout 20000 **/lit-element.test.js", "test:match": "mocha --timeout 20000 -g", 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 000000000000..345a9d2a7eee --- /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 000000000000..c4a75e4ebc81 --- /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 000000000000..00237fbddfdc --- /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 000000000000..250233e1e15f --- /dev/null +++ b/packages/astro/test/fixtures/custom-assets-name/src/pages/index.astro @@ -0,0 +1,18 @@ +--- +const title = 'My App'; +--- + + + + {title} + + +

{title}

+ + + + From bdf12ff8dbc75e5db005f78538356e71c5cdf926 Mon Sep 17 00:00:00 2001 From: wuls Date: Tue, 14 Mar 2023 17:31:10 +0800 Subject: [PATCH 3/7] add test --- pnpm-lock.yaml | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 44e6f54679da..703728f26972 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1936,6 +1936,14 @@ importers: dependencies: astro: 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: specifiers: '@test/custom-element-renderer': workspace:* @@ -4194,14 +4202,15 @@ packages: - react dev: false - /@astrojs/markdown-remark/2.0.1_astro@packages+astro: - resolution: {integrity: sha512-xQF1rXGJN18m+zZucwRRtmNehuhPMMhZhi6HWKrtpEAKnHSPk8lqf1GXgKH7/Sypglu8ivdECZ+EGs6kOYVasQ==} + /@astrojs/markdown-remark/2.1.0_astro@packages+astro: + resolution: {integrity: sha512-w9T5o3UWQIfMcCkM2nLWrlfVQazh/7mw+2N/85QGcSUkZy6oNJoyy8Xz/ZkDhHLx8HPO0RT9fABR0B/H+aDaEw==} peerDependencies: astro: '*' dependencies: - '@astrojs/prism': 2.0.0 + '@astrojs/prism': 2.1.1 astro: link:packages/astro github-slugger: 1.5.0 + image-size: 1.0.2 import-meta-resolve: 2.2.1 rehype-raw: 6.1.1 rehype-stringify: 9.0.3 @@ -4221,8 +4230,8 @@ packages: resolution: {integrity: sha512-mol57cw1jJMcQgKMRGn7p6cewajq6JTNtqj5aAZgROWam/phVDSOCbXj/WU3O9+3qFnyKtpczoufQKwJTQltAw==} engines: {node: '>=16.12.0'} dependencies: - '@astrojs/markdown-remark': 2.0.1_astro@packages+astro - '@astrojs/prism': 2.0.0 + '@astrojs/markdown-remark': 2.1.0_astro@packages+astro + '@astrojs/prism': 2.1.1 '@mdx-js/mdx': 2.3.0 '@mdx-js/rollup': 2.3.0 acorn: 8.8.2 @@ -4266,8 +4275,8 @@ packages: - supports-color dev: false - /@astrojs/prism/2.0.0: - resolution: {integrity: sha512-YgeoeEPqsxaEpg0rwe/bUq3653LqSQnMjrLlpYwrbQQMQQqz6Y5yXN+RX3SfLJ6ppNb4+Fu2+Z49EXjk48Ihjw==} + /@astrojs/prism/2.1.1: + resolution: {integrity: sha512-Gnwnlb1lGJzCQEg89r4/WqgfCGPNFC7Kuh2D/k289Cbdi/2PD7Lrdstz86y1itDvcb2ijiRqjqWnJ5rsfu/QOA==} engines: {node: '>=16.12.0'} dependencies: prismjs: 1.29.0 From 1860d4003982b2f966579a3c14e43ba2b9dcdc38 Mon Sep 17 00:00:00 2001 From: wuls Date: Tue, 14 Mar 2023 18:13:46 +0800 Subject: [PATCH 4/7] fix an error that parsed path --- packages/astro/package.json | 2 -- packages/astro/src/core/build/static-build.ts | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/astro/package.json b/packages/astro/package.json index 151c47a475a1..9bcd9cd1e65d 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -99,8 +99,6 @@ "dev": "astro-scripts dev --copy-wasm --prebuild \"src/runtime/server/astro-island.ts\" --prebuild \"src/runtime/client/{idle,load,media,only,visible}.ts\" \"src/**/*.ts\"", "postbuild": "astro-scripts copy \"src/**/*.astro\" && astro-scripts copy \"src/**/*.wasm\"", "test:unit": "mocha --exit --timeout 30000 ./test/units/**/*.test.js", - "test:1": "mocha --exit --timeout 30000 ./test/custom-assets-name.test.js", - "test:unit:match": "mocha --exit --timeout 30000 ./test/units/**/*.test.js -g", "test": "pnpm run test:unit && mocha --exit --timeout 20000 --ignore **/lit-element.test.js && mocha --timeout 20000 **/lit-element.test.js", "test:match": "mocha --timeout 20000 -g", diff --git a/packages/astro/src/core/build/static-build.ts b/packages/astro/src/core/build/static-build.ts index 742443c81e09..bfa8f9d9a5c3 100644 --- a/packages/astro/src/core/build/static-build.ts +++ b/packages/astro/src/core/build/static-build.ts @@ -387,7 +387,7 @@ async function ssrMoveAssets(opts: StaticBuildOptions) { files.map(async (filename) => { const currentUrl = new URL(filename, appendForwardSlash(serverAssets.toString())); const clientUrl = new URL(filename, appendForwardSlash(clientAssets.toString())); - const dir = path.parse(clientUrl.pathname).dir + const dir = path.join(path.parse(clientUrl.pathname).dir) // It can't find this file cause the node throws an error if the users custom a path that includes the folder path in `assetFileNames` // fix bug https://github.com/withastro/astro/issues/6420 if(!fs.existsSync(dir)) await fs.promises.mkdir(dir, { recursive: true }); From 993398569bba4cae7fd036ecdde8e9316b40c342 Mon Sep 17 00:00:00 2001 From: wuls Date: Wed, 15 Mar 2023 10:19:41 +0800 Subject: [PATCH 5/7] fix path error when in Windows environment --- packages/astro/src/core/build/static-build.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/astro/src/core/build/static-build.ts b/packages/astro/src/core/build/static-build.ts index bfa8f9d9a5c3..0eda9ac5799b 100644 --- a/packages/astro/src/core/build/static-build.ts +++ b/packages/astro/src/core/build/static-build.ts @@ -381,13 +381,12 @@ async function ssrMoveAssets(opts: StaticBuildOptions) { }); if (files.length > 0) { - // Make the directory - + 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 = path.join(path.parse(clientUrl.pathname).dir) + const dir = new URL(path.parse(clientUrl.pathname).dir) // It can't find this file cause the node throws an error if the users custom a path that includes the folder path in `assetFileNames` // fix bug https://github.com/withastro/astro/issues/6420 if(!fs.existsSync(dir)) await fs.promises.mkdir(dir, { recursive: true }); From 4757c939a56b9a2674f63bedc5ac8ba89986059c Mon Sep 17 00:00:00 2001 From: wuls Date: Wed, 15 Mar 2023 10:37:31 +0800 Subject: [PATCH 6/7] fix a path error --- packages/astro/src/core/build/static-build.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/astro/src/core/build/static-build.ts b/packages/astro/src/core/build/static-build.ts index 0eda9ac5799b..9e306944f03b 100644 --- a/packages/astro/src/core/build/static-build.ts +++ b/packages/astro/src/core/build/static-build.ts @@ -386,7 +386,7 @@ async function ssrMoveAssets(opts: StaticBuildOptions) { 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.pathname).dir) + const dir = new URL(path.parse(clientUrl.href).dir) // It can't find this file cause the node throws an error if the users custom a path that includes the folder path in `assetFileNames` // fix bug https://github.com/withastro/astro/issues/6420 if(!fs.existsSync(dir)) await fs.promises.mkdir(dir, { recursive: true }); From ecd1c5b14f68db776feff97cd4346908fd498304 Mon Sep 17 00:00:00 2001 From: wuls Date: Wed, 22 Mar 2023 14:34:56 +0800 Subject: [PATCH 7/7] update comment --- .changeset/moody-points-reflect.md | 2 +- packages/astro/src/core/build/static-build.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.changeset/moody-points-reflect.md b/.changeset/moody-points-reflect.md index 9beb6399a30c..44b16a18f30a 100644 --- a/.changeset/moody-points-reflect.md +++ b/.changeset/moody-points-reflect.md @@ -2,4 +2,4 @@ 'astro': patch --- -It can't find this file cause the node throws an error if the users custom a path that includes the folder path +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 a7b3a9704a31..774a54a9b0c9 100644 --- a/packages/astro/src/core/build/static-build.ts +++ b/packages/astro/src/core/build/static-build.ts @@ -387,8 +387,8 @@ async function ssrMoveAssets(opts: StaticBuildOptions) { 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 cause the node throws an error if the users custom a path that includes the folder path in `assetFileNames` - // fix bug https://github.com/withastro/astro/issues/6420 + // 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); })