Skip to content

Commit

Permalink
Fix: pass Astro config postcss to Svelte preprocess (withastro#3685)
Browse files Browse the repository at this point in the history
* fix: pass Astro config postcss to Svelte preprocess

* test: preset env for nested styles

* chore: changeset
  • Loading branch information
bholmesdev authored Jun 22, 2022
1 parent cec8c0d commit 3b9b3b6
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 24 deletions.
3 changes: 3 additions & 0 deletions test/fixtures/postcss/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
"astro": "workspace:*",
"autoprefixer": "^10.4.7",
"postcss": "^8.4.14"
},
"devDependencies": {
"postcss-preset-env": "^7.7.1"
}
}
15 changes: 10 additions & 5 deletions test/fixtures/postcss/postcss.config.cjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
const postcssPresetEnv = require('postcss-preset-env')
const autoPrefixer = require('autoprefixer')

module.exports = {
plugins: {
autoprefixer: {
plugins: [
// included to ensure public/ CSS resources are NOT transformed
autoPrefixer({
overrideBrowserslist: ['> 0.1%', 'IE 11'] // enforce `appearance: none;` is prefixed with -webkit and -moz
}
}
};
}),
postcssPresetEnv({ features: { 'nesting-rules': true } }),
]
}
8 changes: 5 additions & 3 deletions test/fixtures/postcss/src/components/Astro.astro
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<style>
.astro-component {
appearance: none;
&.nested {
color: red;
}
}
</style>

<div class="astro-component">
Astro
<div class="astro-component nested">
Astro
</div>
4 changes: 3 additions & 1 deletion test/fixtures/postcss/src/components/Solid.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.solid {
appearance: none;
&.nested {
color: red;
}
}
4 changes: 2 additions & 2 deletions test/fixtures/postcss/src/components/Solid.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import './Solid.css';

export default function Counter() {
return (
<div class="solid">
Solid
<div class="solid nested">
Solid
</div>
);
}
8 changes: 5 additions & 3 deletions test/fixtures/postcss/src/components/Svelte.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<style>
.svelte {
appearance: none;
&.nested {
color: red;
}
}
</style>

<div class="svelte">
Svelte
<div class="svelte nested">
Svelte
</div>
8 changes: 5 additions & 3 deletions test/fixtures/postcss/src/components/Vue.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<style>
.vue {
appearance: none;
&.nested {
color: red;
}
}
</style>

<template>
<div class="vue">
Vue
<div class="vue nested">
Vue
</div>
</template>

6 changes: 4 additions & 2 deletions test/fixtures/postcss/src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import Vue from '../components/Vue.vue';
</style>
<style>
.astro-page {
appearance: none;
&.nested {
color: red;
}
}
</style>
</head>

<body>
<div class="astro-page">
<div class="astro-page nested">
<AstroComponent />
<JSX />
<Svelte />
Expand Down
11 changes: 6 additions & 5 deletions test/postcss.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,25 @@ describe('PostCSS', () => {
.replace('/n', '');
});

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

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

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

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

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

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

0 comments on commit 3b9b3b6

Please sign in to comment.