Skip to content

Commit c879f50

Browse files
authored
Revert "fix custom assetFileNames issue (#12449)" (#12746)
* Revert "fix custom `assetFileNames` issue (#12449)" This reverts commit e6b8017. This change caused source maps to be left in the client folder, exposing them into the server. * Add changeset
1 parent 33ae732 commit c879f50

File tree

7 files changed

+29
-30
lines changed

7 files changed

+29
-30
lines changed

.changeset/loud-emus-look.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Remove all assets created from the server build

packages/astro/src/core/build/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ class AstroBuilder {
205205
key: keyPromise,
206206
};
207207

208-
const { internals, ssrOutputChunkNames, ssrOutputAssetNames, contentFileNames } =
208+
const { internals, ssrOutputChunkNames, contentFileNames } =
209209
await viteBuild(opts);
210210

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

217-
await staticBuild(opts, internals, ssrOutputChunkNames, ssrOutputAssetNames, contentFileNames);
217+
await staticBuild(opts, internals, ssrOutputChunkNames, contentFileNames);
218218

219219
// Write any additionally generated assets to disk.
220220
this.timer.assetsStart = performance.now();

packages/astro/src/core/build/static-build.ts

+15-13
Original file line numberDiff line numberDiff line change
@@ -104,26 +104,21 @@ export async function viteBuild(opts: StaticBuildOptions) {
104104
// For static builds, the SSR output won't be needed anymore after page generation.
105105
// We keep track of the names here so we only remove these specific files when finished.
106106
const ssrOutputChunkNames: string[] = [];
107-
const ssrOutputAssetNames: string[] = [];
108107
for (const output of ssrOutputs) {
109108
for (const chunk of output.output) {
110109
if (chunk.type === 'chunk') {
111110
ssrOutputChunkNames.push(chunk.fileName);
112111
}
113-
if (chunk.type === 'asset') {
114-
ssrOutputAssetNames.push(chunk.fileName);
115-
}
116112
}
117113
}
118114

119-
return { internals, ssrOutputChunkNames, ssrOutputAssetNames, contentFileNames };
115+
return { internals, ssrOutputChunkNames, contentFileNames };
120116
}
121117

122118
export async function staticBuild(
123119
opts: StaticBuildOptions,
124120
internals: BuildInternals,
125121
ssrOutputChunkNames: string[],
126-
ssrOutputAssetNames: string[],
127122
contentFileNames?: string[],
128123
) {
129124
const { settings } = opts;
@@ -136,7 +131,7 @@ export async function staticBuild(
136131
settings.timer.start('Server generate');
137132
await generatePages(opts, internals);
138133
await cleanStaticOutput(opts, internals);
139-
await ssrMoveAssets(opts, ssrOutputAssetNames);
134+
await ssrMoveAssets(opts);
140135
settings.timer.end('Server generate');
141136
}
142137
}
@@ -417,21 +412,28 @@ export async function copyFiles(fromFolder: URL, toFolder: URL, includeDotfiles
417412
);
418413
}
419414

420-
async function ssrMoveAssets(opts: StaticBuildOptions, ssrOutputAssetNames: string[]) {
415+
async function ssrMoveAssets(opts: StaticBuildOptions) {
421416
opts.logger.info('build', 'Rearranging server assets...');
422417
const serverRoot =
423418
opts.settings.buildOutput === 'static'
424419
? opts.settings.config.build.client
425420
: opts.settings.config.build.server;
426421
const clientRoot = opts.settings.config.build.client;
427-
if (ssrOutputAssetNames.length > 0) {
422+
const assets = opts.settings.config.build.assets;
423+
const serverAssets = new URL(`./${assets}/`, appendForwardSlash(serverRoot.toString()));
424+
const clientAssets = new URL(`./${assets}/`, appendForwardSlash(clientRoot.toString()));
425+
const files = await glob(`**/*`, {
426+
cwd: fileURLToPath(serverAssets),
427+
});
428+
429+
if (files.length > 0) {
428430
await Promise.all(
429-
ssrOutputAssetNames.map(async function moveAsset(filename) {
430-
const currentUrl = new URL(filename, appendForwardSlash(serverRoot.toString()));
431-
const clientUrl = new URL(filename, appendForwardSlash(clientRoot.toString()));
431+
files.map(async function moveAsset(filename) {
432+
const currentUrl = new URL(filename, appendForwardSlash(serverAssets.toString()));
433+
const clientUrl = new URL(filename, appendForwardSlash(clientAssets.toString()));
432434
const dir = new URL(path.parse(clientUrl.href).dir);
433435
// It can't find this file because the user defines a custom path
434-
// that includes the folder paths in `assetFileNames`
436+
// that includes the folder paths in `assetFileNames
435437
if (!fs.existsSync(dir)) await fs.promises.mkdir(dir, { recursive: true });
436438
return fs.promises.rename(currentUrl, clientUrl);
437439
}),

packages/astro/test/custom-assets-name.test.js

+5-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import assert from 'node:assert/strict';
22
import { before, describe, it } from 'node:test';
33
import { loadFixture } from './test-utils.js';
44

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

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

17-
it('should load CSS file from custom client assets path', async () => {
18-
const files = await fixture.readdir('/client/assets/css');
19-
const cssFile = files.find((file) => file === 'a.css');
20-
assert.ok(cssFile, 'Expected CSS file to exist at client/assets/css/a.css');
21-
});
22-
23-
it('should load image file from custom client assets path', async () => {
24-
const files = await fixture.readdir('/client/imgAssets');
25-
const imgFile = files.find((file) => file === 'penguin1.jpg');
26-
assert.ok(imgFile, 'Expected image file to exist at client/imgAssets/penguin1.jpg');
17+
it('It cant find this file cause the node throws an error if the users custom a path that includes the folder path', async () => {
18+
const csslength = await fixture.readFile('client/assets/css/a.css');
19+
/** @type {Set<string>} */
20+
assert.equal(!!csslength, true);
2721
});
2822
});

packages/astro/test/fixtures/custom-assets-name/astro.config.mjs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ export default defineConfig({
1818
const { ext, dir, base } = path.parse(option.name);
1919

2020
if (ext == ".css") return path.join(dir, "assets/css", 'a.css');
21-
return "imgAssets/[name].[ext]";
21+
return "assets/img/[name].[ext]";
2222
}
2323
}
2424
}
2525
}
2626
},
2727
build: {
28-
assets: 'assetsDir'
28+
assets: 'assets'
2929
},
3030
output: "server",
3131
adapter: node({
Binary file not shown.

packages/astro/test/fixtures/custom-assets-name/src/pages/index.astro

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
---
22
const title = 'My App';
3-
import p1Url from '../images/penguin1.jpg';
43
---
54

65
<html>
@@ -9,7 +8,6 @@ import p1Url from '../images/penguin1.jpg';
98
</head>
109
<body>
1110
<h1>{title}</h1>
12-
<img src={p1Url.src}/>
1311
</body>
1412
</html>
1513

0 commit comments

Comments
 (0)