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

Clean sourcemaps from static output #12749

Merged
merged 3 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/tame-spoons-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Clean server sourcemaps from static output
5 changes: 3 additions & 2 deletions packages/astro/src/core/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,16 @@ class AstroBuilder {
key: keyPromise,
};

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

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

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

// Write any additionally generated assets to disk.
this.timer.assetsStart = performance.now();
Expand Down
18 changes: 10 additions & 8 deletions packages/astro/src/core/build/static-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@
const ssrOutputs = viteBuildReturnToRollupOutputs(ssrOutput);
const clientOutputs = viteBuildReturnToRollupOutputs(clientOutput ?? []);
await runPostBuildHooks(container, ssrOutputs, clientOutputs);
let contentFileNames: string[] | undefined = undefined;
settings.timer.end('Client build');

// Free up memory
Expand All @@ -112,20 +111,19 @@
}
}

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

export async function staticBuild(
opts: StaticBuildOptions,
internals: BuildInternals,
ssrOutputChunkNames: string[],
contentFileNames?: string[],
) {
const { settings } = opts;
if (settings.buildOutput === 'static') {
settings.timer.start('Static generate');
await generatePages(opts, internals);
await cleanServerOutput(opts, ssrOutputChunkNames, contentFileNames, internals);
await cleanServerOutput(opts, ssrOutputChunkNames, internals);
settings.timer.end('Static generate');
} else if (settings.buildOutput === 'server') {
settings.timer.start('Server generate');
Expand Down Expand Up @@ -354,14 +352,12 @@
async function cleanServerOutput(
opts: StaticBuildOptions,
ssrOutputChunkNames: string[],
contentFileNames: string[] | undefined,
internals: BuildInternals,
) {
const out = getOutDirWithinCwd(opts.settings.config.outDir);
// The SSR output chunks for Astro are all .mjs files
const files = ssrOutputChunkNames
.filter((f) => f.endsWith('.mjs'))
.concat(contentFileNames ?? []);
.filter((f) => f.endsWith('.mjs'));
if (internals.manifestFileName) {
files.push(internals.manifestFileName);
}
Expand All @@ -370,7 +366,11 @@
await Promise.all(
files.map(async (filename) => {
const url = new URL(filename, out);
await fs.promises.rm(url);
const map = new URL(url + '.map');
await Promise.all([
fs.promises.rm(url),
fs.promises.rm(new URL(map)).catch((e) => {})

Check notice on line 372 in packages/astro/src/core/build/static-build.ts

View workflow job for this annotation

GitHub Actions / Lint

lint/correctness/noUnusedFunctionParameters

This parameter is unused.

Check notice on line 372 in packages/astro/src/core/build/static-build.ts

View workflow job for this annotation

GitHub Actions / Lint

lint/correctness/noUnusedVariables

This parameter is unused.
]);
}),
);

Expand Down Expand Up @@ -426,6 +426,8 @@
cwd: fileURLToPath(serverAssets),
});

console.log("FILES2", files);

Check warning on line 429 in packages/astro/src/core/build/static-build.ts

View workflow job for this annotation

GitHub Actions / Lint

lint/suspicious/noConsoleLog

Don't use console.log

if (files.length > 0) {
await Promise.all(
files.map(async function moveAsset(filename) {
Expand Down
8 changes: 8 additions & 0 deletions packages/astro/test/astro-basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,14 @@ describe('Astro basic build', () => {
assert.doesNotMatch(otherHtml, /<style/);
});

it('server sourcemaps not included in output', async () => {
const files = await fixture.readdir('/');
const hasSourcemaps = files.some(fileName => {
return fileName.endsWith('.map');
});
assert.equal(hasSourcemaps, false, 'no sourcemap files in output');
});

describe('preview', () => {
it('returns 200 for valid URLs', async () => {
const result = await fixture.fetch('/');
Expand Down
7 changes: 6 additions & 1 deletion packages/astro/test/fixtures/astro-basic/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@ import { defineConfig } from 'astro/config';
export default defineConfig({
integrations: [preact(), mdx()],
// make sure CLI flags have precedence
server: () => ({ port: 4321 })
server: () => ({ port: 4321 }),
vite: {
build: {
sourcemap: true,
}
}
});
10 changes: 8 additions & 2 deletions packages/astro/test/sourcemap.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ describe('Sourcemap', async () => {
});

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