简体   繁体   English

如何防止 webpack 为未使用的模块引发 typescript 错误?

[英]How do I prevent webpack from throwing typescript errors for unused modules?

I have the following structure:我有以下结构:

└── src
    ├── tsconfig.json
    ├── core
    │   ├── [...].ts
    └── ui
        ├── [...].tsx
        └── tsconfig.json

In the frontend I import a small number of modules from the core.在前端,我从核心导入少量模块。 These modules, and any dependent modules, are compliant with both tsconfig files.这些模块以及任何依赖模块都与这两个 tsconfig 文件兼容。

tsc and eslint pass with no errors and webpack builds the desired output file. tsc 和 eslint 顺利通过,webpack 构建了所需的 output 文件。 So far so good.到目前为止,一切都很好。

However when webpack builds it throws loads of type errors for other backend modules.但是,当 webpack 构建时,它会为其他后端模块引发大量类型错误。

How do I suppress these errors?如何抑制这些错误? I tried excluding src/core from babel-loader and including the required modules but I was still getting the same errors.我尝试从babel-loader中排除src/core并包含所需的模块,但我仍然遇到相同的错误。

/// webpack.config.js
/* eslint-disable @typescript-eslint/no-var-requires */

const path = require('path');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  mode: 'development',

  entry: './src/ui',

  output: {
    path: path.resolve(__dirname, './ui-dist'),
  },

  // Enable sourcemaps for debugging webpack's output.
  devtool: 'source-map',

  resolve: {
    // Add '.ts' and '.tsx' as resolvable extensions.
    extensions: ['.ts', '.tsx', '.js', '.jsx'],
  },

  module: {
    rules: [
      {
        test: /\.(j|t)sx?$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: {
            cacheDirectory: true,
            babelrc: false,
            presets: [
              [
                '@babel/preset-env',
                { targets: { browsers: 'last 2 versions' } }, // or whatever your project requires
              ],
              '@babel/preset-typescript',
              '@babel/preset-react',
            ],
            plugins: [
              // plugin-proposal-decorators is only needed if you're using experimental decorators
              //  in TypeScript
              ['@babel/plugin-proposal-decorators', { legacy: true }],
              ['@babel/plugin-transform-runtime', { legacy: true }],
              ['@babel/plugin-proposal-class-properties', { loose: true }],
              'react-hot-loader/babel',
            ],
          },
        },
      },
      // All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
      {
        enforce: 'pre',
        test: /\.js$/,
        loader: 'source-map-loader',
      },
    ],
  },
  plugins: [
    new ForkTsCheckerWebpackPlugin({
      tsconfig: path.resolve(__dirname, './src/ui/tsconfig.json'),
      eslint: true,
      /** Options to supply to eslint https://eslint.org/docs/developer-guide/nodejs-api#cliengine */
      // eslintOptions: EslintOptions;
    }),
    new HtmlWebpackPlugin({
      title: 'development',
      template: './src/ui/template.html',
    }),
  ],
  // When importing a module whose path matches one of the following, just
  // assume a corresponding global variable exists and use that instead.
  // This is important because it allows us to avoid bundling all of our
  // dependencies, which allows browsers to cache those libraries between builds.
  // externals: {
  //   react: 'React',
  //   'react-dom': 'ReactDOM',
  // },
  devServer: {
    contentBase: path.resolve(__dirname, './ui-dist'),
  },
};

EDIT: I suppose I am referencing these modules throwing an error by using import type { x } from '../core/index.ts' .编辑:我想我是通过使用import type { x } from '../core/index.ts'来引用这些引发错误的模块。 Perhaps I need to find a way for babel-loader to skip scanning type imports.也许我需要为babel-loader找到一种跳过扫描类型导入的方法。

removing ForkTsCheckerWebpackPlugin did the trick.删除ForkTsCheckerWebpackPlugin就成功了。 Type checking is done when calling tsc in any case.在任何情况下调用 tsc 时都会进行类型检查。

暂无
暂无

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

相关问题 如何防止打字稿在引用另一个对象方法的对象上引发错误 - How can I prevent typescript from throwing errors on an object that references another object's method 如何防止在 webpack 构建期间丢弃未使用的代码? - How to prevent unused code from being dropped during webpack build? 如何为库设置 TypeScript 编译器,以便 Webpack 将依赖项目中未使用的模块截断? - How to setup the TypeScript compiler for the library so that the unused modules will be cut off by Webpack in the dependents projects? Typescript / Electron / Webpack模块如何/为什么有绝对路径? - How/Why do Typescript/Electron/Webpack modules have absolute paths? Webpack + Babel 正在转译 Webpack 加载器,我该如何防止? - Webpack + Babel is transpiling Webpack loaders, how do I prevent this? 如何初始化和使用其他模块(WebPack和ES6)导出的ReactJS类? - How do I initialize and use exported ReactJS classes from other modules (with WebPack and ES6)? 如何防止 webpack 重写 import() 以便我可以使用 buildIn one? - How do I prevent webpack from rewriting import() so that I can use the buildIn one? 如何为Webpack的动态import()调整模块形状? - How do I need to shape my modules for dynamic import() for webpack? 关于webpack中的nodejs路径,webpack如何在typescript中找到模块? - About nodejs path in webpack, how webpack find modules in typescript? Webpack 正在从我机器的全局“node_modules”导入模块。 如何让它只从我的项目的 `node_modules` 导入? - Webpack is importing modules from my machine's global `node_modules`. How do I get it to only import from my project's `node_modules`?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM