mirror of
https://github.com/withastro/astro.git
synced 2024-12-16 21:46:22 -05:00
* Revert "fix custom `assetFileNames` issue (#12449)"
This reverts commit e6b8017239
.
This change caused source maps to be left in the client folder, exposing them into the server.
* Add changeset
This commit is contained in:
parent
33ae7320e8
commit
c879f501ff
7 changed files with 29 additions and 30 deletions
5
.changeset/loud-emus-look.md
Normal file
5
.changeset/loud-emus-look.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Remove all assets created from the server build
|
|
@ -205,7 +205,7 @@ class AstroBuilder {
|
||||||
key: keyPromise,
|
key: keyPromise,
|
||||||
};
|
};
|
||||||
|
|
||||||
const { internals, ssrOutputChunkNames, ssrOutputAssetNames, contentFileNames } =
|
const { internals, ssrOutputChunkNames, contentFileNames } =
|
||||||
await viteBuild(opts);
|
await viteBuild(opts);
|
||||||
|
|
||||||
const hasServerIslands = this.settings.serverIslandNameMap.size > 0;
|
const hasServerIslands = this.settings.serverIslandNameMap.size > 0;
|
||||||
|
@ -214,7 +214,7 @@ class AstroBuilder {
|
||||||
throw new AstroError(AstroErrorData.NoAdapterInstalledServerIslands);
|
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.
|
// Write any additionally generated assets to disk.
|
||||||
this.timer.assetsStart = performance.now();
|
this.timer.assetsStart = performance.now();
|
||||||
|
|
|
@ -104,26 +104,21 @@ export async function viteBuild(opts: StaticBuildOptions) {
|
||||||
// For static builds, the SSR output won't be needed anymore after page generation.
|
// 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.
|
// We keep track of the names here so we only remove these specific files when finished.
|
||||||
const ssrOutputChunkNames: string[] = [];
|
const ssrOutputChunkNames: string[] = [];
|
||||||
const ssrOutputAssetNames: string[] = [];
|
|
||||||
for (const output of ssrOutputs) {
|
for (const output of ssrOutputs) {
|
||||||
for (const chunk of output.output) {
|
for (const chunk of output.output) {
|
||||||
if (chunk.type === 'chunk') {
|
if (chunk.type === 'chunk') {
|
||||||
ssrOutputChunkNames.push(chunk.fileName);
|
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(
|
export async function staticBuild(
|
||||||
opts: StaticBuildOptions,
|
opts: StaticBuildOptions,
|
||||||
internals: BuildInternals,
|
internals: BuildInternals,
|
||||||
ssrOutputChunkNames: string[],
|
ssrOutputChunkNames: string[],
|
||||||
ssrOutputAssetNames: string[],
|
|
||||||
contentFileNames?: string[],
|
contentFileNames?: string[],
|
||||||
) {
|
) {
|
||||||
const { settings } = opts;
|
const { settings } = opts;
|
||||||
|
@ -136,7 +131,7 @@ export async function staticBuild(
|
||||||
settings.timer.start('Server generate');
|
settings.timer.start('Server generate');
|
||||||
await generatePages(opts, internals);
|
await generatePages(opts, internals);
|
||||||
await cleanStaticOutput(opts, internals);
|
await cleanStaticOutput(opts, internals);
|
||||||
await ssrMoveAssets(opts, ssrOutputAssetNames);
|
await ssrMoveAssets(opts);
|
||||||
settings.timer.end('Server generate');
|
settings.timer.end('Server generate');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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...');
|
opts.logger.info('build', 'Rearranging server assets...');
|
||||||
const serverRoot =
|
const serverRoot =
|
||||||
opts.settings.buildOutput === 'static'
|
opts.settings.buildOutput === 'static'
|
||||||
? opts.settings.config.build.client
|
? opts.settings.config.build.client
|
||||||
: opts.settings.config.build.server;
|
: opts.settings.config.build.server;
|
||||||
const clientRoot = opts.settings.config.build.client;
|
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(
|
await Promise.all(
|
||||||
ssrOutputAssetNames.map(async function moveAsset(filename) {
|
files.map(async function moveAsset(filename) {
|
||||||
const currentUrl = new URL(filename, appendForwardSlash(serverRoot.toString()));
|
const currentUrl = new URL(filename, appendForwardSlash(serverAssets.toString()));
|
||||||
const clientUrl = new URL(filename, appendForwardSlash(clientRoot.toString()));
|
const clientUrl = new URL(filename, appendForwardSlash(clientAssets.toString()));
|
||||||
const dir = new URL(path.parse(clientUrl.href).dir);
|
const dir = new URL(path.parse(clientUrl.href).dir);
|
||||||
// It can't find this file because the user defines a custom path
|
// 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 });
|
if (!fs.existsSync(dir)) await fs.promises.mkdir(dir, { recursive: true });
|
||||||
return fs.promises.rename(currentUrl, clientUrl);
|
return fs.promises.rename(currentUrl, clientUrl);
|
||||||
}),
|
}),
|
||||||
|
|
|
@ -2,7 +2,7 @@ import assert from 'node:assert/strict';
|
||||||
import { before, describe, it } from 'node:test';
|
import { before, describe, it } from 'node:test';
|
||||||
import { loadFixture } from './test-utils.js';
|
import { loadFixture } from './test-utils.js';
|
||||||
|
|
||||||
describe('custom assets name function', () => {
|
describe('custom the assets name function', () => {
|
||||||
/** @type {import('./test-utils').Fixture} */
|
/** @type {import('./test-utils').Fixture} */
|
||||||
let fixture;
|
let fixture;
|
||||||
|
|
||||||
|
@ -14,15 +14,9 @@ describe('custom assets name function', () => {
|
||||||
await fixture.build();
|
await fixture.build();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should load CSS file from custom client assets path', async () => {
|
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 files = await fixture.readdir('/client/assets/css');
|
const csslength = await fixture.readFile('client/assets/css/a.css');
|
||||||
const cssFile = files.find((file) => file === 'a.css');
|
/** @type {Set<string>} */
|
||||||
assert.ok(cssFile, 'Expected CSS file to exist at client/assets/css/a.css');
|
assert.equal(!!csslength, true);
|
||||||
});
|
|
||||||
|
|
||||||
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');
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -18,14 +18,14 @@ export default defineConfig({
|
||||||
const { ext, dir, base } = path.parse(option.name);
|
const { ext, dir, base } = path.parse(option.name);
|
||||||
|
|
||||||
if (ext == ".css") return path.join(dir, "assets/css", 'a.css');
|
if (ext == ".css") return path.join(dir, "assets/css", 'a.css');
|
||||||
return "imgAssets/[name].[ext]";
|
return "assets/img/[name].[ext]";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
build: {
|
build: {
|
||||||
assets: 'assetsDir'
|
assets: 'assets'
|
||||||
},
|
},
|
||||||
output: "server",
|
output: "server",
|
||||||
adapter: node({
|
adapter: node({
|
||||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 11 KiB |
|
@ -1,6 +1,5 @@
|
||||||
---
|
---
|
||||||
const title = 'My App';
|
const title = 'My App';
|
||||||
import p1Url from '../images/penguin1.jpg';
|
|
||||||
---
|
---
|
||||||
|
|
||||||
<html>
|
<html>
|
||||||
|
@ -9,7 +8,6 @@ import p1Url from '../images/penguin1.jpg';
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>{title}</h1>
|
<h1>{title}</h1>
|
||||||
<img src={p1Url.src}/>
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue