Webpack不解析Bun monorepo typescript包

k5ifujac  于 2023-10-19  发布在  Webpack
关注(0)|答案(1)|浏览(125)

所以我有一个monorepo使用bun和nextjs将以下文件系统:

当我试图访问“world types”包来获取typescript类型时,我得到以下错误:

Module parse failed: Unexpected token (1:7)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> export enum ClientOPCodes {
|     PlayerSetup = -1,
|     Move = 1,

Import trace for requested module:
../../packages/world-types/websockets/ClientResponse.ts
../../packages/world-types/websockets/index.ts
./logic/game.ts
./app/page.tsx

这是typescript文件:

export enum ClientOPCodes {
    PlayerSetup = -1,
    Move = 1,
    Click,
    Action,
}

type OPCodeMapper = {
    [ClientOPCodes.Click]: { a: 30 };
    [ClientOPCodes.Action]: { a: 30 };
    [ClientOPCodes.PlayerSetup]: { name: string };
    [ClientOPCodes.Move]: { a: 20 };
};

export interface ClientResponse<T extends keyof typeof ClientOPCodes> {
    op: (typeof ClientOPCodes)[T];
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    data: OPCodeMapper[(typeof ClientOPCodes)[T]];
}

从这个错误中,我假设它出于某种原因没有解析 typescript 。这可能是因为它假设它是一个模块,它已经被转译成JavaScript,但是因为Bun本身支持类型脚本,所以它不转译。
以下是所有项目的package.json:

// Nextjs app
{
  "name": "website",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev -p 8000",
    "build": "next build",
    "start": "next start -p 8000",
    "lint": "next lint"
  },
  "devDependencies": {
    "eslint-config-custom": "workspace:*"
  },
  "dependencies": {
    "logger": "workspace:*",
    "world-types": "workspace:*",
    "@radix-ui/react-dropdown-menu": "^2.0.5",
    "@radix-ui/react-label": "^2.0.2",
    "@radix-ui/react-select": "^1.2.2",
    "@radix-ui/react-slot": "^1.0.2",
    "@types/node": "20.6.2",
    "@types/react": "18.2.21",
    "@types/react-dom": "18.2.7",
    "autoprefixer": "10.4.15",
    "class-variance-authority": "^0.7.0",
    "clsx": "^2.0.0",
    "lucide-react": "^0.279.0",
    "next": "13.4.19",
    "next-themes": "^0.2.1",
    "pixi.js": "latest",
    "postcss": "8.4.29",
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "tailwind-merge": "^1.14.0",
    "tailwindcss": "3.3.3",
    "tailwindcss-animate": "^1.0.7",
    "typescript": "5.2.2"
  }
}
// Root
{
  "name": "root",
  "version": "0.0.0",
  "workspaces": [
    "packages/*",
    "servers/*"
  ],
  "devDependencies": {
    "bun-types": "latest",
    "eslint": "^8.49.0",
    "pino-pretty": "^10.2.0",
    "prettier": "^3.0.3",
    "prettier-eslint": "^15.0.1",
    "redis-commander": "^0.8.0",
    "turbo": "^1.10.14",
    "typescript": "^5.2.2"
  },
  "type": "module"
}
// World types

{
  "name": "world-types",
  "type": "module",
  "exports": {
    "./websockets": "./websockets/index.ts",
    "./game": "./game/entities/index.ts"
  },
  "devDependencies": {
    "bun-types": "latest"
  },
  "peerDependencies": {
    "typescript": "^5.0.0"
  }
}

我很困惑为什么这不起作用,任何提示都非常感谢。

zd287kbt

zd287kbt1#

能够通过添加修复:

// next config
{
   ...,
   transpilePackages: ['world-types']
}

到我的下一个配置

相关问题