|
@@ -13,6 +13,7 @@ export default async (dir)=>{
|
|
|
}else{
|
|
}else{
|
|
|
data = await parseHtml(indexFile);
|
|
data = await parseHtml(indexFile);
|
|
|
}
|
|
}
|
|
|
|
|
+ const bundle = await createBundle(data);
|
|
|
fs.rm(path.join(dir, "tmp/"), {recursive: true, force: true});
|
|
fs.rm(path.join(dir, "tmp/"), {recursive: true, force: true});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -65,3 +66,47 @@ const parseHtml = async (index)=>{
|
|
|
js: path.join(parentPath, `${basename}.js`)
|
|
js: path.join(parentPath, `${basename}.js`)
|
|
|
};
|
|
};
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+const createBundle = async (data)=>{
|
|
|
|
|
+ const entryPoints = [];
|
|
|
|
|
+ if(data.css) entryPoints.push(data.css);
|
|
|
|
|
+ if(data.js) entryPoints.push(data.js);
|
|
|
|
|
+
|
|
|
|
|
+ const esbuildProm = esbuild.build({
|
|
|
|
|
+ entryPoints: entryPoints,
|
|
|
|
|
+ bundle: true,
|
|
|
|
|
+ minify: true,
|
|
|
|
|
+ write: false,
|
|
|
|
|
+ outdir: "/"
|
|
|
|
|
+ });
|
|
|
|
|
+ const htmlProm = htmlMinifier.minify(data.html, {
|
|
|
|
|
+ collapseBooleanAttributes: true,
|
|
|
|
|
+ collapseInlineTagWhitespace: true,
|
|
|
|
|
+ collapseWhitespace: true,
|
|
|
|
|
+ decodeEntities: true,
|
|
|
|
|
+ html5: true,
|
|
|
|
|
+ includeAutoGeneratedTags: false,
|
|
|
|
|
+ noNewlinesBeforeTagClose: true,
|
|
|
|
|
+ removeComments: true,
|
|
|
|
|
+ useShortDoctype: true
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ const [buildData, html] = await Promise.all([esbuildProm, htmlProm]);
|
|
|
|
|
+
|
|
|
|
|
+ //potentially replaceable
|
|
|
|
|
+ const comps = {html: html};
|
|
|
|
|
+ for(let i = 0; i < buildData.outputFiles.length; i++){
|
|
|
|
|
+ const ext = path.extname(buildData.outputFiles[i].path).replace(".", "");
|
|
|
|
|
+ comps[ext] = buildData.outputFiles[i].text;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return mergeFiles(comps);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+const mergeFiles = (comps)=>{
|
|
|
|
|
+ const cssIndex = comps.html.indexOf("</head>");
|
|
|
|
|
+ const html = `${comps.html.slice(0, cssIndex)}${comps.css}${comps.html.slice(cssIndex)}`;
|
|
|
|
|
+
|
|
|
|
|
+ const jsIndex = html.indexOf("</body>");
|
|
|
|
|
+ return `${comps.html.slice(0, jsIndex)}${comps.js}${html.slice(jsIndex)}`;
|
|
|
|
|
+}
|