Skip to content

Commit

Permalink
Prevent inlining SCSS partials in dev (#5477)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Nov 28, 2022
1 parent c137752 commit 5e693c2
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/twelve-kings-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Prevent inlining SCSS partials in dev
11 changes: 9 additions & 2 deletions packages/astro/src/core/render/dev/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@ export async function getStylesForURL(
for await (const importedModule of crawlGraph(loader, viteID(filePath), true)) {
const ext = path.extname(importedModule.url).toLowerCase();
if (STYLE_EXTENSIONS.has(ext)) {
// The SSR module is possibly not loaded. Load it if it's null.
const ssrModule = importedModule.ssrModule ?? (await loader.import(importedModule.url));
let ssrModule: Record<string, any>;
try {
// The SSR module is possibly not loaded. Load it if it's null.
ssrModule = importedModule.ssrModule ?? (await loader.import(importedModule.url));
} catch {
// The module may not be inline-able, e.g. SCSS partials. Skip it as it may already
// be inlined into other modules if it happens to be in the graph.
continue;
}
if (
mode === 'development' && // only inline in development
typeof ssrModule?.default === 'string' // ignore JS module styles
Expand Down
3 changes: 3 additions & 0 deletions packages/astro/test/fixtures/0-css/src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ import raw from '../styles/raw.css?raw'
<style lang="sass">
@import '../styles/linked.sass'
</style>
<style lang="scss">
@import '../styles/partial-main.scss';
</style>
</head>
<body>
<div class="wrapper">
Expand Down
3 changes: 3 additions & 0 deletions packages/astro/test/fixtures/0-css/src/styles/_partial-1.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@mixin make-red {
color: red;
}
5 changes: 5 additions & 0 deletions packages/astro/test/fixtures/0-css/src/styles/_partial-2.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// relies on partial-1. make sure astro don't inline this style.

.partial-test {
@include make-red;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@import './partial-1';
@import './partial-2';

0 comments on commit 5e693c2

Please sign in to comment.