I’m quite new to Webpack and Webdev in general, so I’m confused about how would I redirect to a new page or “HTML” file when using Webpack and JS.
Right now my directory has a dist folder which includes index.html and app.html, as well as their respective bundles which I defined on the webpack config file like this:
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
mode: "development",
entry: {
home: "./src/index.js",
app: "./src/pages/app.js",
},
output: {
path: path.resolve(__dirname, "dist"),
filename: "[name].bundle.js",
clean: true,
},
devServer: {
static: {
directory: path.resolve(__dirname, "dist"),
},
port: 3000,
open: true,
hot: true,
compress: true,
historyApiFallback: true,
},
module: {
rules: [
{
test: /.(png|jpe?g|gif)$/i,
use: [
{
loader: "file-loader",
},
],
},
{
test: /.css$/i,
include: path.resolve(__dirname, "src"),
use: ["style-loader", "css-loader", "postcss-loader"],
},
{
test: /.svg$/,
use: [
{
loader: "svg-url-loader",
options: {
limit: 10000,
},
},
],
},
{
test: /.html$/i,
loader: "html-loader",
},
],
},
plugins: [
new HtmlWebpackPlugin({
filename: "index.html",
template: "src/views/index.html",
chunks: ["home"],
}),
new HtmlWebpackPlugin({
filename: "app.html",
template: "src/views/app.html",
chunks: ["app"],
}),
],
};
On my home.js script (which handles the homepage injection) I created an a element and configured the path for the app.html file, so when I click the a element I get redirected to the new app.html file.
import loginPage from "../views/app.html";
...
const startButton = document.createElement("a");
startButton.id = "start-button";
startButton.innerText = "Get Started";
startButton.href = loginPage;
However, that doesn’t work at all. Instead, when hovering my mouse on top of the a tag I just get the Raw content of app.html, that is, the code that is inside that specific file, so I’m not sure how to accomplish what I’m looking for and I’ve seen some tutorials online but I don’t understand how people redirect users to a new page with webpack and vanilla JS.
Here’s my file structure as well:
ª .gitignore
ª package-lock.json
ª package.json
ª postcss.config.js
ª README.md
ª tailwind.config.js
ª webpack.config.js
ª
+---dist
ª app.bundle.js
ª app.html
ª f0389bbd28ce1918b0936a7570a77ae1.png
ª home.bundle.js
ª index.html
ª
ª
+---src
ª index.js
ª styles.css
ª
+---assets
ª +---img
ª homepageDark.png
ª moon.svg
ª sun.svg
ª
+---elements
ª bgCircles.js
ª
+---functions
ª darkMode.js
ª erasePage.js
ª svgParser.js
ª
+---pages
ª app.js
ª home.js
ª navbar.js
ª
+---views
app.html
index.html