Skip to content

Commit

Permalink
fix: handle empty preprocessed style (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
natemoo-re authored Nov 23, 2021
1 parent 44ee189 commit 9557113
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/short-chairs-attack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Fix panic when preprocessed style is empty
3 changes: 3 additions & 0 deletions cmd/astro-wasm/astro-wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ type TransformResult struct {
// This is spawned as a goroutine to preprocess style nodes using an async function passed from JS
func preprocessStyle(i int, style *astro.Node, transformOptions transform.TransformOptions, cb func()) {
defer cb()
if style.FirstChild == nil {
return
}
attrs := wasm_utils.GetAttrs(style)
data, _ := wasm_utils.Await(transformOptions.PreprocessStyle.(js.Value).Invoke(style.FirstChild.Data, attrs))
// note: Rollup (and by extension our Astro Vite plugin) allows for "undefined" and "null" responses if a transform wishes to skip this occurrence
Expand Down
50 changes: 50 additions & 0 deletions lib/compiler/test/empty-style.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { transform } from '@astrojs/compiler';
import sass from 'sass';

function transformSass(value) {
return new Promise((resolve, reject) => {
sass.render({ data: value }, (err, result) => {
if (err) {
reject(err);
return;
}
resolve({ code: result.css.toString('utf8'), map: result.map });
return;
});
});
}

async function run() {
const result = await transform(
`---
let value = 'world';
---
<style lang="scss"></style>
<div>Hello world!</div>
<div>Ahhh</div>
`,
{
sourcemap: true,
preprocessStyle: async (value, attrs) => {
if (!attrs.lang) {
return null;
}
if (attrs.lang === 'scss') {
try {
return transformSass(value);
} catch (err) {
console.error(err);
}
}
return null;
},
}
);

console.log(result);
}

await run();
1 change: 1 addition & 0 deletions lib/compiler/test/test.mjs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import './basic.test.mjs';
import './empty-style.test.mjs';
import './output.test.mjs';

0 comments on commit 9557113

Please sign in to comment.