Skip to content

Commit 3d554fd

Browse files
authored
Fix: pass Astro config postcss to Svelte preprocess (#3685)
* fix: pass Astro config postcss to Svelte preprocess * test: preset env for nested styles * chore: changeset
1 parent a3654a7 commit 3d554fd

File tree

12 files changed

+565
-30
lines changed

12 files changed

+565
-30
lines changed

.changeset/empty-chicken-refuse.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'astro': patch
3+
'@astrojs/svelte': patch
4+
---
5+
6+
Fix PostCSS config not applied to Svelte component by default

packages/astro/test/fixtures/postcss/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,8 @@
99
"astro": "workspace:*",
1010
"autoprefixer": "^10.4.7",
1111
"postcss": "^8.4.14"
12+
},
13+
"devDependencies": {
14+
"postcss-preset-env": "^7.7.1"
1215
}
1316
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
const postcssPresetEnv = require('postcss-preset-env')
2+
const autoPrefixer = require('autoprefixer')
3+
14
module.exports = {
2-
plugins: {
3-
autoprefixer: {
5+
plugins: [
6+
// included to ensure public/ CSS resources are NOT transformed
7+
autoPrefixer({
48
overrideBrowserslist: ['> 0.1%', 'IE 11'] // enforce `appearance: none;` is prefixed with -webkit and -moz
5-
}
6-
}
7-
};
9+
}),
10+
postcssPresetEnv({ features: { 'nesting-rules': true } }),
11+
]
12+
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<style>
22
.astro-component {
3-
appearance: none;
3+
&.nested {
4+
color: red;
5+
}
46
}
57
</style>
68

7-
<div class="astro-component">
8-
Astro
9+
<div class="astro-component nested">
10+
Astro
911
</div>
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
.solid {
2-
appearance: none;
2+
&.nested {
3+
color: red;
4+
}
35
}

packages/astro/test/fixtures/postcss/src/components/Solid.jsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import './Solid.css';
33

44
export default function Counter() {
55
return (
6-
<div class="solid">
7-
Solid
6+
<div class="solid nested">
7+
Solid
88
</div>
99
);
1010
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<style>
22
.svelte {
3-
appearance: none;
3+
&.nested {
4+
color: red;
5+
}
46
}
57
</style>
68

7-
<div class="svelte">
8-
Svelte
9+
<div class="svelte nested">
10+
Svelte
911
</div>
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<style>
22
.vue {
3-
appearance: none;
3+
&.nested {
4+
color: red;
5+
}
46
}
57
</style>
68

79
<template>
8-
<div class="vue">
9-
Vue
10+
<div class="vue nested">
11+
Vue
1012
</div>
1113
</template>
1214

packages/astro/test/fixtures/postcss/src/pages/index.astro

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ import Vue from '../components/Vue.vue';
1212
</style>
1313
<style>
1414
.astro-page {
15-
appearance: none;
15+
&.nested {
16+
color: red;
17+
}
1618
}
1719
</style>
1820
</head>
1921

2022
<body>
21-
<div class="astro-page">
23+
<div class="astro-page nested">
2224
<AstroComponent />
2325
<JSX />
2426
<Svelte />

packages/astro/test/postcss.test.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,25 @@ describe('PostCSS', () => {
2323
.replace('/n', '');
2424
});
2525

26+
/** All test cases check whether nested styles (i.e. &.nested {}) are correctly transformed */
2627
it('works in Astro page styles', () => {
27-
expect(bundledCSS).to.match(new RegExp(`.astro-page.astro-[^{]+${PREFIXED_CSS}`));
28+
expect(bundledCSS).to.match(new RegExp(`\.astro-page(\.(\w|-)*)*\.nested`));
2829
});
2930

3031
it('works in Astro component styles', () => {
31-
expect(bundledCSS).to.match(new RegExp(`.astro-component.astro-[^{]+${PREFIXED_CSS}`));
32+
expect(bundledCSS).to.match(new RegExp(`\.astro-component(\.(\w|-)*)*\.nested`));
3233
});
3334

3435
it('works in JSX', () => {
35-
expect(bundledCSS).to.match(new RegExp(`.solid[^{]*${PREFIXED_CSS}`));
36+
expect(bundledCSS).to.match(new RegExp(`\.solid(\.(\w|-)*)*\.nested`));
3637
});
3738

3839
it('works in Vue', () => {
39-
expect(bundledCSS).to.match(new RegExp(`.vue[^{]*${PREFIXED_CSS}`));
40+
expect(bundledCSS).to.match(new RegExp(`\.vue(\.(\w|-)*)*\.nested`));
4041
});
4142

4243
it('works in Svelte', () => {
43-
expect(bundledCSS).to.match(new RegExp(`.svelte.s[^{]+${PREFIXED_CSS}`));
44+
expect(bundledCSS).to.match(new RegExp(`\.svelte(\.(\w|-)*)*\.nested`));
4445
});
4546

4647
it('ignores CSS in public/', async () => {

packages/integrations/svelte/src/index.ts

+19-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Options } from '@sveltejs/vite-plugin-svelte';
2+
import type { AstroIntegration, AstroRenderer, AstroConfig } from 'astro';
3+
import type { UserConfig } from 'vite'
24
import { svelte } from '@sveltejs/vite-plugin-svelte';
3-
import type { AstroIntegration, AstroRenderer } from 'astro';
45
import preprocess from 'svelte-preprocess';
56

67
function getRenderer(): AstroRenderer {
@@ -11,13 +12,20 @@ function getRenderer(): AstroRenderer {
1112
};
1213
}
1314

14-
function getViteConfiguration(isDev: boolean, options?: Options | OptionsCallback) {
15-
const defaultOptions = {
15+
type ViteConfigurationArgs = {
16+
isDev: boolean;
17+
options?: Options | OptionsCallback;
18+
postcssConfig: AstroConfig['style']['postcss'];
19+
}
20+
21+
function getViteConfiguration({ options, postcssConfig, isDev }: ViteConfigurationArgs): UserConfig {
22+
const defaultOptions: Partial<Options> = {
1623
emitCss: true,
1724
compilerOptions: { dev: isDev, hydratable: true },
1825
preprocess: [
1926
preprocess({
2027
less: true,
28+
postcss: postcssConfig,
2129
sass: { renderSync: true },
2230
scss: { renderSync: true },
2331
stylus: true,
@@ -61,9 +69,15 @@ export default function (options?: Options | OptionsCallback): AstroIntegration
6169
name: '@astrojs/svelte',
6270
hooks: {
6371
// Anything that gets returned here is merged into Astro Config
64-
'astro:config:setup': ({ command, updateConfig, addRenderer }) => {
72+
'astro:config:setup': ({ command, updateConfig, addRenderer, config }) => {
6573
addRenderer(getRenderer());
66-
updateConfig({ vite: getViteConfiguration(command === 'dev', options) });
74+
updateConfig({
75+
vite: getViteConfiguration({
76+
options,
77+
isDev: command === 'dev',
78+
postcssConfig: config.style.postcss,
79+
})
80+
});
6781
},
6882
},
6983
};

0 commit comments

Comments
 (0)