11import type { FileInfo , FileSystemAdapter , SingleFileConfig } from './types'
2+ import * as path from 'node:path'
23import { minify } from 'html-minifier-terser'
34import { CssInliner } from './css-inliner'
45import { CssTransformer } from './css-transformer'
@@ -9,7 +10,7 @@ export class SingleFileBuilder {
910 constructor ( private fs : FileSystemAdapter ) { }
1011
1112 async build ( buildDir : string , config : SingleFileConfig ) : Promise < void > {
12- const folder = buildDir . endsWith ( '/' ) ? buildDir : ` ${ buildDir } /`
13+ const folder = buildDir
1314
1415 // Find all files recursively
1516 const files = this . findAllFiles ( folder )
@@ -55,7 +56,7 @@ export class SingleFileBuilder {
5556
5657 // Delete empty folders
5758 this . fs . readDir ( folder ) . forEach ( ( f ) => {
58- const file = ` ${ folder } ${ f } `
59+ const file = path . join ( folder , f )
5960 try {
6061 const stats = this . fs . stat ( file )
6162 if ( stats . isDirectory ( ) && this . fs . readDir ( file ) . length === 0 ) {
@@ -70,14 +71,14 @@ export class SingleFileBuilder {
7071
7172 private findAllFiles ( folder : string ) : string [ ] {
7273 return this . fs . readDir ( folder ) . reduce < string [ ] > ( ( acc , f ) => {
73- const file = ` ${ folder } ${ f } `
74+ const file = path . join ( folder , f )
7475 const stats = this . fs . stat ( file )
7576
7677 if ( stats . isFile ( ) ) {
7778 acc . push ( file )
7879 }
7980 else if ( stats . isDirectory ( ) ) {
80- acc = acc . concat ( this . findAllFiles ( ` ${ file } /` ) )
81+ acc = acc . concat ( this . findAllFiles ( file ) )
8182 }
8283
8384 return acc
@@ -87,10 +88,10 @@ export class SingleFileBuilder {
8788 private openFiles ( files : string [ ] , fileType : 'html' | 'css' ) : FileInfo [ ] {
8889 return files
8990 . filter ( i => i . endsWith ( `.${ fileType } ` ) )
90- . map ( path => ( {
91- contents : this . fs . readFile ( path , 'utf8' ) ,
92- path,
93- fileName : path . split ( '/' ) . pop ( ) ! ,
91+ . map ( filePath => ( {
92+ contents : this . fs . readFile ( filePath , 'utf8' ) ,
93+ path : filePath ,
94+ fileName : path . basename ( filePath ) ,
9495 } ) )
9596 }
9697}
0 commit comments