tsbundle/ts/tsbundle.htmlhandler.ts

39 lines
1.2 KiB
TypeScript
Raw Normal View History

2019-06-16 15:47:34 +00:00
import * as plugins from './tsbundle.plugins';
import * as paths from './tsbundle.paths';
export class HtmlHandler {
public sourceFilePath: string = plugins.path.join(paths.htmlDir, 'index.html');
public targetFilePath: string = plugins.path.join(paths.distWebDir, 'index.html');
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,
2019-07-17 10:22:24 +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
}