`[WDS] Disconnected`ReactJS,webpack

时间:2018-03-26 18:40:06

标签: javascript reactjs webpack

我在运行yarn start后收到此错误。我正在尝试用 webpack Babel

开发一个hello-word ReactJS 应用程序

[WDS] Disconnected! Content Security Policy: The page’s settings blocked the loading of a resource at http://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.12/semantic.min.css (“style-src”). Content Security Policy: The page’s settings blocked the loading of a resource at blob:http://localhost:3000/edcc77c5-a0d7-4b24-9f6d-5e740ffd77df (“style-src”).

这是我的webpack.config.js文件

const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const port = process.env.PORT || 3000;
module.exports = {
    mode: 'development',  
    entry: './src/index.js',
    output: {
        filename: 'bundle.[hash].js'
    },
    devtool: 'inline-source-map',
    module: {
        rules: [
            {
                test: /\.(js)$/,
                exclude: /node_modules/,
                use: ['babel-loader']
            },
            {
                test: /\.css$/,
                use: [
                    {
                        loader: 'style-loader'
                    },
                    {
                        loader: 'css-loader',
                        options: {
                          modules: true,
                          camelCase: true,
                          sourceMap: true
                        }
                    }
                ]
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: 'public/index.html',
            favicon: 'public/favicon.ico'
        })
    ],
    devServer: {
        host: 'localhost',
        port: port,
        historyApiFallback: true,
        open: true
    }
};

这是我的package.jon文件

{
  "scripts": {
    "start": "webpack-dev-server"
  },
  "dependencies": {
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "react-prop-types": "^0.4.0",
    "react-router-dom": "^4.2.2",
    "semantic-ui-react": "^0.79.0"
  },
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.4",
    "babel-preset-env": "^1.6.1",
    "babel-preset-react": "^6.24.1",
    "babel-preset-stage-1": "^6.24.1",
    "css-loader": "^0.28.11",
    "html-webpack-plugin": "^3.1.0",
    "style-loader": "^0.20.3",
    "webpack": "^4.2.0",
    "webpack-cli": "^2.0.13",
    "webpack-dev-server": "^3.1.1"
  }
}

公共/ index.html中

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
  <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.12/semantic.min.css"></link>
  <title>webpack-for-react</title>
</head>

<body>
  <div id="root"></div>
</body>

</html>

我试图解决这个错误。但我无法做到。如果有人有线索请帮助我。我认为这个错误来自webpack.config.js文件。

1 个答案:

答案 0 :(得分:2)

问题在于您使用的Content-Security-Policy

目前您只允许使用本地css文件和内联样式。

当您尝试导入semantic.min.css时, cloudflare 不是允许的主机。

所以你可能需要添加

<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline' http://cdnjs.cloudflare.com; script-src 'self' 'unsafe-inline' 'unsafe-eval'">

我建议您阅读CSP documentation以了解详情。

并且在开发过程中也尝试使用Content-Security-Policy-Report-Only,因此您只能获得违规报告。