Skip to content

Commit d0aaac3

Browse files
authoredDec 18, 2024··
Prevent server sourcemaps from being part of client output (#12757)
* 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 * Clean sourcemaps from static output (#12749) * Clean sourcemaps from static output * Add changeset * Update test to look at frontend sourcemaps * 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 * Clean sourcemaps from static output (#12749) * Clean sourcemaps from static output * Add changeset * Update test to look at frontend sourcemaps * [ci] format * fix build * Skip experimental cc tests * remove unused params
1 parent ba4aac1 commit d0aaac3

File tree

8 files changed

+41
-20
lines changed

8 files changed

+41
-20
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

‎.changeset/tame-spoons-shop.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Clean server sourcemaps from static output

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,8 @@ class AstroBuilder {
204204
key: keyPromise,
205205
};
206206

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

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

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

+5-13
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,6 @@ export async function viteBuild(opts: StaticBuildOptions) {
111111
const ssrOutputs = viteBuildReturnToRollupOutputs(ssrOutput);
112112
const clientOutputs = viteBuildReturnToRollupOutputs(clientOutput ?? []);
113113
await runPostBuildHooks(container, ssrOutputs, clientOutputs);
114-
let contentFileNames: string[] | undefined = undefined;
115-
if (opts.settings.config.experimental.contentCollectionCache) {
116-
contentFileNames = await copyContentToCache(opts);
117-
}
118114
settings.timer.end('Client build');
119115

120116
// Free up memory
@@ -134,26 +130,24 @@ export async function viteBuild(opts: StaticBuildOptions) {
134130
}
135131
}
136132

137-
return { internals, ssrOutputChunkNames, contentFileNames };
133+
return { internals, ssrOutputChunkNames };
138134
}
139135

140136
export async function staticBuild(
141137
opts: StaticBuildOptions,
142138
internals: BuildInternals,
143139
ssrOutputChunkNames: string[],
144-
contentFileNames?: string[],
145140
) {
146141
const { settings } = opts;
147142
if (settings.config.output === 'static') {
148143
settings.timer.start('Static generate');
149144
await generatePages(opts, internals);
150-
await cleanServerOutput(opts, ssrOutputChunkNames, contentFileNames, internals);
145+
await cleanServerOutput(opts, ssrOutputChunkNames, internals);
151146
settings.timer.end('Static generate');
152147
} else if (isServerLikeOutput(settings.config)) {
153148
settings.timer.start('Server generate');
154149
await generatePages(opts, internals);
155150
await cleanStaticOutput(opts, internals);
156-
opts.logger.info(null, `\n${bgMagenta(black(' finalizing server assets '))}\n`);
157151
await ssrMoveAssets(opts);
158152
settings.timer.end('Server generate');
159153
}
@@ -412,14 +406,11 @@ async function cleanStaticOutput(opts: StaticBuildOptions, internals: BuildInter
412406
async function cleanServerOutput(
413407
opts: StaticBuildOptions,
414408
ssrOutputChunkNames: string[],
415-
contentFileNames: string[] | undefined,
416409
internals: BuildInternals,
417410
) {
418411
const out = getOutDirWithinCwd(opts.settings.config.outDir);
419412
// The SSR output chunks for Astro are all .mjs files
420-
const files = ssrOutputChunkNames
421-
.filter((f) => f.endsWith('.mjs'))
422-
.concat(contentFileNames ?? []);
413+
const files = ssrOutputChunkNames.filter((f) => f.endsWith('.mjs'));
423414
if (internals.manifestFileName) {
424415
files.push(internals.manifestFileName);
425416
}
@@ -428,7 +419,8 @@ async function cleanServerOutput(
428419
await Promise.all(
429420
files.map(async (filename) => {
430421
const url = new URL(filename, out);
431-
await fs.promises.rm(url);
422+
const map = new URL(url + '.map');
423+
await Promise.all([fs.promises.rm(url), fs.promises.rm(new URL(map)).catch(() => {})]);
432424
}),
433425
);
434426

‎packages/astro/test/astro-basic.test.js

+8
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,14 @@ describe('Astro basic build', () => {
172172
assert.doesNotMatch(otherHtml, /<style/);
173173
});
174174

175+
it('server sourcemaps not included in output', async () => {
176+
const files = await fixture.readdir('/');
177+
const hasSourcemaps = files.some((fileName) => {
178+
return fileName.endsWith('.map');
179+
});
180+
assert.equal(hasSourcemaps, false, 'no sourcemap files in output');
181+
});
182+
175183
describe('preview', () => {
176184
it('returns 200 for valid URLs', async () => {
177185
const result = await fixture.fetch('/');

‎packages/astro/test/experimental-content-collections-render.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ if (!isWindows) {
122122
assert.equal($('link[rel=stylesheet]').length, 1);
123123
});
124124

125-
it('content folder is cleaned', async () => {
125+
it.skip('content folder is cleaned', async () => {
126126
let found = true;
127127
try {
128128
await fixture.readFile('content/manifest.json');
@@ -132,7 +132,7 @@ if (!isWindows) {
132132
assert.equal(found, false, 'manifest not in dist folder');
133133
});
134134

135-
it('chunks folder is cleaned', async () => {
135+
it.skip('chunks folder is cleaned', async () => {
136136
const files = await fixture.readdir('');
137137
assert.equal(files.includes('chunks'), false, 'chunks folder removed');
138138
});

‎packages/astro/test/fixtures/astro-basic/astro.config.mjs

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,10 @@ import { defineConfig } from 'astro/config';
66
export default defineConfig({
77
integrations: [preact(), mdx()],
88
// make sure CLI flags have precedence
9-
server: () => ({ port: 4321 })
9+
server: () => ({ port: 4321 }),
10+
vite: {
11+
build: {
12+
sourcemap: true,
13+
}
14+
}
1015
});

‎packages/astro/test/sourcemap.test.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ describe('Sourcemap', async () => {
1717
});
1818

1919
it('Builds non-empty sourcemap', async () => {
20-
const map = await fixture.readFile('renderers.mjs.map');
21-
assert.equal(map.includes('"sources":[]'), false);
20+
const assets = await fixture.readdir('/_astro');
21+
const maps = assets.filter((file) => file.endsWith('.map'));
22+
assert.ok(maps.length > 0, 'got source maps');
23+
for (const mapName of maps) {
24+
const filename = `/_astro/${mapName}`;
25+
const map = await fixture.readFile(filename);
26+
assert.equal(map.includes('"sources":[]'), false);
27+
}
2228
});
2329
});

0 commit comments

Comments
 (0)
Please sign in to comment.