I'm trying to get an extremely simple bare-bones TypeScript project setup with Webpack and I'm having an incredibly hard time for some reason.
I followed this guide and everything is working. When I simply run webpack
my files will bundle and compile and be outputted into the dist
directory. From there I can host them on a local server and it all works fine.
However, I want to avoid having to manually bundle and compile my code every single time I make a change, and I'd like for this to be done automatically each time I save my files. As far as I can tell this is a common use for Webpack and so I'm not sure why I'm having such an incredibly hard time finding any good information on it.
I decided to use the webpack dev server to accomplish this, so I read this tutorial and for the life of me I can't find anybody who explains how to get it to also server my index.html
? When I run webpack serve --mode=development
all goes well, and when I navigate to the localhost port that I server to I get a 404 error.
One of the "tips" on the previously linked page reads
If you're having trouble, navigating to the
/webpack-dev-server
route will show where files are served. For example, http://localhost:9000/webpack-dev-server.
When I navigate to localhost:1234/webpack-dev-server
it tells me that it's only serving a single file: bundle.js
Clearly a JavaScript file alone is not going to work, and I need it to also serve the index.html
file that's in my dist
directory.
Now, my knowledge here is very limited as this is my first time working with Webpack, but here's all the detail I can give and hopefully it's not accidentally totally unrelated:
Whenever my dev server reloads itself (whenever it's booted up or if a watched file changes while it's running) it only updates the bundle.js
file that it's serving. It doesn't update the bundle.js
file stored in my dist
directory on my hard drive. How can I make it update both?
And also, how do I make the server also serve my index.html
file instead of only the bundle.js
? Where is it even getting that bundle.js
from? Is it compiling all of my code from scratch to create that js file or is it taking that out of the dist
directory?
And additionally, am I going about this totally in the wrong way? Where should my index.html
even go? I put my TypeScript files into a src
directory and they're converted to .js
files and moved into my dist
directory... Should I also put my index.html
inside src
or does it belong in dist
or somewhere else entirely? When I put index.html
in src
it doesn't get copied over into dist
, it just ignores it completely. If my index.html
file doesn't belong in src
it must belong in dist
, but if it belongs in dist
then how can I expect the dev server to find it and serve it along with the other TypeScript files in src
? Why can't the dev server just serve everything in dist
and automatically compile everything from src
into dist
? I must be misunderstanding the flow of it all, but I have no idea where to look for an explanation and I've been at this for several hours now.
Just a general explanation of how it all works would be very helpful as well, as I can't find a single article or forum post anywhere that details all the spaghetti going on with Webpack. I've been avoiding bundlers and all this NPM mess for as long as I can because I constantly run into issues like this, but I finally decided to jump in and just push through all the mess and I'm already regretting it. If someone could just point me (and other people having similar problems) to some good resources for learning about all the automagic going on that would be hugely appreciated. The Webpack documentation and guides are very much worthless to a newbie.
My Webpack config file:
const path = require("path");
module.exports = {
entry: "./src/index.ts",
module: {
rules: [
{
test: /\.tsx?$/,
use: "ts-loader",
exclude: /node_modules/,
}
]
},
resolve: {
extensions: [".tsx", ".ts", ".js"]
},
output: {
filename: "bundle.js",
path: path.resolve(__dirname, "dist")
},
devServer: {
compress: false,
static: false,
client: {
logging: "warn",
overlay: {
errors: true,
warnings: false
},
progress: true
},
port: 1234, host: "0.0.0.0"
}
};
CodePudding user response:
Is your entry set in webpack? So that it is included?
entry: {
main: 'path/to/index.ts'
},
You could also try adding to your index.ts file
require('file-loader?name=[name].[ext]!../index.html');
Or
require("./src/index.html");
// Then In your webpack config file add a loader
loaders : { { test: /\.html/, loader: 'file?name=[name].[ext]' } }
Otherwise you could always copy this file over with webpack copy plugin
{
plugins: [
new HtmlWebpackPlugin({
template: 'src/index.html'
})
]
}
will need to run npm install --save-dev html-webpack-plugin
to install it. As well as move your index.html file into your src folder
CodePudding user response:
Please visit localhost:1234/bundle
(Where bundle.js
is your bundled file) the magichHTML
route, which will load the HTML file with the bundled script.