简体   繁体   English

Webpack 开发服务器 - 不允许加载本地资源

[英]Webpack dev server - Not allowed to load local resource

I'm using webpack-dev-server in order to develop a React app on windows.我正在使用 webpack-dev-server 来在 Windows 上开发 React 应用程序。

When running the command: webpack-dev-server --config webpack/webpack.dev.js , then going to localhost, I'm getting an error message for the js file (the bundled one):运行命令时: webpack-dev-server --config webpack/webpack.dev.js ,然后转到本地主机,我收到 js 文件(捆绑的)的错误消息:

Not allowed to load local resource: file:///C:/Dev/react-starter/dist/main.js

I'm not fully familiar with webpack-dev-server, but didn't get a sense about why this can happen even from the docs and the GH issues.我对 webpack-dev-server 并不完全熟悉,但即使从文档和 GH 问题中也无法理解为什么会发生这种情况。

my config looks like:我的配置看起来像:

var path = require('path');
var WebpackDashboard = require('webpack-dashboard');
var HtmlWebpackPlugin = require('html-webpack-plugin');

var srcFolder   = path.resolve(__dirname, '../src');
var buildFolder = path.resolve(__dirname, '../dist'); 
var publicFolder = path.resolve(__dirname, '../assets');

module.exports = { 
    target: 'web',
    entry: './index.js',
    context: srcFolder,
    devtool: 'source-map',

    output: {
        path: buildFolder,
        publicPath: path.resolve(__dirname, '../dist/'),        
        filename: '[name].js',
        chunkFilename: '[name].chunk.js',

    },

    resolve: {
        extensions: ['.js', '.jsx', '.json', '.scss'],
        alias: {
            '@': srcFolder,
        }
    },

    module: {
        rules: [
            {
                test: /\.(js|jsx)/,
                loader: 'babel-loader',
                exclude: /node_modules/
            },
            {
                test: /\.scss$/,
                use: [
                    { loader: 'style-loader' },
                    { loader: 'css-loader' },
                    { loader: 'sass-loader' },
                ]
            }
        ]
    },

    plugins: [
        new HtmlWebpackPlugin({
            inject: 'body',
            template: path.resolve(__dirname, '../src/index.html'),
        }),
    ],

    devServer: {
        contentBase: path.join(__dirname, '../dist'),
        port: 4464,
        hot: true,
        publicPath: buildFolder,
    }

}

Hope someone can help.希望有人能帮忙。 Tnx!天啊!

I got the same problem today, and I think I know where is the problem.我今天遇到了同样的问题,我想我知道问题出在哪里了。 It's the publicPath .这是publicPath When we have this value, the script tag in html file should be updated accordingly, but it seems like it's not easy to change something auto-generated by HtmlWebpackPlugin.当我们有这个值时,html文件中的脚本标签应该相应地更新,但似乎很难改变HtmlWebpackPlugin自动生成的东西。

So easy fix is delete the publicPath value.简单的解决方法是删除publicPath值。 Works for me.为我工作。

I also got the same problem today.我今天也遇到了同样的问题。 My solution is to move/copy the local resource to the public folder.我的解决方案是将本地资源移动/复制到公用文件夹。 After doing so, I also updated the path that references that resource accordingly in the js/html file to //<resource_name>.这样做之后,我还将 js/html 文件中相应地引用该资源的路径更新为 //<resource_name>。

It does not require to modify the webpack config.它不需要修改 webpack 配置。 Hope it helps!^_^希望能帮到你!^_^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM