I'm trying desperately to split my HTML into multiple files using Webpack. I've tried the "<%= require() %>" method but doesn't work. Only work with my images. See my webpack config below. Basically what I wanna do is this:
file1.html
<h1>This is my file number 1</h1>
file 2.html
<h1>This is my file number 2</h1>
<%= require("file1.html") %>
And then webpack when rendering the index.html file, will bundle those two files together. This is my webpack.config.js:
const path = require("path")
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const webpack = require("webpack");
module.exports = {
entry: "./src/assets/js/index.js",
mode: "development",
output: {
filename: '[name].[contenthash].js',
path: path.resolve(__dirname, "dist"),
clean: {
keep: /images\//, // Keep these assets under 'ignored/dir'.
},
},
devtool: 'inline-source-map',
module: {
rules: [
{
test: /\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: "./"
}
},
{
loader: 'css-loader',
options: {importLoaders: 2, url: false},
},
{
loader: 'postcss-loader',
options: {
postcssOptions: {
config: path.resolve(__dirname, 'postcss.config.js'),
},
},
},
],
},
{
test: /\.(png|jpe?g|gif|svg)$/i,
use: [
{
loader: 'file-loader',
options: {
filename: "[name].[hash:6].[ext]",
outputPath: 'images/',
emitFile: true,
esModule: false
},
},
],
},
],
},
plugins: [
new HtmlWebpackPlugin({
title: 'Caching',
template: "./src/template.html"
}),
new MiniCssExtractPlugin({
filename: "[name].[contenthash].css",
}),
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
}),
],
optimization: {
splitChunks: {
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
},
},
},
},
}
Any help is more than welcome. Thanks in advance!