html-bundler-webpack-plugin
Version:
Generates complete single-page or multi-page website from source assets. Built-in support for Markdown, Eta, EJS, Handlebars, Nunjucks, Pug. Alternative to html-webpack-plugin.
67 lines (55 loc) • 1.7 kB
JavaScript
const PreprocessorModeAbstract = require('./PreprocessorModeAbstract');
const { decodeReservedChars, escapeSequences } = require('../Utils');
const { isWin, pathToPosix } = require('../../Common/Helpers');
const { errorToHtml } = require('../Messages/Exeptions');
/**
* Compile into JS function and export as a JS module.
*/
class Compile extends PreprocessorModeAbstract {
enclosingQuotes = `'`;
isExport = false;
/**
* @param {PreprocessorModeProperties} props
*/
constructor(props) {
super(props);
this.isExport = typeof this.preprocessor.export === 'function';
}
/**
* @param {string} enclosingQuotes
*/
setEnclosingQuotes(enclosingQuotes) {
this.enclosingQuotes = enclosingQuotes;
}
/**
* @param {string} file
* @return {string}
*/
requireExpression(file) {
const quote = this.enclosingQuotes;
if (isWin) file = pathToPosix(file);
return `${quote} + require('${file}') + ${quote}`;
}
/**
* Export a template function depending on the code generated by the preprocessor.
*
* @param {string} content The source of template function.
* @param {BundlerPluginLoaderContext} loaderContext
* @return {string}
*/
export(content, loaderContext) {
return this.isExport ? this.preprocessor.export(content, loaderContext) : content;
}
/**
* Export code with error message.
*
* @param {string|Error} error The error.
* @param {string} issuer The issuer where the error occurred.
* @return {string}
*/
exportError(error, issuer) {
const content = errorToHtml(error);
return this.exportCode + "'" + decodeReservedChars(content) + "';";
}
}
module.exports = Compile;