Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #6420 #6544

Merged
merged 20 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/moody-points-reflect.md
Original file line number Diff line number Diff line change
@@ -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
JerryWu1234 marked this conversation as resolved.
Show resolved Hide resolved
8 changes: 6 additions & 2 deletions packages/astro/src/core/build/static-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -380,12 +381,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 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
JerryWu1234 marked this conversation as resolved.
Show resolved Hide resolved
if(!fs.existsSync(dir)) await fs.promises.mkdir(dir, { recursive: true });
return fs.promises.rename(currentUrl, clientUrl);
})
);
Expand Down
21 changes: 21 additions & 0 deletions packages/astro/test/custom-assets-name.test.js
Original file line number Diff line number Diff line change
@@ -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<string>} */
expect(!!csslength).to.equal(true);
});
});
34 changes: 34 additions & 0 deletions packages/astro/test/fixtures/custom-assets-name/astro.config.mjs
Original file line number Diff line number Diff line change
@@ -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"
})
});
9 changes: 9 additions & 0 deletions packages/astro/test/fixtures/custom-assets-name/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@test/custom-assets-name",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*",
"@astrojs/node": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
const title = 'My App';
---

<html>
<head>
<title>{title}</title>
</head>
<body>
<h1>{title}</h1>
</body>
</html>

<style>
h1 {
color: red;
}
</style>
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.