繁体   English   中英

Chrome Dev Tools 上的 Webpack 和 Babel 配置问题

[英]Webpack and Babel configuration problem on Chrome Dev Tools

我正在尝试构建新项目,我希望它拥有 webpack 和 babel,应用程序可以工作,在网页上刷新,但是我在 Chrome 开发工具中遇到了问题。 我尝试添加 babel-plugin-add-module-exports。 我希望在 Sources 中看到与在我的代码中看到的相同的内容。 我缺少什么?

例如我的文件 Contact.js 看起来:

 
const Contact = () => {
    return (
       <div>
          <h1>Contact US</h1>
          <p>Contact US page body content</p>
       </div>
    );
}
 
export default Contact;

但是在 Sources 中的 devtools 上,我得到了:

/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "default": () => __WEBPACK_DEFAULT_EXPORT__
/* harmony export */ });
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! react */ "./node_modules/react/index.js");


var Contact = function Contact() {
  return /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0__.createElement("div", null, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0__.createElement("h1", null, "Contact US"), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0__.createElement("p", null, "Contact US page body content"));
};

/* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = (Contact);

webpack.config.js

import HtmlWebPackPlugin from 'html-webpack-plugin';

module.exports = {
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader"
        }
      },
      {
        test: /\.html$/,
        use: [
          {
            loader: "html-loader"
          }
        ]
      }
    ]
  },
  plugins: [
    new HtmlWebPackPlugin({
      template: "./src/index.html",
      filename: "./index.html"
    })
  ]
};

包.json:

{
  "name": "newstart",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "webpack serve --mode development --env development",
    "build": "webpack --mode production"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.12.3",
    "@babel/plugin-proposal-class-properties": "^7.12.1",
    "@babel/preset-env": "^7.12.1",
    "@babel/preset-react": "^7.12.1",
    "babel-loader": "^8.1.0",
    "babel-plugin-add-module-exports": "^1.0.4",
    "html-loader": "^1.3.2",
    "html-webpack-plugin": "^5.0.0-alpha.6",
    "webpack": "^5.1.3",
    "webpack-cli": "^4.1.0",
    "webpack-dev-server": "^3.11.0"
  },
  "dependencies": {
    "axios": "^0.20.0",
    "bootstrap": "^4.5.3",
    "react": "^17.0.0",
    "react-bootstrap": "^1.4.0",
    "react-dom": "^17.0.0",
    "react-router-dom": "^5.2.0"
  }
}

.babelrc:

{
    "presets": [
      "@babel/preset-env", 
      "@babel/preset-react"],
      
    "plugins": [
      [
        "@babel/plugin-proposal-class-properties",
        {
          "loose": true
        }
      ]
    ]
  }

正如 Matti 提到的,您将需要源映射,详细信息可以在 webpack 官方网站上找到,网址为https://webpack.js.org/configuration/devtool/

你的 webpack 看起来像注意添加 devtool: 'source-map'

import HtmlWebPackPlugin from 'html-webpack-plugin';

module.exports = {
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader"
        }
      },
      {
        test: /\.html$/,
        use: [
          {
            loader: "html-loader"
          }
        ]
      }
    ]
  },
devtool: 'source-map',
  plugins: [
    new HtmlWebPackPlugin({
      template: "./src/index.html",
      filename: "./index.html"
    })
  ]
};

暂无
暂无

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

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