@@ -6,6 +6,7 @@ import * as v from '@vue/compiler-sfc';
66import color from 'chalk' ;
77import { type Node , walk } from 'estree-walker' ;
88import { type TsConfigResult , createPathsMatcher , getTsconfig } from 'get-tsconfig' ;
9+ import * as parse5 from 'parse5' ;
910import path from 'pathe' ;
1011import * as prettier from 'prettier' ;
1112import * as sv from 'svelte/compiler' ;
@@ -77,6 +78,87 @@ const css: Lang = {
7778 } ,
7879} ;
7980
81+ /** Language support for `*.html` files. */
82+ const html : Lang = {
83+ matches : ( fileName ) => fileName . endsWith ( '.html' ) ,
84+ resolveDependencies : ( { filePath, isSubDir, excludeDeps, dirs, cwd } ) => {
85+ const sourceCode = fs . readFileSync ( filePath ) . toString ( ) ;
86+
87+ const ast = parse5 . parse ( sourceCode ) ;
88+
89+ const imports : string [ ] = [ ] ;
90+
91+ // @ts -ignore yeah I know
92+ const walk = ( node , enter : ( node ) => void ) => {
93+ if ( ! node ) return ;
94+
95+ enter ( node ) ;
96+
97+ if ( node . childNodes && node . childNodes . length > 0 ) {
98+ for ( const n of node . childNodes ) {
99+ walk ( n , enter ) ;
100+ }
101+ }
102+ } ;
103+
104+ for ( const node of ast . childNodes ) {
105+ walk ( node , ( n ) => {
106+ if ( n . tagName === 'script' ) {
107+ for ( const attr of n . attrs ) {
108+ if ( attr . name === 'src' ) {
109+ imports . push ( attr . value ) ;
110+ }
111+ }
112+ }
113+
114+ if (
115+ n . tagName === 'link' &&
116+ // @ts -ignore yeah I know
117+ n . attrs . find ( ( attr ) => attr . name === 'rel' && attr . value === 'stylesheet' )
118+ ) {
119+ for ( const attr of n . attrs ) {
120+ if ( attr . name === 'href' && ! attr . value . startsWith ( 'http' ) ) {
121+ imports . push ( attr . value ) ;
122+ }
123+ }
124+ }
125+ } ) ;
126+ }
127+
128+ const resolveResult = resolveImports ( {
129+ moduleSpecifiers : imports ,
130+ filePath,
131+ isSubDir,
132+ dirs,
133+ cwd,
134+ doNotInstall : [ 'svelte' , '@sveltejs/kit' , ...excludeDeps ] ,
135+ } ) ;
136+
137+ if ( resolveResult . isErr ( ) ) {
138+ return Err (
139+ resolveResult
140+ . unwrapErr ( )
141+ . map ( ( err ) => formatError ( err ) )
142+ . join ( '\n' )
143+ ) ;
144+ }
145+
146+ return Ok ( resolveResult . unwrap ( ) ) ;
147+ } ,
148+ comment : ( content ) => `<!--\n${ lines . join ( lines . get ( content ) , { prefix : ( ) => '\t' } ) } \n-->` ,
149+ format : async ( code , { formatter, prettierOptions } ) => {
150+ if ( ! formatter ) return code ;
151+
152+ if ( formatter === 'prettier' ) {
153+ return await prettier . format ( code , { parser : 'html' , ...prettierOptions } ) ;
154+ }
155+
156+ // biome is in progress for formatting html
157+
158+ return code ;
159+ } ,
160+ } ;
161+
80162/** Language support for `*.(json)` files. */
81163const json : Lang = {
82164 matches : ( fileName ) => fileName . endsWith ( '.json' ) ,
@@ -131,6 +213,23 @@ const jsonc: Lang = {
131213 } ,
132214} ;
133215
216+ /** Language support for `*.(sass|scss)` files. */
217+ const sass : Lang = {
218+ matches : ( fileName ) => fileName . endsWith ( '.sass' ) || fileName . endsWith ( '.scss' ) ,
219+ resolveDependencies : ( ) =>
220+ Ok ( { dependencies : [ ] , local : [ ] , devDependencies : [ ] , imports : { } } ) ,
221+ comment : ( content ) => `/*\n${ lines . join ( lines . get ( content ) , { prefix : ( ) => '\t' } ) } \n*/` ,
222+ format : async ( code , { formatter, prettierOptions } ) => {
223+ if ( ! formatter ) return code ;
224+
225+ if ( formatter === 'prettier' ) {
226+ return await prettier . format ( code , { parser : 'scss' , ...prettierOptions } ) ;
227+ }
228+
229+ return code ;
230+ } ,
231+ } ;
232+
134233/** Language support for `*.svelte` files. */
135234const svelte : Lang = {
136235 matches : ( fileName ) => fileName . endsWith ( '.svelte' ) ,
@@ -719,6 +818,6 @@ const resolveRemoteDeps = (
719818 } ;
720819} ;
721820
722- const languages : Lang [ ] = [ css , json , jsonc , svelte , svg , typescript , vue , yaml ] ;
821+ const languages : Lang [ ] = [ css , html , json , jsonc , sass , svelte , svg , typescript , vue , yaml ] ;
723822
724- export { css , json , jsonc , svelte , svg , typescript , vue , yaml , languages } ;
823+ export { css , html , json , jsonc , sass , svelte , svg , typescript , vue , yaml , languages } ;
0 commit comments