使用 typescript 时 html-webpack-plugin 出现太多错误

2024-05-08

我正在使用 Webpack 和 typescript 启动一个项目。但是当我尝试运行开发服务器时,我在 html-webpack-plugin 上遇到很多错误。这是我的输出:

> [email protected] /cdn-cgi/l/email-protection dev /home/alejo/tecnimaq/tecnilab/tecnilab-frontend
> cross-env NODE_ENV=development webpack-dev-server --mode=development --inline --hot
 
ℹ 「wds」: Project is running at http://localhost:8080/
ℹ 「wds」: webpack output is served from /
ℹ 「wds」: Content not from webpack is served from ./public
ℹ 「wds」: 404s will fallback to /index.html
ℹ 「wdm」: wait until bundle finished: /
✖ 「wdm」: Hash: 9d40f5b1be348f5478dc
Version: webpack 4.43.0
Time: 10565ms
Built at: 21/07/2020 11:05:56 a. m.
     Asset       Size  Chunks             Chunk Names
 bundle.js   3.82 MiB    main  [emitted]  main
index.html  369 bytes          [emitted]  
Entrypoint main = bundle.js
[0] multi (webpack)-dev-server/client?http://localhost:8080 (webpack)/hot/dev-server.js ./src/index.tsx 52 bytes {main} [built]
[./node_modules/react-dom/index.js] 1.33 KiB {main} [built]
[./node_modules/react/index.js] 190 bytes {main} [built]
[./node_modules/webpack-dev-server/client/index.js?http://localhost:8080] (webpack)-dev-server/client?http://localhost:8080 4.29 KiB {main} [built]
[./node_modules/webpack-dev-server/client/overlay.js] (webpack)-dev-server/client/overlay.js 3.51 KiB {main} [built]
[./node_modules/webpack-dev-server/client/socket.js] (webpack)-dev-server/client/socket.js 1.53 KiB {main} [built]
[./node_modules/webpack-dev-server/client/utils/createSocketUrl.js] (webpack)-dev-server/client/utils/createSocketUrl.js 2.91 KiB {main} [built]
[./node_modules/webpack-dev-server/client/utils/log.js] (webpack)-dev-server/client/utils/log.js 964 bytes {main} [built]
[./node_modules/webpack-dev-server/client/utils/reloadApp.js] (webpack)-dev-server/client/utils/reloadApp.js 1.59 KiB {main} [built]
[./node_modules/webpack-dev-server/client/utils/sendMessage.js] (webpack)-dev-server/client/utils/sendMessage.js 402 bytes {main} [built]
[./node_modules/webpack-dev-server/node_modules/strip-ansi/index.js] (webpack)-dev-server/node_modules/strip-ansi/index.js 161 bytes {main} [built]
[./node_modules/webpack/hot sync ^\.\/log$] (webpack)/hot sync nonrecursive ^\.\/log$ 170 bytes {main} [built]
[./node_modules/webpack/hot/dev-server.js] (webpack)/hot/dev-server.js 1.59 KiB {main} [built]
[./node_modules/webpack/hot/emitter.js] (webpack)/hot/emitter.js 75 bytes {main} [built]
[./src/index.tsx] 218 bytes {main} [built]
    + 36 hidden modules
 
ERROR in /home/alejo/tecnimaq/tecnilab/tecnilab-frontend/node_modules/html-webpack-plugin/index.js
[tsl] ERROR in /home/alejo/tecnimaq/tecnilab/tecnilab-frontend/node_modules/html-webpack-plugin/index.js(16,19)
      TS2306: File '/home/alejo/tecnimaq/tecnilab/tecnilab-frontend/node_modules/lodash/lodash.js' is not a module.
 
ERROR in /home/alejo/tecnimaq/tecnilab/tecnilab-frontend/node_modules/html-webpack-plugin/index.js
[tsl] ERROR in /home/alejo/tecnimaq/tecnilab/tecnilab-frontend/node_modules/html-webpack-plugin/index.js(183,15)
      TS2322: Type 'this' is not assignable to type 'HtmlWebpackPlugin'.
  Type 'import("/home/alejo/tecnimaq/tecnilab/tecnilab-frontend/node_modules/html-webpack-plugin/index")' is not assignable to type 'import("/home/alejo/tecnimaq/tecnilab/tecnilab-frontend/node_modules/html-webpack-plugin/typings")'.
    Types of property 'apply' are incompatible.
      Type '(compiler: import("/home/alejo/tecnimaq/tecnilab/tecnilab-frontend/node_modules/webpack/lib/Compiler")) => void' is not assignable to type '(compiler: import("/home/alejo/tecnimaq/tecnilab/tecnilab-frontend/node_modules/@types/webpack/index").Compiler) => void'.
        Types of parameters 'compiler' and 'compiler' are incompatible.
          Type 'Compiler' is missing the following properties from type 'Compiler': parentCompilation, recordsInputPath, recordsOutputPath, records, and 21 more.
 
ERROR in /home/alejo/tecnimaq/tecnilab/tecnilab-frontend/node_modules/html-webpack-plugin/index.js
[tsl] ERROR in /home/alejo/tecnimaq/tecnilab/tecnilab-frontend/node_modules/html-webpack-plugin/index.js(201,13)
      TS2322: Type 'this' is not assignable to type 'HtmlWebpackPlugin'.
 
ERROR in /home/alejo/tecnimaq/tecnilab/tecnilab-frontend/node_modules/html-webpack-plugin/index.js
[tsl] ERROR in /home/alejo/tecnimaq/tecnilab/tecnilab-frontend/node_modules/html-webpack-plugin/index.js(213,15)
      TS2322: Type 'this' is not assignable to type 'HtmlWebpackPlugin'.

还有很多错误,我不得不截断它,以便它可以适合这个 stackoverflow 问题,完整输出在https://pastebin.com/NJXnbkAq https://pastebin.com/NJXnbkAq

如果您需要更多详细信息来了解此问题的原因,请告诉我

这是我的 package.json:

{
  "name": "tecnilab-frontend",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "cross-env NODE_ENV=development webpack-dev-server --mode=development --inline --hot",
    "prestart": "npm run build",
    "start": "sirv build -s -e",
    "prebuild": "mkdirp build",
    "build": "cross-env NODE_ENV=production webpack --mode=production --progress",
    "test": "jest --watchAll --passWithNoTests",
    "lint": "find . -path ./node_modules -prune -o -type f \\( -iname \\*\\.ts -o -iname \\*.tsx -o -iname \\*.js -o -iname \\*\\.jsx \\) -print | xargs -I{} npx eslint {}",
    "lintFix": "find . -path ./node_modules -prune -o -type f \\( -iname \\*\\.ts -o -iname \\*.tsx -o -iname \\*.js -o -iname \\*\\.jsx \\) -print | xargs -I{} npx eslint --fix {}"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^3.7.0",
    "@typescript-eslint/parser": "^3.7.0",
    "eslint": "^7.5.0",
    "eslint-config-standard": "^14.1.1",
    "eslint-plugin-import": "^2.22.0",
    "eslint-plugin-node": "^11.1.0",
    "eslint-plugin-promise": "^4.2.1",
    "eslint-plugin-react": "^7.20.3",
    "eslint-plugin-standard": "^4.0.1",
    "jest": "^26.1.0"
  },
  "dependencies": {
    "@babel/core": "^7.10.5",
    "@babel/plugin-proposal-class-properties": "^7.10.4",
    "@babel/plugin-syntax-dynamic-import": "^7.8.3",
    "@babel/preset-env": "^7.10.4",
    "@babel/preset-react": "^7.10.4",
    "@testing-library/jest-dom": "^5.11.1",
    "@testing-library/react": "^10.4.7",
    "@testing-library/user-event": "^12.0.11",
    "@types/jest": "^26.0.5",
    "@types/node": "^14.0.24",
    "@types/react": "^16.9.43",
    "@types/react-dom": "^16.9.8",
    "autoprefixer": "^9.8.5",
    "babel-eslint": "^10.1.0",
    "babel-jest": "^26.1.0",
    "babel-loader": "^8.1.0",
    "babel-preset-jest": "^26.1.0",
    "bootstrap": "^5.0.0-alpha1",
    "cross-env": "^7.0.2",
    "css-loader": "^3.6.0",
    "html-webpack-plugin": "^4.3.0",
    "mini-css-extract-plugin": "^0.9.0",
    "postcss-loader": "^3.0.0",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-test-renderer": "^16.13.1",
    "sass": "^1.26.10",
    "sass-loader": "^9.0.2",
    "style-loader": "^1.2.1",
    "terser-webpack-plugin": "^3.0.7",
    "ts-loader": "^8.0.1",
    "typescript": "^3.9.7",
    "url-loader": "^4.1.0",
    "webpack": "^4.43.0",
    "webpack-cli": "^3.3.12",
    "webpack-dev-server": "^3.11.0"
  }
}

我的 webpack.config.js

const webpack = require('webpack')
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const TerserPlugin = require('terser-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')

const ENV = process.env.NODE_ENV || 'development'

module.exports = {
  entry: './src/index.tsx',

  devtool: 'inline-source-map',

  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'build')
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: ['babel-loader']
      },
      {
        test: /\.(ts|tsx)?$/,
        use: 'ts-loader',
        exclude: /node_modules/
      },
      {
        test: /\.scss$/,
        use: [
          'style-loader', // inject CSS to page
          'css-loader', // translates CSS into CommonJS modules
          {
            loader: 'postcss-loader', // Run postcss actions
            options: {
              plugins: function () { // postcss plugins, can be exported to postcss.config.js
                return [
                  require('autoprefixer')
                ]
              }
            }
          },
          'sass-loader' // compiles Sass to CSS
        ]
      }

    ]
  },

  resolve: {
    extensions: ['.tsx', '.ts', '.js']
  },

  node: {
    browser: true,
    node: true
  },

  optimization: {
    minimizer: [
      new TerserPlugin({
        cache: true,
        parallel: true,
        sourceMap: true, // Must be set to true if using source-maps in production
        terserOptions: {
          // https://github.com/webpack-contrib/terser-webpack-plugin#terseroptions
        }
      })
    ]
  },

  plugins: ([
    new MiniCssExtractPlugin(),
    new webpack.DefinePlugin({
      'process.env.NODE_ENV': JSON.stringify(ENV)
    }),
    new HtmlWebpackPlugin({
      template: './src/index.html',
      hash: true,
      minify: { collapseWhitespace: true }
    })
  ]).concat(ENV === 'production' ? [
    new webpack.ProgressPlugin()
  ] : []),
  devServer: {
    port: process.env.PORT || 8080,
    host: 'localhost',
    publicPath: '/',
    contentBase: './public',
    historyApiFallback: true,
    open: true,
    openPage: ''
  }
}

还有我的index.tsx

import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'
import './css/main.scss'

ReactDOM.render(<App message="The count is:"/>, document.getElementById('root'))

尝试添加@types/html-webpack-plugin https://www.npmjs.com/package/@types/html-webpack-plugin到你的 devDependency,希望它能起作用。 :)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 typescript 时 html-webpack-plugin 出现太多错误 的相关文章

随机推荐

  • 按行号和列号对文件进行子集化

    我们想要按行和列对文本文件进行子集化 其中行数和列数是从文件中读取的 不包括标题 第 1 行 和行名称 第 1 列 输入文件 txt制表符分隔的文本文件 header 62 9 3 54 6 1 25 1 2 3 4 5 6 96 1 1
  • 现已弃用使用 Google Places API 获取多种类型

    谷歌最近宣布 自 2016 年 2 月 16 日起 types 参数已被弃用 取而代之的是新的类型参数 每个搜索请求仅支持一种类型 我的问题是 现在有什么方法 不使用已弃用的参数 从单个 API 调用中获取多个地点类型吗 谢谢 None
  • dplyr :过滤一系列行(在一列中)

    虚拟数据框 id family lt c 1 1 2 2 3 3 people lt c male female male female male children dataset lt data frame id family peopl
  • 演员邮箱溢出。斯卡拉

    我目前正在与 scala 的两位演员合作 一 producer 产生一些数据并将其发送到parcer 生产者发送一个HashMap String HashMap Object List Int 通过消息 以及this标记发件人 parcer
  • 不透明div内的透明文本

    我有一个背景图像 上面有一个白色的 div 我希望该 div 内的文本是透明的 以便您可以 透过 背景图像 这有可能吗 应该看起来像这样 您需要将其用于您的文本CSS webkit text fill color transparent
  • 使用 Azure 表存储进行代码优先和身份验证

    我正在开发一个小型网络应用程序 并且刚刚达到开发阶段 我需要开始做出数据库决策 我最初的计划是在 Azure 上使用 EF Code First 和 MSSQL 因为它只是简化了使用数据库的过程 然而 当我研究 Azure 上的数据库托管功
  • 具有异步管道多个条件的 Angular *ngIf 变量

    有一个关于在 Angular 中使用 ngIf 的很好的文档 https angular io api common NgIf https angular io api common NgIf但是 是否可以有 ngIf 异步变量并对其进行多
  • iOS 8 使用 UITextView 自动调整 UITableViewCell 大小

    iOS 8 引入了一种让 tableView 根据内容自动调整单元格高度的方法 通过 AutoLayout in viewDidLoad tableView rowHeight UITableViewAutomaticDimension t
  • 引发 404 并继续 URL 链

    我有一个像这样的 URL 模式 urlpatterns url r list titles name list url r P
  • 在 C 或 C++ 中使用逗号作为宏名称

    我想做这样的事情 define define MAX 10 000 000 undef 有什么技巧可以做到吗 编辑 我知道 C 14 中的数字分隔符 我正在寻找一种技巧来对不兼容的编译器执行相同的操作 EDIT2 请考虑Variadic M
  • ASP.NET 搜索表单 - 动态 Linq to SQL?

    我有一个搜索表单 允许用户以多种不同的方式搜索多个不同的字段 这是我的代码的示例 var claims from c in db Claims select c switch ddlSearchField Text case StartsW
  • Twitter 不再使用请求库 python

    我有一个 python 函数 它使用 requests 库和 BeautifulSoup 来抓取特定用户的推文 import requests from bs4 import BeautifulSoup contents requests
  • 使用 libgdx 裁剪图像

    I need to crop image like this 我需要从中心绘制部分图像 我知道有一个带有很多参数的批处理的draw 方法 但是没有关于所有这些参数的良好文档 所以我不知道如何使用它 这是我实现的 public class T
  • 如何创建一个类似“隐形”的Android应用程序?

    我想让我的应用程序以某种 隐形 模式运行 我想做的两件主要事情 不以编程方式在已安装的应用程序列表 抽屉 中显示应用程序图标 通过拨号盘启动应用程序 一些特殊的数字组合 我知道我可以删除启动器意图过滤器来隐藏图标
  • 使用“onclick”和 JavaScript 获取按钮上的 X/Y 坐标

    我是一名 JavaScript 初学者 正在寻找一种方法来获取单击按钮时的 x 和 y 坐标 这适用于 Opera IE9 和 Chrome 但我无法让它在 Firefox 中工作 到目前为止 这是我的代码 JavaScript 中的函数
  • SQLSTATE[HY000] [2002] 连接尝试失败,因为连接方在一段时间后没有正确响应,

    我尝试连接 2 个数据库Laravel 5 6项目 但我遇到这样的错误 SQLSTATE HY000 2002 连接尝试失败 因为连接方在一段时间后没有正确响应 或者由于连接的主机未能响应而建立的连接失败 SQL 从中选择 mt merch
  • Nodejs Base64 中的读取文件

    我正在尝试从客户端读取以 base64 编码的图像 如何使用nodejs进行阅读 My code add to buffer base64 image var encondedImage new Buffer image name base
  • 垫子选择面板最小宽度

    我正在尝试使用多个复选框自定义垫选择 由于某种原因 面板的最小宽度错误 如下所示 我不知道它在哪里计算这个最小宽度 我还尝试添加 panelClass 并覆盖此类的最小宽度 例如
  • datatables.search 函数修改后的奇怪行为

    这个问题是后续问题这个问题 https stackoverflow com questions 54671211 overriding datatables js search behavior 我已经创建了这个 JSFiddle http
  • 使用 typescript 时 html-webpack-plugin 出现太多错误

    我正在使用 Webpack 和 typescript 启动一个项目 但是当我尝试运行开发服务器时 我在 html webpack plugin 上遇到很多错误 这是我的输出 gt email protected cdn cgi l emai