【发布时间】:2020-07-16 13:31:55
【问题描述】:
我尝试用 2 个 React 包创建一个 monorepo:
- TypeScript (npx create-react-app
app--template typescript) - JavaScript (
ui)
我有一个基本的lerna.json 配置
{
"packages": ["packages/*"],
"version": "1.0.0"
}
在ui 包中,我只需导出一个按钮(来自src/Button.jsx):
import React from 'react';
const Button = () => {
return (
<button>
Start
</button>
)
}
export default Button;
我已经引导 app 使用 ui 包。
在app 中导入它会导致以下错误:
Failed to compile
/lerna-demo/packages/ui/src/Button.jsx 5:4
Module parse failed: Unexpected token (5:4)
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
| const Button = () => {
| return (
> <button>
| Start
| </button>
有没有办法将加载程序添加到 lerna 或 app 以修复导入?
编辑
项目结构:
lerna-demo/
- node_modules/
- lerna.json
- package.json
- packages/
- app (created using create-react-app)
- ...
- ui
- node_modules/
- package.json
- yarn.lock
- src/
- Button.jsx
我导入Button组件的方式:
import React from 'react';
import logo from './logo.svg';
import './App.css';
import * as Button from 'ui/src/Button';
const App: React.FC = () => {
return (
<div className="App">
<Button />
</div>
);
}
export default App;
【问题讨论】:
-
编写更多代码 - 导入和错误
-
@Adidi 我已经更新了问题
-
@Bartek 我会看看这个:stackoverflow.com/questions/50179670/using-react-with-lerna
-
@NelsonYeung 成功了 :) 我还必须将
"noImplicitAny": false添加到 tsconfig.json
标签: javascript reactjs typescript lerna