Skip to content

Commit 7d95bd9

Browse files
authored
Allow vite to refer to inlined CSS (#8351)
* fix(client build): keep inlined stylesheets * add changesets * appease linter * eslint: allow variables beginning with an underscore to be unused * remove eslint-ignore that's no longer needed * ready for review
1 parent 5126c6a commit 7d95bd9

16 files changed

+118
-8
lines changed

.changeset/chatty-walls-happen.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixed a case where dynamic imports tried to preload inlined stylesheets.

.changeset/fair-countries-admire.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@astrojs/svelte': patch
3+
---
4+
5+
Removed vite warnings.

.eslintrc.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = {
1313
rules: {
1414
// These off/configured-differently-by-default rules fit well for us
1515
'@typescript-eslint/array-type': ['error', { default: 'array-simple' }],
16-
'@typescript-eslint/no-unused-vars': ['error', { ignoreRestSiblings: true }],
16+
'@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: "^_", ignoreRestSiblings: true }],
1717
'no-only-tests/no-only-tests': 'error',
1818
'@typescript-eslint/no-shadow': ['error'],
1919
'no-console': 'warn',

packages/astro/src/core/app/index.ts

-2
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,6 @@ export class App {
121121
}
122122
return pathname;
123123
}
124-
// Disable no-unused-vars to avoid breaking signature change
125-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
126124
match(request: Request, _opts: MatchOptions = {}): RouteData | undefined {
127125
const url = new URL(request.url);
128126
// ignore requests matching public assets

packages/astro/src/core/build/plugins/plugin-css.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[] {
200200
const inlineConfig = settings.config.build.inlineStylesheets;
201201
const { assetsInlineLimit = 4096 } = settings.config.vite?.build ?? {};
202202

203-
Object.entries(bundle).forEach(([id, stylesheet]) => {
203+
Object.entries(bundle).forEach(([_, stylesheet]) => {
204204
if (
205205
stylesheet.type !== 'asset' ||
206206
stylesheet.name?.endsWith('.css') !== true ||
@@ -217,8 +217,6 @@ function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[] {
217217
? false
218218
: assetSize <= assetsInlineLimit;
219219

220-
if (toBeInlined) delete bundle[id];
221-
222220
// there should be a single js object for each stylesheet,
223221
// allowing the single reference to be shared and checked for duplicates
224222
const sheet: StylesheetAsset = toBeInlined
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { expect } from 'chai';
2+
import { loadFixture } from './test-utils.js';
3+
4+
const cssAssetReferenceRegExp = /_astro\/[A-Za-z0-9\-]+\.[a0-9a-f]{8}\.css/g
5+
6+
describe("When Vite's preloadModule polyfill is used", async () => {
7+
8+
let fixture;
9+
10+
before(async () => {
11+
fixture = await loadFixture({
12+
root: './fixtures/css-dangling-references/'
13+
});
14+
await fixture.build();
15+
});
16+
17+
it('there are no references to deleted CSS chunks', async () => {
18+
19+
const fileNames = await fixture.readdir('/_astro/')
20+
const filePaths = fileNames.map(filename => '_astro/' + filename)
21+
22+
const expectations =
23+
filePaths
24+
.filter(filePath => filePath.endsWith('js'))
25+
.map(async filePath => {
26+
const contents = await fixture.readFile(filePath)
27+
const cssReferences = contents.match(cssAssetReferenceRegExp)
28+
29+
if (cssReferences === null) return
30+
31+
expect(filePaths).to.contain.members(cssReferences, filePath + ' contains a reference to a deleted css asset: ' + cssReferences)
32+
})
33+
34+
await Promise.all(expectations)
35+
})
36+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { defineConfig } from 'astro/config';
2+
import svelte from '@astrojs/svelte';
3+
4+
// https://astro.build/config
5+
export default defineConfig({
6+
integrations: [svelte()],
7+
});
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "@test/css-dangling-references",
3+
"version": "0.0.0",
4+
"private": true,
5+
"dependencies": {
6+
"astro": "workspace:*",
7+
"@astrojs/svelte": "workspace:*",
8+
"svelte": "4"
9+
}
10+
}
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<style>
2+
h1 {
3+
background-color: gold;
4+
}
5+
</style>
6+
<h1>This sentence should have a gold background.</h1>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<style>
2+
p {
3+
background-color: lavender;
4+
}
5+
</style>
6+
<p>This sentence should have a lavender background color.</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<script>
2+
3+
export let path
4+
5+
const allAppModules = import.meta.glob('./*.svelte')
6+
7+
const AppModule = Object.entries(allAppModules).find(
8+
([key]) => key.includes(path)
9+
)[1]
10+
11+
</script>
12+
13+
{#await AppModule() then Mod}
14+
<Mod.default />
15+
{/await}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
import Wrapper from "../components/Wrapper.svelte"
3+
---
4+
<Wrapper path="1" client:load/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
import Wrapper from "../components/Wrapper.svelte"
3+
---
4+
<Wrapper path="2" client:load/>
5+

packages/astro/test/fixtures/css-inline-stylesheets/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "@test/css-inline-stylesheets-always",
2+
"name": "@test/css-inline-stylesheets",
33
"version": "0.0.0",
44
"private": true,
55
"dependencies": {

packages/integrations/svelte/src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ async function svelteConfigHasPreprocess(root: URL) {
1717
for (const file of svelteConfigFiles) {
1818
const filePath = fileURLToPath(new URL(file, root));
1919
try {
20-
const config = (await import(filePath)).default;
20+
// Suppress warnings by vite: "The above dynamic import cannot be analyzed by Vite."
21+
const config = (await import(/* @vite-ignore */ filePath)).default;
2122
return !!config.preprocess;
2223
} catch {}
2324
}

pnpm-lock.yaml

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)