I am trying to build with webpack and the HtmlWebpackPlugin. When I do so and open my newly generated index.html, it seems that all changes done inside the index.js to the template index.html are not being reflected. For example when clicking on buttons that have been assigned to an onclick event, I get errors like: Uncaught ReferenceError: selectAccount is not defined. Also, buttons that should simply modify the display property of divs don’t react at all.
webpack.config.js:
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const config = {
entry: { script: "./src/index.js" },
output: {
path: __dirname + "/dist",
filename: "index_bundle.js",
},
module: {
rules: [
{
test: /.js$/,
exclude: /node_modules/,
use: ["babel-loader"],
},
{
test: /.css$/,
use: [MiniCssExtractPlugin.loader, "css-loader"],
},
{
test: /.html$/,
use: "html-loader",
},
{
test: /.txt|docx$/,
use: "raw-loader",
},
],
},
resolve: {
extensions: ["*", ".js"],
},
devServer: {
static: "./dist",
port: 8000,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, PATCH, OPTIONS",
"Access-Control-Allow-Headers":
"X-Requested-With, content-type, Authorization",
},
},
plugins: [
new MiniCssExtractPlugin(),
new HtmlWebpackPlugin({
title: "Project Title",
template: "index.html",
inject: true,
// excludeChunks: ["script"],
}),
],
};
module.exports = (env, argv) => {
process.env.NODE_ENV = argv.mode;
if (argv.mode === "development") {
//
}
if (argv.mode === "production") {
//
}
return config;
};
package.json:
{
"name": "abc",
"version": "1.0.0",
"description": "abc",
"main": "index.js",
"scripts": {
"start": "webpack serve --config ./webpack.config.js --mode development",
"dev": "webpack serve --config ./webpack.config.js --mode development",
"build": "webpack --config ./webpack.config.js --mode production",
},
"repository": {},
"keywords": [
"javascript",
"code",
"node.js"
],
"author": "",
"license": "MIT",
"homepage": "",
"dependencies": {
"angular-expressions": "^1.1.5",
"docxtemplater": "^3.29.0",
"express": "^4.17.1",
"file-saver": "^2.0.5",
"file-system": "^2.2.2",
"install": "^0.13.0",
"lodash": "^4.17.21",
"morgan": "^1.10.0",
"npm": "^8.5.0",
"pizzip": "^3.1.1",
"survey-creator": "^1.9.13",
"survey-jquery": "^1.9.7",
"vis-data": "^7.1.2",
"vis-network": "^9.1.0"
},
"devDependencies": {
"@babel/core": "^7.17.8",
"babel-loader": "^8.2.4",
"css-loader": "^6.7.1",
"html-inline-css-webpack-plugin": "^1.11.1",
"html-inline-script-webpack-plugin": "^3.0.0",
"html-loader": "^3.1.0",
"html-webpack-inline-source-plugin": "^0.0.10",
"html-webpack-plugin": "^5.5.0",
"mini-css-extract-plugin": "^2.6.0",
"nodemon": "^2.0.3",
"path": "^0.12.7",
"raw-loader": "^4.0.2",
"replace-in-file-webpack-plugin": "^1.0.6",
"webpack": "^5.70.0",
"webpack-cli": "^4.9.2",
"webpack-dev-server": "^4.7.4"
}
}
project structure:
dist-folder
- index_bundle.js
- index.html
- ...
node_modules-folder
src-folder
- index.js
- ...
index.html (template)
package.json
style.css
webpack.config.js