Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore typescript errors when compile by webpack production mode

environment

webpack 4.41.2

typescript 3.7.2

problem

When I compile files by webpack development mode, there is no problem. But when I compile by production mode, there is a lot of errors and I can't compile.

destination

find the way how to ignore typescript errors when webpack compiles by production mode

code

▼webpack.config.js (js part)

{
    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"]
    },
},

▼ tsconfig

{
  "compilerOptions": {
    "sourceMap": false,
    "target": "es5",
    "module": "es2015",
    "jsx": "react",
    "moduleResolution": "node",
    "lib": [
      "es2019",
      "dom"
    ],
    "removeComments": true,
    "noUnusedLocals": false
  }
}

error content

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

What I've tried so far

・check similer post in Internet like these Ignore Typescript errors in Webpack-dev-server How to ignore typescript errors with webpack? but it doesn't help me

・add this code to tsconfig.js

"no-consecutive-blank-lines": false,
"no-unused-variable": false,

but error "Unknown compiler option"

like image 925
kazon Avatar asked Feb 18 '20 10:02

kazon


2 Answers

module.exports = {
  ...
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: [
          {
            loader: 'ts-loader',
            options: {
              transpileOnly: true
            }
          }
        ]
      }
    ]
  }
}
like image 83
Chris Su Avatar answered Nov 12 '22 01:11

Chris Su


use ignoreDiagnostics to ignore certain TypeScript errors, but this is more applicable when you just want to run code

{
  test: /\.tsx?$/,
  use: "ts-loader",
  options: {
    ignoreDiagnostics: [2322]
  }
},

https://www.npmjs.com/package/ts-loader#ignorediagnostics

like image 27
marsk Avatar answered Nov 12 '22 01:11

marsk