[英]not able to reference javascript and css files from my index.html generated by webpack in dist folder
After I introduced Routes on my app I started to face problem when of getting not found page when made changes on my code. 在我的应用中引入Routes之后,当我对代码进行更改时,在找不到页面时开始遇到问题。
I googled it and i found out that on my webpack i need to add something like: 我在Google上搜索了一下,发现我需要在Webpack上添加以下内容:
publicPath: '/',
historyApiFallback: true,
After the change i made by adding the above into my webpack when running npm run build it generates a dist folder with index.html bundle.css and bundle.js but the reference files are not like: 在更改后,通过在运行npm run build时将以上内容添加到我的webpack中进行了更改,它生成了一个带有index.html bundle.css和bundle.js的dist文件夹,但参考文件不是这样的:
<link href="/bundle.css" rel="stylesheet">
it used to be as: 它曾经是:
<link href="bundle.css" rel="stylesheet">
so basically the production mode is not showing error of 404 not found file. 因此基本上生产模式不会显示404找不到文件的错误。
WHole Webpack file looks as following: WHole Webpack文件如下所示:
const HtmlWebPackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CleanWebpackPlugin = require('clean-webpack-plugin');
const webpack = require('webpack');
const path = require('path');
module.exports = (env, argv) => {
console.log("ENV DETECTED: " + argv.mode);
return {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
publicPath: '/',
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
{
test: /\.html$/,
use: [
{
loader: "html-loader",
options: {
minimize: true
}
}
]
},
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
// 'style-loader',
{
loader: 'css-loader',
options: {
importLoaders: 1,
minimize: true
}
},
{
loader: 'postcss-loader',
options: {
config: {
path: './postcss.config.js'
}
}
}
]
},
{
test: /\.scss$/,
use: [
argv.mode !== 'production' ? 'style-loader' : MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
importLoaders: 1,
minimize: true
}
},
{
loader: 'postcss-loader',
options: {
config: {
path: './postcss.config.js'
}
}
},
"sass-loader"
]
}
],
},
devServer: {
historyApiFallback: true,
},
plugins: [
new CleanWebpackPlugin('dist', {}),
new HtmlWebPackPlugin({
template: "src/index.html",
filename: "./index.html"
}),
new MiniCssExtractPlugin({
filename: "bundle.css",
chunkFilename: "bundle.css"
}),
require('autoprefixer'),
]
}
};
So if i want to not have problem with referencing files from my index.html within /dist I can just comment publicPath: '/', from webpack but then I have problem i have to refresh my browser to see my latest changes. 因此,如果我希望在/ dist中从index.html引用文件没有问题,我可以在webpack中注释publicPath:'/',但是我有问题,我必须刷新浏览器以查看最新更改。
I dont know how can I fix so that I can reference files from index.html and at the same time being able to use publicPath: '/', so i dont have problem with livereload! 我不知道该如何解决,以便可以从index.html引用文件,同时可以使用publicPath:'/',所以livereload没有问题!
Check the file paths. 检查文件路径。 Using a relative file path starting from where ur file is located.
使用从ur文件所在的位置开始的相对文件路径。
Assuming ur file path is in public/css/bundle.css and ur index.html is in the root folder. 假设您的文件路径位于public / css / bundle.css中,而您的index.html位于根文件夹中。 Your link tag will look like this:
您的链接标记将如下所示:
<Link href = "./public/css/bundle.css" relax="text/stylesheet">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.