tsbundle/ts/mod_rollup/htmlhandler.ts

39 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-03-15 23:21:05 +00:00
import * as plugins from './plugins.js';
import * as paths from '../paths.js';
2019-06-16 15:47:34 +00:00
export class HtmlHandler {
public sourceFilePath: string = plugins.path.join(paths.htmlDir, 'index.html');
2020-03-14 22:40:09 +00:00
public targetFilePath: string = plugins.path.join(paths.distServeDir, 'index.html');
2019-06-16 15:47:34 +00:00
2019-07-17 10:22:24 +00:00
public async checkIfExists() {
2019-07-17 09:54:49 +00:00
return plugins.smartfile.fs.fileExists(this.sourceFilePath);
}
2019-06-16 15:47:34 +00:00
// copies the html
2020-03-09 14:36:55 +00:00
public async copyHtml(targetPathArg = this.targetFilePath) {
2019-07-17 10:40:40 +00:00
if (!(await this.checkIfExists())) {
2019-07-17 09:54:49 +00:00
return;
}
2020-03-09 14:36:55 +00:00
await plugins.smartfile.fs.copy(this.sourceFilePath, targetPathArg);
2019-06-16 15:47:34 +00:00
}
// copies and minifies the html
2020-03-09 14:36:55 +00:00
public async minifyHtml(targetPathArg = this.targetFilePath) {
2019-07-17 10:40:40 +00:00
if (!(await this.checkIfExists())) {
2019-07-17 09:54:49 +00:00
return;
}
2019-06-16 15:47:34 +00:00
const fileString = plugins.smartfile.fs.toStringSync(this.sourceFilePath);
const minifiedHtml = plugins.htmlMinifier.minify(fileString, {
minifyCSS: true,
minifyJS: true,
sortAttributes: true,
sortClassName: true,
2019-06-16 15:52:20 +00:00
removeAttributeQuotes: true,
collapseWhitespace: true,
collapseInlineTagWhitespace: true,
2022-03-14 15:32:12 +00:00
removeComments: true,
2019-06-16 15:47:34 +00:00
});
2020-03-09 14:36:55 +00:00
plugins.smartfile.memory.toFsSync(minifiedHtml, targetPathArg);
2019-06-16 15:47:34 +00:00
}
2019-07-17 10:22:24 +00:00
}