Skip to content
Open
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/fix-dev-quit-fonts-dist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes an issue where stopping the dev server with `q+enter` incorrectly created a `dist` folder and copied font files when using the experimental Fonts API
60 changes: 30 additions & 30 deletions packages/astro/src/assets/fonts/vite-plugin-fonts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,38 +308,38 @@ export function fontsPlugin({ settings, sync, logger }: Options): Plugin {
}
},
async buildEnd() {
if (sync || settings.config.experimental.fonts!.length === 0) {
cleanup();
return;
}
if (sync || settings.config.experimental.fonts!.length === 0 || !isBuild) {
cleanup();
return;
}

try {
const dir = getClientOutputDirectory(settings);
const fontsDir = new URL(`.${assetsDir}`, dir);
try {
const dir = getClientOutputDirectory(settings);
const fontsDir = new URL(`.${assetsDir}`, dir);
try {
mkdirSync(fontsDir, { recursive: true });
} catch (cause) {
throw new AstroError(AstroErrorData.UnknownFilesystemError, { cause });
}
if (fontFileDataMap) {
logger.info(
'assets',
`Copying fonts (${fontFileDataMap.size} file${fontFileDataMap.size === 1 ? '' : 's'})...`,
);
await Promise.all(
Array.from(fontFileDataMap.entries()).map(async ([hash, associatedData]) => {
const data = await fontFetcher!.fetch({ hash, ...associatedData });
try {
writeFileSync(new URL(hash, fontsDir), data);
} catch (cause) {
throw new AstroError(AstroErrorData.UnknownFilesystemError, { cause });
}
}),
);
}
} finally {
cleanup();
mkdirSync(fontsDir, { recursive: true });
} catch (cause) {
throw new AstroError(AstroErrorData.UnknownFilesystemError, { cause });
}
},
if (fontFileDataMap) {
logger.info(
'assets',
`Copying fonts (${fontFileDataMap.size} file${fontFileDataMap.size === 1 ? '' : 's'})...`,
);
await Promise.all(
Array.from(fontFileDataMap.entries()).map(async ([hash, associatedData]) => {
const data = await fontFetcher!.fetch({ hash, ...associatedData });
try {
writeFileSync(new URL(hash, fontsDir), data);
} catch (cause) {
throw new AstroError(AstroErrorData.UnknownFilesystemError, { cause });
}
}),
);
}
} finally {
cleanup();
}
},
};
}
28 changes: 28 additions & 0 deletions packages/astro/test/fonts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,34 @@ describe('astro fonts', () => {
assert.equal(parsed[0].src[0].url.startsWith('/_astro/fonts/'), true);
});
});

it('Does not create dist folder or copy fonts when dev server stops', async () => {
const { fixture, run } = await createDevFixture({
experimental: {
fonts: [
{
name: 'Poppins',
cssVariable: '--font-test',
provider: fontProviders.fontsource(),
weights: [400, 500],
},
],
},
});
await run(async () => {
await fixture.fetch('/');
});


try {
await readdir(new URL('./dist/', fixture.config.root));
assert.fail('dist folder should not exist after dev server shutdown');
} catch (err) {
if (typeof err === 'object' && err && 'code' in err && err.code !== 'ENOENT') {
throw err;
}
}
});
});

describe('build', () => {
Expand Down
Loading