mirror of
https://github.com/withastro/astro.git
synced 2024-12-23 21:53:55 -05:00
Co-authored-by: Nate Moore <nate@astro.build>
This commit is contained in:
parent
fbb34e1ef6
commit
e779c62424
2 changed files with 14 additions and 2 deletions
5
.changeset/neat-actors-switch.md
Normal file
5
.changeset/neat-actors-switch.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Improve error handling when top-level `return` is present
|
|
@ -93,7 +93,8 @@ async function enhanceCompileError({
|
||||||
source,
|
source,
|
||||||
config,
|
config,
|
||||||
logging,
|
logging,
|
||||||
}: EnhanceCompilerErrorOptions): Promise<never> {
|
}: EnhanceCompilerErrorOptions): Promise<void> {
|
||||||
|
const lineText = (err as any).loc?.lineText;
|
||||||
// Verify frontmatter: a common reason that this plugin fails is that
|
// Verify frontmatter: a common reason that this plugin fails is that
|
||||||
// the user provided invalid JS/TS in the component frontmatter.
|
// the user provided invalid JS/TS in the component frontmatter.
|
||||||
// If the frontmatter is invalid, the `err` object may be a compiler
|
// If the frontmatter is invalid, the `err` object may be a compiler
|
||||||
|
@ -104,8 +105,14 @@ async function enhanceCompileError({
|
||||||
// If frontmatter is valid or cannot be parsed, then continue.
|
// If frontmatter is valid or cannot be parsed, then continue.
|
||||||
const scannedFrontmatter = FRONTMATTER_PARSE_REGEXP.exec(source);
|
const scannedFrontmatter = FRONTMATTER_PARSE_REGEXP.exec(source);
|
||||||
if (scannedFrontmatter) {
|
if (scannedFrontmatter) {
|
||||||
|
// Top-level return is not supported, so replace `return` with throw
|
||||||
|
const frontmatter = scannedFrontmatter[1].replace(/\breturn\b/g, 'throw');
|
||||||
|
|
||||||
|
// If frontmatter does not actually include the offending line, skip
|
||||||
|
if (lineText && !frontmatter.includes(lineText)) throw err;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await transformWithEsbuild(scannedFrontmatter[1], id, {
|
await transformWithEsbuild(frontmatter, id, {
|
||||||
loader: 'ts',
|
loader: 'ts',
|
||||||
target: 'esnext',
|
target: 'esnext',
|
||||||
sourcemap: false,
|
sourcemap: false,
|
||||||
|
|
Loading…
Reference in a new issue