请参阅示例回购 https://github.com/inspiraller/webpack-and-microbundle
微束码
mymicrobundle/src/index.js
import React from 'react'
const MyMicroBundle = ({session}) => {
return (
<div>Session = {session}</div>
)
}
export default MyMicroBundle
microbundle/package.json - 用于输出
{
...
"source": "src/index.js",
"main": "dist/index.umd.js",
"module": "dist/index.module.js",
"exports": "dist/index.modern.js",
"unpkg": "dist/index.umd.js"
}
导入微束代码
webpack-loads-microbundle/package.json
{
"dependencies": {
"@mymicrobundle/example": "file:../mymicrobundle",
}
}
webpack-load-microbundle/src/index.tsx
import React, { useState } from 'react'
import ReactDOM from 'react-dom'
import MyMicroBundle from '@mymicrobundle/example'
import './index.scss'
const App = () => {
const [session, setSession] = useState('')
return (
<div>
<h1>Hello</h1>
</div>
)
}
ReactDOM.render(<App />, document.getElementById('root'))
注意:microbundle 包被捆绑为 javascript,但我使用 typescript 来导入它。虽然不应该有任何区别。我也在使用 pnpm 而不是 npm,但这应该没问题,因为所有其他导入都有效。
我的路径可能有问题。
错误
未找到模块:错误:无法解析“C:\baps\react_all\webpack-and-microbundle\webpack-loads-microbundle\src”中的“@mymicrobundle/example”