簡體   English   中英

Webpack 無法從 io-ts 解析類型

[英]Webpack can't resolve type from io-ts

我目前正在使用 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' },
    ])
  ],
};

我通過將類型定義和 io-ts 定義移動到單獨的文件中來解決這個問題。 我真的不明白為什么會這樣,但我會添加這個以防其他人發現這個

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM