Skip to content

Commit 039d022

Browse files
authored
Clean sourcemaps from static output (#12749)
* Clean sourcemaps from static output * Add changeset * Update test to look at frontend sourcemaps
1 parent d891d5d commit 039d022

File tree

6 files changed

+40
-13
lines changed

6 files changed

+40
-13
lines changed

.changeset/tame-spoons-shop.md

Lines changed: 5 additions & 0 deletions
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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,15 +205,16 @@ class AstroBuilder {
205205
key: keyPromise,
206206
};
207207

208-
const { internals, ssrOutputChunkNames, contentFileNames } = await viteBuild(opts);
208+
const { internals, ssrOutputChunkNames } =
209+
await viteBuild(opts);
209210

210211
const hasServerIslands = this.settings.serverIslandNameMap.size > 0;
211212
// Error if there are server islands but no adapter provided.
212213
if (hasServerIslands && this.settings.buildOutput !== 'server') {
213214
throw new AstroError(AstroErrorData.NoAdapterInstalledServerIslands);
214215
}
215216

216-
await staticBuild(opts, internals, ssrOutputChunkNames, contentFileNames);
217+
await staticBuild(opts, internals, ssrOutputChunkNames);
217218

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

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ export async function viteBuild(opts: StaticBuildOptions) {
9292
const ssrOutputs = viteBuildReturnToRollupOutputs(ssrOutput);
9393
const clientOutputs = viteBuildReturnToRollupOutputs(clientOutput ?? []);
9494
await runPostBuildHooks(container, ssrOutputs, clientOutputs);
95-
let contentFileNames: string[] | undefined = undefined;
9695
settings.timer.end('Client build');
9796

9897
// Free up memory
@@ -112,20 +111,19 @@ export async function viteBuild(opts: StaticBuildOptions) {
112111
}
113112
}
114113

115-
return { internals, ssrOutputChunkNames, contentFileNames };
114+
return { internals, ssrOutputChunkNames };
116115
}
117116

118117
export async function staticBuild(
119118
opts: StaticBuildOptions,
120119
internals: BuildInternals,
121120
ssrOutputChunkNames: string[],
122-
contentFileNames?: string[],
123121
) {
124122
const { settings } = opts;
125123
if (settings.buildOutput === 'static') {
126124
settings.timer.start('Static generate');
127125
await generatePages(opts, internals);
128-
await cleanServerOutput(opts, ssrOutputChunkNames, contentFileNames, internals);
126+
await cleanServerOutput(opts, ssrOutputChunkNames, internals);
129127
settings.timer.end('Static generate');
130128
} else if (settings.buildOutput === 'server') {
131129
settings.timer.start('Server generate');
@@ -354,14 +352,12 @@ async function cleanStaticOutput(opts: StaticBuildOptions, internals: BuildInter
354352
async function cleanServerOutput(
355353
opts: StaticBuildOptions,
356354
ssrOutputChunkNames: string[],
357-
contentFileNames: string[] | undefined,
358355
internals: BuildInternals,
359356
) {
360357
const out = getOutDirWithinCwd(opts.settings.config.outDir);
361358
// The SSR output chunks for Astro are all .mjs files
362359
const files = ssrOutputChunkNames
363-
.filter((f) => f.endsWith('.mjs'))
364-
.concat(contentFileNames ?? []);
360+
.filter((f) => f.endsWith('.mjs'));
365361
if (internals.manifestFileName) {
366362
files.push(internals.manifestFileName);
367363
}
@@ -370,7 +366,11 @@ async function cleanServerOutput(
370366
await Promise.all(
371367
files.map(async (filename) => {
372368
const url = new URL(filename, out);
373-
await fs.promises.rm(url);
369+
const map = new URL(url + '.map');
370+
await Promise.all([
371+
fs.promises.rm(url),
372+
fs.promises.rm(new URL(map)).catch((e) => {})
373+
]);
374374
}),
375375
);
376376

@@ -426,6 +426,8 @@ async function ssrMoveAssets(opts: StaticBuildOptions) {
426426
cwd: fileURLToPath(serverAssets),
427427
});
428428

429+
console.log("FILES2", files);
430+
429431
if (files.length > 0) {
430432
await Promise.all(
431433
files.map(async function moveAsset(filename) {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,14 @@ describe('Astro basic build', () => {
167167
assert.doesNotMatch(otherHtml, /<style/);
168168
});
169169

170+
it('server sourcemaps not included in output', async () => {
171+
const files = await fixture.readdir('/');
172+
const hasSourcemaps = files.some(fileName => {
173+
return fileName.endsWith('.map');
174+
});
175+
assert.equal(hasSourcemaps, false, 'no sourcemap files in output');
176+
});
177+
170178
describe('preview', () => {
171179
it('returns 200 for valid URLs', async () => {
172180
const result = await fixture.fetch('/');

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

Lines changed: 6 additions & 1 deletion
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

Lines changed: 8 additions & 2 deletions
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)