使用紗線工作區導出枚舉時出現意外的令牌錯誤 (Unexpected token error exporting enums using yarn workspaces)


問題描述

使用紗線工作區導出枚舉時出現意外的令牌錯誤 (Unexpected token error exporting enums using yarn workspaces)

我有一個打字稿項目,結構如下:

root/
  packges/
    client/ <‑‑ Built with CRA
    server/
    domain/
      src/models
      package.json
      tsconfig.json
      webpack.config.js

導入模型,例如 @project/domain/src/models/some‑model.ts在客戶端項目內部似乎工作正常(或者至少 linter 沒有抱怨它)。但是,當我運行應用程序時,出現此錯誤:

../domain/src/models/some‑model.ts 12:7
[1] Module parse failed: Unexpected token (12:7)
[1] File was processed with these loaders:
[1]  * ../../node_modules/@pmmmwh/react‑refresh‑webpack‑plugin/loader/index.js
[1] You may need an additional loader to handle the result of these loaders.
[1] > export enum SomeEnum {
[1] |   VALUE1,
[1] |   VALUE2

我猜這是因為域“應用程序”本身沒有運行,只是被引用,所以它的 webpack 配置被忽略. 但如果是這種情況,我怎樣才能讓它正常工作?

webpack.config.js

module.exports = {
  module: {
    rules: [{ test: /\.ts$/, use: 'ts‑loader', exclude: '/node_modules/' }]
  }
}

tsconfig.json

{
  "compilerOptions": {
    "target": "es6",
    "module": "esnext",
    ...
  },
  "include": ["src"]

其他一些相關問題:

  1. 我不打算將共享代碼放在 domain 中的模型之外,那麼它是否有必要擁有一個 src 文件夾?我能否以某種方式盡可能地獲得它,或者它是否必須是一個完整的可運行子項目?
  2. 在使用紗線工作區時是否有必要在 package.json 中使用節點引用?


參考解法

方法 1:

After deciding not to program at 5am, I used a different search term and was finally able to google the answer. Basically, this error is due to CRA not allowing code outside the src directory. So the fix is either ejecting, using react‑app‑rewired, or using craco which is what I went with. To answer my own misc questions:

  1. I moved out the models folder into the root of the domain directory/package. I don't know if this is good practice per say but looking at the import path without src makes me feel better.
  2. Nope

(by idlackageidlackage)

參考文件

  1. Unexpected token error exporting enums using yarn workspaces (CC BY‑SA 2.5/3.0/4.0)

#yarn-workspaces #TypeScript #yarnpkg






相關問題

如何使用帶有打字稿和輸出文件夾的紗線工作區? (How to use yarn workspaces with typescript and out folders?)

如何在 Monorepo 中運行多個開發包 (How to run multiple packages in development inside a Monorepo)

帶有許多 npm 腳本的 Monorepo (Monorepo with Many npm-scripts)

使用紗線工作區導出枚舉時出現意外的令牌錯誤 (Unexpected token error exporting enums using yarn workspaces)

如何在 Expo 應用示例嵌套文件夾中熱重載開發包? (How to hot reload a development package in an Expo app example nested folder?)

將 monorepo 包放在包文件夾下是一種約定,還是 yarn 工作區期望這樣? (Is putting monorepo packages under a package folder a convention, or do yarn workspace expect that?)

通過創建具有循環依賴關係的 monorepo 導致錯誤 (Causing an error by creating a monorepo with circular dependencies)

Monorepo – Yarn 工作區 Typescript Node.JS 項目 – 運行 nodemon 時找不到模塊 (Monorepo – Yarn workspaces Typescript Node.JS project – cannot find module when running nodemon)

紗線工作區為每個運行 shell 命令 (Yarn workspaces run shell command for each)

紗線工作區共享包未在 create-react-app 中加載 (yarn workspaces shared package does not load in create-react-app)

如何使用 Yarn 工作區將共享依賴項添加到 monorepo? (How do I add shared dependencies to a monorepo using Yarn workspaces?)

使用紗線工作區在monorepo中跨項目共享配置變量? (Sharing config variables across projects in a monorepo using yarn workspaces?)







留言討論