【问题标题】:Webpack can't resolve type from io-tsWebpack 无法从 io-ts 解析类型
【发布时间】:2020-11-16 21:55:04
【问题描述】:

我目前正在使用 TS + React 制作一个简单的应用程序,其中包含一些对服务器的 API 请求。当我尝试使用 io-ts 来解码响应时,webpack 会以 Module not found: Error: Can't resolve '../shared/Response' 响应 - 如果我删除使用 io-ts 来解码响应,我不会收到该错误。

我的文件夹结构是

src
    client
        PlayerTimer.tsx
        <skipped>
    server
        <skipped>
    shared
        Phase.d.ts
        Response.d.ts

Phase.d.ts 包含以下内容:

import * as t from 'io-ts';

export const PhaseDecode = t.union([
  t.literal(1),
  t.literal(2),
  t.literal(3),
  t.literal(4),
  t.literal(5),
]);
export type Phase = t.TypeOf<typeof PhaseDecode>

Response.d.ts 包含以下内容:

import * as t from 'io-ts';
import { DateFromISOString as IoDate } from 'io-ts-types/DateFromISOString';
import { PhaseDecode } from './Phase';

const ApiResponseDecode = t.type({
  turnNumber: t.number,
  phase: PhaseDecode,
  breakingNews: t.union([t.string, t.null]),
  active: t.boolean,
  phaseEnd: IoDate
});

type ApiResponse = t.TypeOf<typeof ApiResponseDecode>

export { ApiResponseDecode, ApiResponse as default };

PlayerTimer.tsx 包含一堆 React 组件,但这可以通过顶部的以下代码重现

import { ApiResponseDecode } from '../shared/Response';

const temp = {};

if (ApiResponseDecode.is(temp)) {
  console.log('Webpack fails');
}

我的 webpack 配置是:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CopyPlugin = require('copy-webpack-plugin');

const outputDirectory = 'dist';

module.exports = {
  entry: ['babel-polyfill', './src/client/index.tsx'],
  output: {
    path: path.join(__dirname, outputDirectory),
    filename: './js/[name].bundle.js'
  },
  devtool: 'source-map',
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader'
        }
      },
      {
        test: /\.tsx?$/,
        use: 'ts-loader',
        exclude: /node_modules/
      },
      {
        enforce: 'pre',
        test: /\.js$/,
        loader: 'source-map-loader'
      },
      {
        test: /\.less$/,
        use: [
          { loader: 'style-loader' },
          {
            loader: MiniCssExtractPlugin.loader,
            options: {
              publicPath: './Less',
              hmr: process.env.NODE_ENV === 'development',
            },
          },
          { loader: 'css-loader' },
          {
            loader: 'less-loader',
            options: {
              strictMath: true,
              noIeCompat: true,
            }
          },
        ]
      },
      {
        test: /\.css$/,
        use: [
          { loader: 'style-loader' },
          { loader: 'css-loader' },
        ],
      },
      {
        test: /\.(png|woff|woff2|eot|ttf|svg)$/,
        loader: 'url-loader?limit=100000'
      },
    ]
  },
  resolve: {
    extensions: ['*', '.ts', '.tsx', '.js', '.jsx', '.json', '.less']
  },
  devServer: {
    port: 3000,
    open: true,
    hot: true,
    historyApiFallback: true,
    proxy: {
      '/api/**': {
        target: 'http://localhost:8050',
        secure: false,
        changeOrigin: true
      }
    }
  },
  plugins: [
    new CleanWebpackPlugin([outputDirectory]),
    new HtmlWebpackPlugin({
      template: './public/index.html',
      favicon: './public/favicon.ico',
      title: 'Test application',
    }),
    new MiniCssExtractPlugin({
      filename: './css/[name].css',
      chunkFilename: './css/[id].css',
    }),
    new CopyPlugin([
      { from: './src/client/Assets', to: 'assets' },
    ])
  ],
};

【问题讨论】:

  • 为了增加乐趣 - 如果我将类型复制到 PlayerTimer.tsx 并删除导入,一切正常。

标签: javascript typescript webpack


【解决方案1】:

我通过将类型定义和 io-ts 定义移动到单独的文件中来解决此问题。我真的不明白为什么会这样,但我会添加这个以防其他人发现这个

【讨论】:

    猜你喜欢
    • 2020-11-18
    • 2021-09-18
    • 1970-01-01
    • 2021-02-24
    • 2015-10-22
    • 1970-01-01
    • 2016-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多