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

fix: better assetsInlineLimit runtime type checking #10154

Merged
merged 8 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/late-bears-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Fixes a minor regression from 4.3.x when the vite.build.assetsInlineLimit configuration option was set to a string. Astro now automatically casts this to a number to match the Vite behaviour.
4 changes: 2 additions & 2 deletions packages/astro/src/core/build/plugins/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ export function shouldInlineAsset(
assetPath: string,
assetsInlineLimit: NonNullable<BuildOptions['assetsInlineLimit']>
) {
if (typeof assetsInlineLimit === 'number') {
return Buffer.byteLength(assetContent) < assetsInlineLimit;
if (typeof assetsInlineLimit === 'number' || typeof assetsInlineLimit === 'string') {
Cherry marked this conversation as resolved.
Show resolved Hide resolved
return Buffer.byteLength(assetContent) < Number(assetsInlineLimit);
Cherry marked this conversation as resolved.
Show resolved Hide resolved
}

const result = assetsInlineLimit(assetPath, Buffer.from(assetContent));
Expand Down
46 changes: 46 additions & 0 deletions packages/astro/test/css-assets.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,49 @@ describe('Assets in CSS', () => {
assert.equal(getAllMatches(/font-face/g, css), 1);
});
});

describe('Assets in CSS - string assetsInlineLimit', () => {
let fixture;

before(async () => {
fixture = await loadFixture({
root: './fixtures/css-assets/',
vite: {
build: {
assetsInlineLimit: '0',
},
},
});
await fixture.build();
});

function getAllMatches(re, text) {
let count = 0;
while (re.exec(text) !== null) {
++count;
}
return count;
}

async function getCSSForPage(pathname) {
const html = await fixture.readFile(pathname);
const $ = cheerio.load(html);
const cssPath = $('link').attr('href');
const css = await fixture.readFile(cssPath);
return css;
}

it('Bundled CSS does not have __VITE_ASSET__', async () => {
let css = await getCSSForPage('/one/index.html');
assert.equal(css.includes('__VITE_ASSET__'), false);
css = await getCSSForPage('/two/index.html');
assert.equal(css.includes('__VITE_ASSET__'), false);
});

it('Pages contain only their own CSS', async () => {
let css = await getCSSForPage('/one/index.html');
assert.equal(getAllMatches(/font-face/g, css), 1);
css = await getCSSForPage('/two/index.html');
assert.equal(getAllMatches(/font-face/g, css), 1);
});
});
Loading