11import type {
22 AstNode ,
3+ LoadResult ,
34 ParseInfo ,
45 ParseResult ,
56 ParserOptions ,
@@ -13,7 +14,7 @@ import {doParse, doRender, tokenize, tokenizeStream} from "./lib/index.ts";
1314import { dirname , matchUrl , resolve } from "./lib/fs/index.ts" ;
1415import { Readable } from "node:stream" ;
1516import { createReadStream } from "node:fs" ;
16- import { lstat } from "node:fs/promises" ;
17+ import { lstat , readFile } from "node:fs/promises" ;
1718
1819export type * from "./@types/index.d.ts" ;
1920export type * from "./@types/ast.d.ts" ;
@@ -57,38 +58,45 @@ export {dirname, resolve};
5758 * load file or url as stream
5859 * @param url
5960 * @param currentFile
61+ * @param asStream
6062 * @throws Error file not found
6163 *
6264 * @private
6365 */
64- export async function load ( url : string , currentFile : string = '.' ) : Promise < ReadableStream < Uint8Array > | string > {
66+ export async function load ( url : string , currentFile : string = '.' , asStream : boolean = false ) : Promise < string | ReadableStream < Uint8Array < ArrayBufferLike > > > {
6567
6668 const resolved = resolve ( url , currentFile ) ;
6769
68- // @ts -ignore
6970 if ( matchUrl . test ( resolved . absolute ) ) {
7071
71- return fetch ( resolved . absolute ) . then ( ( response : Response ) => {
72+ return fetch ( resolved . absolute ) . then ( async ( response : Response ) : Promise < string | ReadableStream < Uint8Array < ArrayBufferLike > > > => {
7273
7374 if ( ! response . ok ) {
7475
7576 throw new Error ( `${ response . status } ${ response . statusText } ${ response . url } ` )
7677 }
7778
78- return response . body as ReadableStream < Uint8Array < ArrayBuffer > > ;
79- } )
79+ return asStream ? response . body as ReadableStream < Uint8Array < ArrayBuffer > > : await response . text ( ) ;
80+ } ) ;
8081 }
8182
8283 try {
8384
85+ if ( ! asStream ) {
86+
87+ return readFile ( resolved . absolute , 'utf-8' ) ;
88+ }
89+
8490 const stats = await lstat ( resolved . absolute ) ;
8591
8692 if ( stats . isFile ( ) ) {
8793
88- return Readable . toWeb ( createReadStream ( resolved . absolute ) ) as ReadableStream < Uint8Array > ;
94+ return Readable . toWeb ( createReadStream ( resolved . absolute , { encoding : 'utf-8' , highWaterMark : 64 * 1024 } ) ) as ReadableStream < Uint8Array > ;
8995 }
96+
9097 } catch ( error ) {
9198
99+ console . warn ( error ) ;
92100 }
93101
94102 throw new Error ( `File not found: '${ resolved . absolute || url } '` ) ;
@@ -131,6 +139,7 @@ export function render(data: AstNode, options: RenderOptions = {}): RenderResult
131139 * parse css file
132140 * @param file url or path
133141 * @param options
142+ * @param asStream load file as stream
134143 *
135144 * @throws Error file not found
136145 *
@@ -149,9 +158,9 @@ export function render(data: AstNode, options: RenderOptions = {}): RenderResult
149158 * console.log(result.ast);
150159 * ```
151160 */
152- export async function parseFile ( file : string , options : ParserOptions = { } ) : Promise < ParseResult > {
161+ export async function parseFile ( file : string , options : ParserOptions = { } , asStream : boolean = false ) : Promise < ParseResult > {
153162
154- return load ( file ) . then ( stream => parse ( stream , { src : file , ...options } ) ) ;
163+ return Promise . resolve ( ( ( options . load ?? load ) as ( file : string , currentFile : string , asStream : boolean ) => LoadResult ) ( file , '.' , asStream ) ) . then ( stream => parse ( stream , { src : file , ...options } ) ) ;
155164}
156165
157166/**
@@ -211,6 +220,7 @@ export async function parse(stream: string | ReadableStream<Uint8Array>, options
211220 * transform css file
212221 * @param file url or path
213222 * @param options
223+ * @param asStream load file as stream
214224 *
215225 * @throws Error file not found
216226 *
@@ -229,9 +239,9 @@ export async function parse(stream: string | ReadableStream<Uint8Array>, options
229239 * console.log(result.code);
230240 * ```
231241 */
232- export async function transformFile ( file : string , options : TransformOptions = { } ) : Promise < TransformResult > {
242+ export async function transformFile ( file : string , options : TransformOptions = { } , asStream : boolean = false ) : Promise < TransformResult > {
233243
234- return load ( file ) . then ( stream => transform ( stream , { src : file , ...options } ) ) ;
244+ return Promise . resolve ( ( ( options . load ?? load ) as ( file : string , currentFile : string , asStream : boolean ) => LoadResult ) ( file , '.' , asStream ) ) . then ( stream => transform ( stream , { src : file , ...options } ) ) ;
235245}
236246
237247/**
0 commit comments