Skip to content

Commit

Permalink
Fix use of preprocessors with the static build (#2490)
Browse files Browse the repository at this point in the history
* Fix use of preprocessors with the static build

* Adds a changeset
  • Loading branch information
matthewp committed Jan 28, 2022
1 parent aebe7f5 commit 69d5b70
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-dots-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix for CSS preprocessing using the static build
6 changes: 6 additions & 0 deletions examples/fast-build/src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ import ExternalHoisted from '../components/ExternalHoisted.astro';
color: salmon;
}
</style>
<style lang="scss">
$color: purple;
h2 {
color: purple;
}
</style>
</head>
<body>
<section>
Expand Down
20 changes: 12 additions & 8 deletions packages/astro/src/vite-plugin-astro/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,18 @@ async function compile(config: AstroConfig, filename: string, source: string, vi
experimentalStaticExtraction: config.buildOptions.experimentalStaticBuild,
// TODO add experimental flag here
preprocessStyle: async (value: string, attrs: Record<string, string>) => {
// When using this flag CSS is added via <link> and therefore goes
// through Vite's CSS pipeline. We don't need to transform here, it will be
// transformed on CSS requests.
if (config.buildOptions.experimentalStaticBuild) {
return { code: value };
}

const lang = `.${attrs?.lang || 'css'}`.toLowerCase();
try {
let prefix = '';
// In the static build, strip away at-imports so that they can be resolved
// by the pseudo-module that gets created.
if(config.buildOptions.experimentalStaticBuild) {
value = value.replace(/(?:@import)\s(?:url\()?\s?["\'](.*?)["\']\s?\)?(?:[^;]*);?/gi, (match) => {
prefix += match;
// Replace with an empty string of the same length, to preserve source maps.
return new Array(match.length).fill(' ').join('');
});
}
const result = await transformWithVite({
value,
lang,
Expand All @@ -76,7 +79,8 @@ async function compile(config: AstroConfig, filename: string, source: string, vi
map = result.map.toString();
}
}
return { code: result.code, map };
const code = prefix += result.code;
return { code, map };
} catch (err) {
// save error to throw in plugin context
cssTransformError = err as any;
Expand Down

0 comments on commit 69d5b70

Please sign in to comment.