Skip to content

Commit 64ad606

Browse files
committed
Lazy load astro only if available
1 parent 76607eb commit 64ad606

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/index.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ import requireFrom from 'import-from'
1818
import requireFresh from 'import-fresh'
1919
import objectHash from 'object-hash'
2020
import * as svelte from 'prettier-plugin-svelte'
21-
import * as astro from 'prettier-plugin-astro'
2221
import lineColumn from 'line-column'
2322
import jsesc from 'jsesc'
2423
import escalade from 'escalade/sync'
2524

25+
// We need to load this plugin dynamically because it's not available by default
26+
// And we are not bundling it with the main Prettier plugin
27+
let astro = loadIfExists('prettier-plugin-astro')
28+
2629
let contextMap = new Map()
2730

2831
function bigSign(bigIntValue) {
@@ -448,7 +451,7 @@ export const parsers = {
448451
transformSvelte(ast.html, { env, changes })
449452
ast.changes = changes
450453
}),
451-
astro: createParser(astro.parsers.astro, transformAstro)
454+
...astro ? { astro: createParser(astro.parsers.astro, transformAstro) } : {},
452455
}
453456

454457
function transformAstro(ast, { env, changes }) {
@@ -548,3 +551,14 @@ function visit(ast, callbackMap) {
548551
}
549552
_visit(ast)
550553
}
554+
555+
// For loading prettier plugins only if they exist
556+
function loadIfExists(name) {
557+
try {
558+
if (require.resolve(name)) {
559+
return require(name)
560+
}
561+
} catch (e) {
562+
return null
563+
}
564+
}

0 commit comments

Comments
 (0)