简体   繁体   English

无法从dist文件夹中的webpack生成的index.html中引用javascript和css文件

[英]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.

相关问题 Webpack 从入口文件导入,在 dist/index.html 中不可用 - Webpack imports from entry file, not available in dist/index.html 如何从index.html访问webpack生成的js文件 - How to access webpack generated js file from index.html Webpack - 在 index.html 中更改 javascript 文件的“src”路径 - Webpack - Change 'src' path of javascript files in index.html 使用grunt自动将本地* .js和* .css文件引用到index.html - Automatic reference of local *.js and *.css files into index.html with grunt Github 实时预览 readme 而不是 /dist/ 文件夹中的 index.html - Github live previews readme instead of index.html in the /dist/ folder 对于Angular2项目,我如何连接从typescript生成的所有JavaScript文件并将它们添加到我的index.html文件中 - For Angular2 project, In gulp how do I concat all my JavaScript files that were generated from typescript and add them to my index.html file 无法从index.html调用javascript函数 - Not able to call javascript function from index.html 无法将javascript从index.html移动到外部文件 - Not able to move javascript from index.html into external file 使用 Webpack 5 将 `html/index.pug` 构建到 `dist/index.html` - Build `html/index.pug` into `dist/index.html` using Webpack 5 webpack-dev-server 不提供 index.html 由 HtmlWebPackPlugin 生成 - webpack-dev-server not serving index.html generated by HtmlWebPackPlugin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM