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

Revert "fix custom assetFileNames issue (#12449)" #12746

Merged
merged 2 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions packages/astro/src/core/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ class AstroBuilder {
key: keyPromise,
};

const { internals, ssrOutputChunkNames, ssrOutputAssetNames, contentFileNames } =
const { internals, ssrOutputChunkNames, contentFileNames } =
await viteBuild(opts);

const hasServerIslands = this.settings.serverIslandNameMap.size > 0;
Expand All @@ -214,7 +214,7 @@ class AstroBuilder {
throw new AstroError(AstroErrorData.NoAdapterInstalledServerIslands);
}

await staticBuild(opts, internals, ssrOutputChunkNames, ssrOutputAssetNames, contentFileNames);
await staticBuild(opts, internals, ssrOutputChunkNames, contentFileNames);

// Write any additionally generated assets to disk.
this.timer.assetsStart = performance.now();
Expand Down
28 changes: 15 additions & 13 deletions packages/astro/src/core/build/static-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,26 +104,21 @@ export async function viteBuild(opts: StaticBuildOptions) {
// For static builds, the SSR output won't be needed anymore after page generation.
// We keep track of the names here so we only remove these specific files when finished.
const ssrOutputChunkNames: string[] = [];
const ssrOutputAssetNames: string[] = [];
for (const output of ssrOutputs) {
for (const chunk of output.output) {
if (chunk.type === 'chunk') {
ssrOutputChunkNames.push(chunk.fileName);
}
if (chunk.type === 'asset') {
ssrOutputAssetNames.push(chunk.fileName);
}
}
}

return { internals, ssrOutputChunkNames, ssrOutputAssetNames, contentFileNames };
return { internals, ssrOutputChunkNames, contentFileNames };
}

export async function staticBuild(
opts: StaticBuildOptions,
internals: BuildInternals,
ssrOutputChunkNames: string[],
ssrOutputAssetNames: string[],
contentFileNames?: string[],
) {
const { settings } = opts;
Expand All @@ -136,7 +131,7 @@ export async function staticBuild(
settings.timer.start('Server generate');
await generatePages(opts, internals);
await cleanStaticOutput(opts, internals);
await ssrMoveAssets(opts, ssrOutputAssetNames);
await ssrMoveAssets(opts);
settings.timer.end('Server generate');
}
}
Expand Down Expand Up @@ -417,21 +412,28 @@ export async function copyFiles(fromFolder: URL, toFolder: URL, includeDotfiles
);
}

async function ssrMoveAssets(opts: StaticBuildOptions, ssrOutputAssetNames: string[]) {
async function ssrMoveAssets(opts: StaticBuildOptions) {
opts.logger.info('build', 'Rearranging server assets...');
const serverRoot =
opts.settings.buildOutput === 'static'
? opts.settings.config.build.client
: opts.settings.config.build.server;
const clientRoot = opts.settings.config.build.client;
if (ssrOutputAssetNames.length > 0) {
const assets = opts.settings.config.build.assets;
const serverAssets = new URL(`./${assets}/`, appendForwardSlash(serverRoot.toString()));
const clientAssets = new URL(`./${assets}/`, appendForwardSlash(clientRoot.toString()));
const files = await glob(`**/*`, {
cwd: fileURLToPath(serverAssets),
});

if (files.length > 0) {
await Promise.all(
ssrOutputAssetNames.map(async function moveAsset(filename) {
const currentUrl = new URL(filename, appendForwardSlash(serverRoot.toString()));
const clientUrl = new URL(filename, appendForwardSlash(clientRoot.toString()));
files.map(async function moveAsset(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`
// that includes the folder paths in `assetFileNames
if (!fs.existsSync(dir)) await fs.promises.mkdir(dir, { recursive: true });
return fs.promises.rename(currentUrl, clientUrl);
}),
Expand Down
16 changes: 5 additions & 11 deletions packages/astro/test/custom-assets-name.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import { loadFixture } from './test-utils.js';

describe('custom assets name function', () => {
describe('custom the assets name function', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;

Expand All @@ -14,15 +14,9 @@ describe('custom assets name function', () => {
await fixture.build();
});

it('should load CSS file from custom client assets path', async () => {
const files = await fixture.readdir('/client/assets/css');
const cssFile = files.find((file) => file === 'a.css');
assert.ok(cssFile, 'Expected CSS file to exist at client/assets/css/a.css');
});

it('should load image file from custom client assets path', async () => {
const files = await fixture.readdir('/client/imgAssets');
const imgFile = files.find((file) => file === 'penguin1.jpg');
assert.ok(imgFile, 'Expected image file to exist at client/imgAssets/penguin1.jpg');
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>} */
assert.equal(!!csslength, true);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ export default defineConfig({
const { ext, dir, base } = path.parse(option.name);

if (ext == ".css") return path.join(dir, "assets/css", 'a.css');
return "imgAssets/[name].[ext]";
return "assets/img/[name].[ext]";
}
}
}
}
},
build: {
assets: 'assetsDir'
assets: 'assets'
},
output: "server",
adapter: node({
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
const title = 'My App';
import p1Url from '../images/penguin1.jpg';
---

<html>
Expand All @@ -9,7 +8,6 @@ import p1Url from '../images/penguin1.jpg';
</head>
<body>
<h1>{title}</h1>
<img src={p1Url.src}/>
</body>
</html>

Expand Down
Loading