* 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 <linsheng.wu@beantechs.com>
This commit is contained in:
wulinsheng123 2023-04-13 02:26:53 +08:00 committed by GitHub
parent c1e8f42a20
commit a9c22994e4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 101 additions and 2 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Correctly generate directories for assets when users customise the output via rollup options.

View file

@ -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);
})
);

View file

@ -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);
});
});

View file

@ -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"
})
});

View file

@ -0,0 +1,9 @@
{
"name": "@test/custom-assets-name",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*",
"@astrojs/node": "workspace:*"
}
}

View file

@ -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>

View file

@ -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':