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
This commit is contained in:
parent
5126c6a40f
commit
7d95bd9baa
16 changed files with 118 additions and 8 deletions
5
.changeset/chatty-walls-happen.md
Normal file
5
.changeset/chatty-walls-happen.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fixed a case where dynamic imports tried to preload inlined stylesheets.
|
5
.changeset/fair-countries-admire.md
Normal file
5
.changeset/fair-countries-admire.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@astrojs/svelte': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Removed vite warnings.
|
|
@ -13,7 +13,7 @@ module.exports = {
|
||||||
rules: {
|
rules: {
|
||||||
// These off/configured-differently-by-default rules fit well for us
|
// These off/configured-differently-by-default rules fit well for us
|
||||||
'@typescript-eslint/array-type': ['error', { default: 'array-simple' }],
|
'@typescript-eslint/array-type': ['error', { default: 'array-simple' }],
|
||||||
'@typescript-eslint/no-unused-vars': ['error', { ignoreRestSiblings: true }],
|
'@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: "^_", ignoreRestSiblings: true }],
|
||||||
'no-only-tests/no-only-tests': 'error',
|
'no-only-tests/no-only-tests': 'error',
|
||||||
'@typescript-eslint/no-shadow': ['error'],
|
'@typescript-eslint/no-shadow': ['error'],
|
||||||
'no-console': 'warn',
|
'no-console': 'warn',
|
||||||
|
|
|
@ -121,8 +121,6 @@ export class App {
|
||||||
}
|
}
|
||||||
return pathname;
|
return pathname;
|
||||||
}
|
}
|
||||||
// Disable no-unused-vars to avoid breaking signature change
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
||||||
match(request: Request, _opts: MatchOptions = {}): RouteData | undefined {
|
match(request: Request, _opts: MatchOptions = {}): RouteData | undefined {
|
||||||
const url = new URL(request.url);
|
const url = new URL(request.url);
|
||||||
// ignore requests matching public assets
|
// ignore requests matching public assets
|
||||||
|
|
|
@ -200,7 +200,7 @@ function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[] {
|
||||||
const inlineConfig = settings.config.build.inlineStylesheets;
|
const inlineConfig = settings.config.build.inlineStylesheets;
|
||||||
const { assetsInlineLimit = 4096 } = settings.config.vite?.build ?? {};
|
const { assetsInlineLimit = 4096 } = settings.config.vite?.build ?? {};
|
||||||
|
|
||||||
Object.entries(bundle).forEach(([id, stylesheet]) => {
|
Object.entries(bundle).forEach(([_, stylesheet]) => {
|
||||||
if (
|
if (
|
||||||
stylesheet.type !== 'asset' ||
|
stylesheet.type !== 'asset' ||
|
||||||
stylesheet.name?.endsWith('.css') !== true ||
|
stylesheet.name?.endsWith('.css') !== true ||
|
||||||
|
@ -217,8 +217,6 @@ function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[] {
|
||||||
? false
|
? false
|
||||||
: assetSize <= assetsInlineLimit;
|
: assetSize <= assetsInlineLimit;
|
||||||
|
|
||||||
if (toBeInlined) delete bundle[id];
|
|
||||||
|
|
||||||
// there should be a single js object for each stylesheet,
|
// there should be a single js object for each stylesheet,
|
||||||
// allowing the single reference to be shared and checked for duplicates
|
// allowing the single reference to be shared and checked for duplicates
|
||||||
const sheet: StylesheetAsset = toBeInlined
|
const sheet: StylesheetAsset = toBeInlined
|
||||||
|
|
36
packages/astro/test/css-dangling-references.test.js
Normal file
36
packages/astro/test/css-dangling-references.test.js
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
import { expect } from 'chai';
|
||||||
|
import { loadFixture } from './test-utils.js';
|
||||||
|
|
||||||
|
const cssAssetReferenceRegExp = /_astro\/[A-Za-z0-9\-]+\.[a0-9a-f]{8}\.css/g
|
||||||
|
|
||||||
|
describe("When Vite's preloadModule polyfill is used", async () => {
|
||||||
|
|
||||||
|
let fixture;
|
||||||
|
|
||||||
|
before(async () => {
|
||||||
|
fixture = await loadFixture({
|
||||||
|
root: './fixtures/css-dangling-references/'
|
||||||
|
});
|
||||||
|
await fixture.build();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('there are no references to deleted CSS chunks', async () => {
|
||||||
|
|
||||||
|
const fileNames = await fixture.readdir('/_astro/')
|
||||||
|
const filePaths = fileNames.map(filename => '_astro/' + filename)
|
||||||
|
|
||||||
|
const expectations =
|
||||||
|
filePaths
|
||||||
|
.filter(filePath => filePath.endsWith('js'))
|
||||||
|
.map(async filePath => {
|
||||||
|
const contents = await fixture.readFile(filePath)
|
||||||
|
const cssReferences = contents.match(cssAssetReferenceRegExp)
|
||||||
|
|
||||||
|
if (cssReferences === null) return
|
||||||
|
|
||||||
|
expect(filePaths).to.contain.members(cssReferences, filePath + ' contains a reference to a deleted css asset: ' + cssReferences)
|
||||||
|
})
|
||||||
|
|
||||||
|
await Promise.all(expectations)
|
||||||
|
})
|
||||||
|
})
|
8
packages/astro/test/fixtures/css-dangling-references/astro.config.ts
vendored
Normal file
8
packages/astro/test/fixtures/css-dangling-references/astro.config.ts
vendored
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
import { defineConfig } from 'astro/config';
|
||||||
|
import svelte from '@astrojs/svelte';
|
||||||
|
|
||||||
|
// https://astro.build/config
|
||||||
|
export default defineConfig({
|
||||||
|
integrations: [svelte()],
|
||||||
|
});
|
||||||
|
|
11
packages/astro/test/fixtures/css-dangling-references/package.json
vendored
Normal file
11
packages/astro/test/fixtures/css-dangling-references/package.json
vendored
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
{
|
||||||
|
"name": "@test/css-dangling-references",
|
||||||
|
"version": "0.0.0",
|
||||||
|
"private": true,
|
||||||
|
"dependencies": {
|
||||||
|
"astro": "workspace:*",
|
||||||
|
"@astrojs/svelte": "workspace:*",
|
||||||
|
"svelte": "4"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
<style>
|
||||||
|
h1 {
|
||||||
|
background-color: gold;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
<h1>This sentence should have a gold background.</h1>
|
|
@ -0,0 +1,6 @@
|
||||||
|
<style>
|
||||||
|
p {
|
||||||
|
background-color: lavender;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
<p>This sentence should have a lavender background color.</p>
|
15
packages/astro/test/fixtures/css-dangling-references/src/components/Wrapper.svelte
vendored
Normal file
15
packages/astro/test/fixtures/css-dangling-references/src/components/Wrapper.svelte
vendored
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
<script>
|
||||||
|
|
||||||
|
export let path
|
||||||
|
|
||||||
|
const allAppModules = import.meta.glob('./*.svelte')
|
||||||
|
|
||||||
|
const AppModule = Object.entries(allAppModules).find(
|
||||||
|
([key]) => key.includes(path)
|
||||||
|
)[1]
|
||||||
|
|
||||||
|
</script>
|
||||||
|
|
||||||
|
{#await AppModule() then Mod}
|
||||||
|
<Mod.default />
|
||||||
|
{/await}
|
4
packages/astro/test/fixtures/css-dangling-references/src/pages/glob-import-1.astro
vendored
Normal file
4
packages/astro/test/fixtures/css-dangling-references/src/pages/glob-import-1.astro
vendored
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
---
|
||||||
|
import Wrapper from "../components/Wrapper.svelte"
|
||||||
|
---
|
||||||
|
<Wrapper path="1" client:load/>
|
5
packages/astro/test/fixtures/css-dangling-references/src/pages/glob-import-2.astro
vendored
Normal file
5
packages/astro/test/fixtures/css-dangling-references/src/pages/glob-import-2.astro
vendored
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
import Wrapper from "../components/Wrapper.svelte"
|
||||||
|
---
|
||||||
|
<Wrapper path="2" client:load/>
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
{
|
{
|
||||||
"name": "@test/css-inline-stylesheets-always",
|
"name": "@test/css-inline-stylesheets",
|
||||||
"version": "0.0.0",
|
"version": "0.0.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|
|
@ -17,7 +17,8 @@ async function svelteConfigHasPreprocess(root: URL) {
|
||||||
for (const file of svelteConfigFiles) {
|
for (const file of svelteConfigFiles) {
|
||||||
const filePath = fileURLToPath(new URL(file, root));
|
const filePath = fileURLToPath(new URL(file, root));
|
||||||
try {
|
try {
|
||||||
const config = (await import(filePath)).default;
|
// Suppress warnings by vite: "The above dynamic import cannot be analyzed by Vite."
|
||||||
|
const config = (await import(/* @vite-ignore */ filePath)).default;
|
||||||
return !!config.preprocess;
|
return !!config.preprocess;
|
||||||
} catch {}
|
} catch {}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2437,6 +2437,18 @@ importers:
|
||||||
|
|
||||||
packages/astro/test/fixtures/css-assets/packages/font-awesome: {}
|
packages/astro/test/fixtures/css-assets/packages/font-awesome: {}
|
||||||
|
|
||||||
|
packages/astro/test/fixtures/css-dangling-references:
|
||||||
|
dependencies:
|
||||||
|
'@astrojs/svelte':
|
||||||
|
specifier: workspace:*
|
||||||
|
version: link:../../../../integrations/svelte
|
||||||
|
astro:
|
||||||
|
specifier: workspace:*
|
||||||
|
version: link:../../..
|
||||||
|
svelte:
|
||||||
|
specifier: '4'
|
||||||
|
version: 4.2.0
|
||||||
|
|
||||||
packages/astro/test/fixtures/css-import-as-inline:
|
packages/astro/test/fixtures/css-import-as-inline:
|
||||||
dependencies:
|
dependencies:
|
||||||
astro:
|
astro:
|
||||||
|
|
Loading…
Reference in a new issue