对于typescript的项目,使用webpack publish为component的关键点在于:
tsconfig
中module
选commonjs
,但webpack config中output.libraryTarget
选umd
- 不要把react打包,在webpack config中设置
externals: ['react']
webpack在这里做得非常不好,上面两个选项猜了一天才猜出来。以后发布package还是首选rollup。
其它需要注意的:
- tsconfig中设置
declaration=true
等 - package.json中设置正确的入口文件和types入口
本demo中的my-component为一个独立的项目,在demo根目录下不直接引用,而是通过npm publish
发布出去以后,
在根目录下以package的形式引用。
npm install
npm run dist
npm publish
npm install
npm run demo
It will open page on browser automatically.