Files
tsbundle/ts/mod_html/index.ts

65 lines
1.7 KiB
TypeScript

import * as plugins from './plugins.js';
import * as paths from '../paths.js';
export class HtmlHandler {
public defaultFromPath: string = plugins.path.join(
paths.htmlDir,
'index.html',
);
public defaultToPath: string = plugins.path.join(
paths.distServeDir,
'index.html',
);
public async checkIfExists() {
return await plugins.fs.file(this.defaultFromPath).exists();
}
// copies the html
public async processHtml(optionsArg: {
from?: string;
to?: string;
minify?: boolean;
}) {
optionsArg = {
...{
from: this.defaultFromPath,
to: this.defaultToPath,
minify: false,
},
...optionsArg,
};
if (await this.checkIfExists()) {
console.log(`${optionsArg.from} replaces file at ${optionsArg.to}`);
}
optionsArg.from = plugins.smartpath.transform.toAbsolute(
optionsArg.from,
paths.cwd,
) as string;
optionsArg.to = plugins.smartpath.transform.toAbsolute(
optionsArg.to,
paths.cwd,
) as string;
let fileString = (await plugins.fs
.file(optionsArg.from)
.encoding('utf8')
.read()) as string;
if (optionsArg.minify) {
fileString = plugins.htmlMinifier.minify(fileString, {
minifyCSS: true,
minifyJS: true,
sortAttributes: true,
sortClassName: true,
removeAttributeQuotes: true,
collapseWhitespace: true,
collapseInlineTagWhitespace: true,
removeComments: true,
});
}
const toDir = plugins.path.dirname(optionsArg.to);
await plugins.fs.directory(toDir).create();
await plugins.fs.file(optionsArg.to).encoding('utf8').write(fileString);
console.log(`html processing succeeded!`);
}
}