备注:
使用webpack 进行模块导出,方便进行通信
1. 项目初始化
├── main.js
├── package.json
├── show.js
├── webpack.config.js
└── yarn.lock
2. 代码说明
main.js
const shortid = require("shortid");
const demo = require("./show.js");
module.exports = {
shortid:function(){
return shortid.generate();
},
printinfo:function(){
demo();
}
};
show.js
function demo(content){
window.document.getElementById("app").innerText="demo"+content;
}
module.exports=demo;
package.json
{
"dependencies": {
"shortid": "^2.2.8"
},
"scripts": {
"build": "webpack"
},
"name": "@cnpm/dalong-demo",
"version": "1.0.2",
"main": "bundle.js",
"license": "MIT"
}
webpack.config.js
const path = require("path");
module.exports = {
entry:"./main.js",
output:{
filename:"bundle.js",
path:path.resolve(__dirname,"./"),
library:"dalongrong",
libraryTarget:"commonjs2"
}
}
3. 构建(同时发布私服)
yarn && yarn run build && npm publish
4. 使用
yarn add @cnpm/dalong-demo
app.js
const myfirst = require("@cnpm/dalong-demo");
console.log(myfirst.shortid())
备注:
实际上也普通的npm 模块开发是一样的,只不过使用使用了webpack 同时将对应的依赖打包在一起了