您的位置:首页 > Web前端 > Webpack

webpack libray 参考例子

2017-12-29 10:17 183 查看
备注:
使用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 同时将对应的依赖打包在一起了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: