webpack生产模式编译时如何忽略打字稿错误

kaz*_*zon 5 javascript typescript reactjs webpack

环境

网络包 4.41.2

打字稿 3.7.2

问题

当我通过webpack开发模式编译文件时,没有问题。但是我用生产模式编译的时候,错误很多,编译不了。

目的地

找到在 webpack 通过生产模式编译时如何忽略打字稿错误的方法

代码

?webpack.config.js (js 部分)

{
    mode: "development",
    entry: "./src/index.tsx",
    output: {
        path: `${__dirname}/dist`,
        filename: "index.js"
    },
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                use: "ts-loader"
            },
            {
                test: /\.svg$/,
                loader: "react-svg-loader",
                options: {
                    svgo: {
                        plugins: [
                            { removeTitle: false }
                        ],
                        floatPrecision: 2
                    }
                }
            },
            {
                test: /\.(vert|frag|glsl)$/,
                use: {
                    loader: 'webpack-glsl-loader'
                }
            }
        ]
    },
    resolve: {
        extensions: [".ts", ".tsx", ".js", ".json"]
    },
},
Run Code Online (Sandbox Code Playgroud)

? 配置文件

{
  "compilerOptions": {
    "sourceMap": false,
    "target": "es5",
    "module": "es2015",
    "jsx": "react",
    "moduleResolution": "node",
    "lib": [
      "es2019",
      "dom"
    ],
    "removeComments": true,
    "noUnusedLocals": false
  }
}
Run Code Online (Sandbox Code Playgroud)

错误内容

ERROR in /var/www/hoge/src/index.tsx
[tsl] ERROR in /var/www/hoge/src/index.tsx(56,33)
      TS2322: Type '(page: any) => void' is not assignable to type 'void'.

ERROR in /var/www/hoge/src/about/index.tsx
[tsl] ERROR in /var/www/hoge/src/about/index.tsx(15,48)
      TS2339: Property 'appRef' does not exist on type 'About'.

ERROR in /var/www/hoge/src/gallery/index.tsx
[tsl] ERROR in /var/www/hoge/src/gallery/index.tsx(8,27)
      TS2307: Cannot find module './picturesData'.

ERROR in /var/www/hoge/src/gallery/index.tsx
[tsl] ERROR in /var/www/hoge/src/gallery/index.tsx(9,9)
      TS2529: Duplicate identifier 'Promise'. Compiler reserves name 'Promise' in top level scope of a module containing async functions.

...and other almost similar 40 errors
Run Code Online (Sandbox Code Playgroud)

到目前为止我尝试过的

?在互联网上检查类似的帖子,比如 在 Webpack-dev-server 中忽略打字稿错误如何忽略 webpack 中的打字稿错误? 但这对我没有帮助

? 将此代码添加到 tsconfig.js

"no-consecutive-blank-lines": false,
"no-unused-variable": false,
Run Code Online (Sandbox Code Playgroud)

但错误“未知的编译器选项”

Chr*_* Su 21

module.exports = {
  ...
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: [
          {
            loader: 'ts-loader',
            options: {
              transpileOnly: true
            }
          }
        ]
      }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 您应该解释您的代码的作用以及它如何帮助问题的作者。 (14认同)