0

Webpack Example Plugin to Write Files

Function JavaScript 1 revisions 129 7 months ago 7 months ago
function() {
			this.plugin("done", stats => {
				stats = stats.toJson();
				Object.keys(stats.entrypoints).forEach(entry => {
					const siteFolder = path.resolve(stats.outputPath, entry, "..");
					const cssFile = path.basename(stats.entrypoints[entry].assets.find(asset => path.extname(asset) === ".css"));
					const jsFile = path.basename(stats.entrypoints[entry].assets.find(asset => path.extname(asset) === ".js"));

					fs.writeFileSync(path.resolve(siteFolder, "public", "app.html"),
						fs.readFileSync(path.resolve(siteFolder, "index.html"), "utf8")
							.replace("{{css}}", `/${cssFile}`)
							.replace("{{js}}", `/${jsFile}`)
					);
				});
			});
		}

i am only posting this to meme on overcoder but it does have good use case, put it in your plugins part of your webpack config and you can make your html file generate with proper file names if you were to use content hashes/other stuff like that