添加两种规范以及 ts
的入口文件
"main": "dist/lego-component.umd.js",
"module": "dist/lego-component.esm.js",
"types": "dist/index.d.ts",
先进入需要被本地 link
的目录中,执行
npm link
然后进入到使用这个库的目录中,执行
npm link lego-component
引入本地组件库
import LegoComponents from 'lego-Component';
app.use(LegoComponents);
结果发现这一行 Ts
报错了,原因是 vue
的版本不一致,真实的场景下不会出现这个问题,因为真实情况下组件库用的是上级的依赖。
解决这个问题,可以把组件库的 vue
版本 link
成为别的项目的版本。执行
npm link ../lego-fe/node_modules/vue
这样两个项目就使用了同样的版本,错误也就消失了。
Rollup
插件的作用就是将各种各样的文件转化成 Rollup
认识的 esm
的代码。
import { dataToEsm } from '@rollup/pluginutils'
// 插件就是一个返回特定对象的函数
function exPlugin() {
return {
name: "my-plugin",
buildStart(options) {
console.log('🚀🚀 buildStart ~ options', options);
},
load(id) {
console.log('🚀🚀 load ~ id', id);
return null
},
transform(code, id) {
console.log('🚀🚀 transform ~ id', id);
console.log('🚀🚀 transform ~ code', code);
if (id.slice(-5) !== '.json') {
return null
}
try {
const parsed = JSON.parse(code)
const afterCode = dataToEsm(parsed)
console.log('🚀🚀 transform ~ afterCode', afterCode);
return {
code: afterCode
}
} catch (error) {
console.log('🚀🚀 ~ error', error);
}
},
buildEnd(error) {
console.log('🚀🚀 buildEnd ~ error', error);
}
}
}
export default {
input: "package.json",
output: {
name,
file: file('esm'),
format: "es"
},
plugins: [
exPlugin()
],
}
npm
下载别人编写的第三方包到本地,比如 vue
npm
下载并安装别人编写的命令行工具到本地,比如 vue-cli
npm
供别人使用// 查看登录状态 没有登录就会提示错误 登录了就会显示用户名
npm whoami
// 查看一些配置信息,尤其是 registry ,仓库的源
npm config ls
// 登录 npm 账号 ,除了用户名,密码,邮箱外 还需要填写一个验证码
npm login
// 发布包到 npm ,包名不能重复,必须是 npm 官方源
npm bublish
npm publish
的时候需要上传的内容.gitignore
中的文件,上传其余文件package.json/README.md/CHANGELOG.md/LICENSE
都会被包含在其中使用 prepublishOnly
钩子,让组件库在发布之前进行一次打包,就不用我们手动执行 npm run build
了。
"scripts": {
"prepublishOnly": "npm run build"
},
这样就是发布完成了。
添加单元测试工具
vue add unit-jest
LText.spec.ts
import { shallowMount } from '@vue/test-utils';
import LText from '@/components/LText';
import { textDefaultProps } from '../../src/defaultProps';
describe('LText.vue', () => {
it.only('default LText render', async () => {
// 传入组件的属性
const msg = 'test';
const props = {
...textDefaultProps,
text: msg,
};
// 获取组件
const wrapper = shallowMount(LText, { props });
// 文本正确
expect(wrapper.text()).toBe(msg);
// 标签正确
expect(wrapper.element.tagName).toBe('DIV');
const style = wrapper.attributes().style;
// 包含指定类名
expect(style.includes('font-size')).toBeTruthy();
// 不包含指定类名
expect(style.includes('actionType')).toBeFalsy();
});
});
"scripts": {
"serve": "vue-cli-service serve",
"clean": "rimraf ./dist",
"build": "npm run clean && npm run build:esm && npm run build:umd",
"build:esm": "rollup --config rollup.esm.config.js",
"build:umd": "rollup --config rollup.umd.config.js",
"test:watch": "vue-cli-service test:unit --watch",
"test": "vue-cli-service test:unit",
"lint": "vue-cli-service lint --max-warnings 5",
"prepublishOnly": "npm run lint && npm run test && npm run build"
},