編輯:不幸的是,這似乎是一個已知問題,如果不弄亂create-react-app就無法解決,盡管我可能是錯的:( https://github.com/facebook/create-react-app/issues/3547#issuecomment -593764097
我正在使用 typescript 和 firebase 函式開發一個 react 專案,我的 firebase 函式專案檔案夾位于 react 應用程式的專案檔案夾中。由于我希望在前端(react 應用程式)和后端(firebase 函式)之間保持許多列舉和介面的一致性,因此我使用符號鏈接來共享包含這兩個專案之間通用檔案的檔案夾。這適用于介面,但當我嘗試使用從此符號鏈接檔案夾匯出的列舉時會導致錯誤:
ERROR in ./functions/src/shared-types/roles.ts 3:0
Module parse failed: The keyword 'enum' is reserved (3:0)
File was processed with these loaders:
* ./node_modules/@pmmmwh/react-refresh-webpack-plugin/loader/index.js
* ./node_modules/source-map-loader/dist/cjs.js
You may need an additional loader to handle the result of these loaders.
| __webpack_require__.$Refresh$.runtime = require('/Users/joel/my-project/node_modules/react-refresh/runtime.js');
|
> enum Roles {
| Admin = 'Admin',
| Access = 'Access',
復制品
從一個支持 typescript 的全新 create-react-app 開始,添加一個名為的檔案夾shared-types,src并在其中放置一個名為MyEnum.ts:
// shared-types/MyEnum.ts
enum MyEnum {
Foo = "foo",
Bar = "bar"
}
export default MyEnum;
然后,在該檔案夾和另一個也稱為shared-typesinside的檔案夾之間建立符號鏈接src:
$ ln -s /path/to/project/shared-types /path/to/project/src/shared-types
然后,匯入并使用MyEnumin App.tsx:
// src/App.tsx
import React from 'react';
import './App.css';
import MyEnum from "./shared-types/MyEnum";
function App() {
return (
<div className="App">
<h1>{MyEnum.Bar}</h1>
</div>
);
}
export default App;
最后,只需運行npm start:
ERROR in ./shared-types/MyEnum.ts 3:0
Module parse failed: The keyword 'enum' is reserved (3:0)
File was processed with these loaders:
* ./node_modules/@pmmmwh/react-refresh-webpack-plugin/loader/index.js
* ./node_modules/source-map-loader/dist/cjs.js
You may need an additional loader to handle the result of these loaders.
| __webpack_require__.$Refresh$.runtime = require('/Users/joel/repro/node_modules/react-refresh/runtime.js');
|
> enum MyEnum {
| Foo = 'foo',
| Bar = 'bar',
不是導致它的事情
- 并不是 typescript 忽略了該
shared-types檔案夾,因為如果您向該檔案夾添加一個介面并在App.tsx. 另外,運行tsc --listFilesOnly將回傳一個串列,包括/path/to/project/src/shared-types/MyEnum.tsc. - 并不是說我的 typescript 版本不支持列舉或列舉被禁用,因為如果您向
App.tsx自身添加列舉,一切都會正常作業。
在此先感謝您的幫助!如果有的話,請隨時在評論中建議在這兩個專案之間共享檔案的更好方法!
uj5u.com熱心網友回復:
事實證明 create-react-app 根本不支持 src 下的符號鏈接!界面的東西完全起作用的事實似乎是僥幸。
https://github.com/facebook/create-react-app/issues/3547
這似乎是一個已知問題已有四年了,但還沒有一個幸運的解決方案。??
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/488871.html
標籤:javascript 反应 打字稿 网页包 创建反应应用
下一篇:將NextJS專案匯出為模塊
