Note: I'm a beginner to webpack.
I'm trying to get webpack to load my .htmls files (index.html and login.html) as they will serve as windows for my electron application. This is what I have tried so far with no results:
rules: [
{
test: /\.html$/,
use: ["html-loader"]
},
...
and
rules: [
{
test: /\.html$/,
loader: "file-loader"
},
...
This is my webpack.config.js file:
const path = require("path");
const { spawn } = require("child_process");
const srcDir = path.resolve(__dirname, "src/renderer");
const outDir = path.resolve(__dirname, "build/client");
const defaultIncludes = [srcDir];
module.exports = {
entry: `${srcDir}/index`,
output: {
path: outDir,
filename: "app.bundle.js"
},
resolve: {
extensions: [".ts", ".tsx", ".js", ".json", ".html"]
},
module: {
rules: [
{
test: /\.html$/,
loader: "file-loader"
},
{
// Include ts, tsx, and js files.
test: /\.(tsx?)|(js)$/,
exclude: /node_modules/,
loader: "babel-loader",
include: defaultIncludes
},
{
test: /\.scss$/,
use: [
"style-loader", // creates style nodes from JS strings
"css-loader", // translates CSS into CommonJS
"sass-loader" // compiles Sass to CSS
]
},
{
test: /\.(ttf|eot|woff|woff2)$/,
loader: "file-loader",
options: {
name: "fonts/[name].[ext]"
}
}
]
},
devServer: {
inline: true,
contentBase: outDir,
compress: true,
stats: {
colors: true,
chunks: false,
children: false
},
before() {
spawn("electron", ["."], {
shell: true,
env: process.env,
stdio: "inherit"
}).on("close", code => process.exit(0)).on("error", spawnError => console.error(spawnError));
}
},
target: "electron-renderer",
mode: "development"
};
What am I doing wrong? Webpack builds everything into the /build
yet the .html files (index.html and login.html, which are under my /src/
directory) are not included.
The loader configuration for HTML files will allow require
calls with HTML files to work inside javascript files. With the file loader you'll get a file path and with the HTML loader you'll get the HTML content as a result of the call.
If you want your HTML files to be copied alongside with your compiled sources you'll have to use a plugin like copy-webpack-plugin or html-webpack-plugin.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With