library-ts/node/webpack/TemplatesIndexBuilder.ts

136 lines
3.4 KiB
TypeScript
Raw Permalink Normal View History

2025-03-31 12:00:55 +00:00
import * as fs from "fs";
import * as path from "path";
import { RJLog } from "../log/RJLog";
import { Files } from "../files/Files";
import { PathReference } from "../files/PathReference";
export class TemplatesInfo
{
templates:string[] = [];
}
export class TemplatesIndexBuilder
{
2025-09-06 11:33:04 +00:00
inputDirectories:string[];
2025-03-31 12:00:55 +00:00
outputDir:string;
2025-09-06 11:33:04 +00:00
constructor( source:string[], build:string )
2025-03-31 12:00:55 +00:00
{
2025-09-06 11:33:04 +00:00
this.inputDirectories = source;
2025-03-31 12:00:55 +00:00
this.outputDir = build;
}
filterFiles( fileName:string )
{
if ( fileName.startsWith( "__" ) )
{
return false;
}
return fileName.endsWith( ".html" );
}
apply( compiler:any )
{
compiler.hooks.afterCompile.tapAsync( "TemplatesIndexBuilder",
async ( compilation:any, callback:any ) =>
{
2025-09-06 11:33:04 +00:00
for ( let inputDirectory of this.inputDirectories )
{
await Files.forAllIn( inputDirectory, null,
async ( p:PathReference ) =>
2025-03-31 12:00:55 +00:00
{
2025-09-06 11:33:04 +00:00
if ( await p.isDirectory() )
{
return Promise.resolve();
}
2025-03-31 12:00:55 +00:00
2025-09-06 11:33:04 +00:00
let fileName = p.fileName;
2025-03-31 12:00:55 +00:00
2025-09-06 11:33:04 +00:00
if ( ! this.filterFiles( fileName ) )
{
return Promise.resolve();
}
2025-03-31 12:00:55 +00:00
2025-09-06 11:33:04 +00:00
compilation.fileDependencies.add( p.absolutePath );
2025-03-31 12:00:55 +00:00
2025-09-06 11:33:04 +00:00
return Promise.resolve();
}
);
}
2025-03-31 12:00:55 +00:00
callback();
}
);
compiler.hooks.emit.tapAsync( "TemplatesIndexBuilder",
async ( compilation:any, callback:any ) =>
{
2025-09-06 11:33:04 +00:00
let templates = new TemplatesInfo();
for ( let inputDirectory of this.inputDirectories )
{
let templatesPathReference = new PathReference( path.resolve( inputDirectory ) );
2025-03-31 12:00:55 +00:00
2025-09-06 11:33:04 +00:00
// RJLog.log( templatesPathReference.absolutePath );
2025-03-31 12:00:55 +00:00
2025-09-06 11:33:04 +00:00
await Files.forAllIn( templatesPathReference.absolutePath, null,
async ( p:PathReference ) =>
2025-03-31 12:00:55 +00:00
{
2025-09-06 11:33:04 +00:00
if ( await p.isDirectory() )
{
return Promise.resolve();
}
2025-03-31 12:00:55 +00:00
2025-09-06 11:33:04 +00:00
let fileName = p.fileName;
2025-03-31 12:00:55 +00:00
2025-09-06 11:33:04 +00:00
if ( ! this.filterFiles( fileName ) )
{
return Promise.resolve();
}
2025-03-31 12:00:55 +00:00
2025-09-06 11:33:04 +00:00
let absoluteFilePath = p.absolutePath;
let inputRelativePath = templatesPathReference.createRelativeFromAbsolute( absoluteFilePath, true );
2025-03-31 12:00:55 +00:00
2025-09-06 11:33:04 +00:00
// RJLog.log( "Abs to Rel", absoluteFilePath, inputRelativePath.relativePath );
let outputFileName = path.join( this.outputDir, inputRelativePath.relativePath );
2025-03-31 12:00:55 +00:00
2025-09-06 11:33:04 +00:00
templates.templates.push( inputRelativePath.relativePath );
2025-03-31 12:00:55 +00:00
2025-09-06 11:33:04 +00:00
let content = fs.readFileSync( p.absolutePath, "utf-8" );
2025-03-31 12:00:55 +00:00
2025-09-06 11:33:04 +00:00
// RJLog.log( "Adding", p.absolutePath, outputFileName );
compilation.assets[ outputFileName ] =
{
source: () => content,
size: () => content.length,
};
return Promise.resolve();
}
);
}
2025-03-31 12:00:55 +00:00
let templatesPath = path.join( this.outputDir, "index.json" );
let templatesJSON = JSON.stringify( templates, null, " " );
compilation.assets[ templatesPath ] =
{
source: () => templatesJSON,
size: () => templatesJSON.length,
};
callback();
return Promise.resolve();
}
);
}
}