63 lines
1.7 KiB
TypeScript
63 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;
|
|
}) {
|
|
const options = {
|
|
from: this.defaultFromPath,
|
|
to: this.defaultToPath,
|
|
minify: false,
|
|
...optionsArg,
|
|
};
|
|
if (await this.checkIfExists()) {
|
|
console.log(`${options.from} replaces file at ${options.to}`);
|
|
}
|
|
options.from = plugins.smartpath.transform.toAbsolute(
|
|
options.from,
|
|
paths.cwd,
|
|
) as string;
|
|
options.to = plugins.smartpath.transform.toAbsolute(
|
|
options.to,
|
|
paths.cwd,
|
|
) as string;
|
|
let fileString = (await plugins.fs
|
|
.file(options.from)
|
|
.encoding('utf8')
|
|
.read()) as string;
|
|
if (options.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(options.to);
|
|
await plugins.fs.directory(toDir).create();
|
|
await plugins.fs.file(options.to).encoding('utf8').write(fileString);
|
|
console.log(`html processing succeeded!`);
|
|
}
|
|
}
|