我在使用js interop时遇到了麻烦。我尝试使用js组件react-slick,如下所示:
// src/interop/Carousel.js
import React from 'react'
import Slider from 'react-slick'
export function Carousel(props) {
return (
<Slider>
<div>
<h3>{props.name} 1</h3>
</div>
</Slider>
)
}
/* src/Carousel.re */
[@bs.module "./interop/Carousel"] [@react.component]
external make: (~name: string) => React.element = "";
/* src/index.re */
ReactDOMRe.renderToElementWithId(<Carousel name="ahaha" />, "carousel");但在《webpack》中却遇到了这样的错误:
ERROR in ./lib/js/src/Index.bs.js
Module not found: Error: Can't resolve './interop/Carousel' in '[...]/reason_react_example/lib/js/src'
@ ./lib/js/src/Index.bs.js 6:15-44所以看起来BS在编译时不考虑Carousel.js文件?
顺便说一句,我正在关注这个reason-react doc
发布于 2019-04-14 21:35:09
默认情况下,BuckleScript会将生成的js工件放在lib/js/...中,因此您必须编写与之相关的导入,或者配置bsb将工件放在源文件旁边。后一种方法是在bsconfig.json中为给定的package-spec设置"in-source": true。例如:
{
"package-specs": {
"module": "commonjs",
"in-source": true
}
}发布于 2019-04-14 23:09:09
经过一些调整后,以下是对我有效的方法:
// lib/js/src/interop/Carousel.js
import React from 'react'
import Slider from 'react-slick'
const Carousel = props => {
return (
<Slider>
<div>
<h3>{props.name} 1</h3>
</div>
</Slider>
)
}
export default Carousel
// src/Carousel.re
[@bs.module "./interop/Carousel"] [@react.component]
external make: (~name: string) => React.element = "default"; // handle default export
// src/Index.re
ReactDOMRe.renderToElementWithId(<Carousel name="it works!" />, "carousel");因为Carousel.js使用的是es6和jsx,所以我需要设置webpack来使用它(es6,jsx)。并且bsconfig.json需要有以下设置:
"reason": {
"react-jsx": 3
},
"package-specs": [
{
"module": "commonjs",
"in-source": false
}
]https://stackoverflow.com/questions/55673630
复制相似问题