概述
webpack打包library发布到npm
项目结构
$ tree
.
├── package.json
├── src
│ └── index.js
└── webpack.config.js
入口文件 index.js
// src/index.js
export function echo(message) {
console.log(message);
}
依赖配置 package.json
{
"name": "more-echo",
"version": "1.0.0",
"description": "a demo for webpack package to npm",
"main": "dist/more-echo.js",
"scripts": {
"build": "webpack"
},
"keywords": [
"demo"
],
"author": "",
"license": "ISC",
"devDependencies": {
"webpack": "^5.72.1",
"webpack-cli": "^4.9.2"
}
}
配置文件 webpack.config.js
const path = require('path');
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'more-echo.js',
library: {
name: 'moreEcho',
type: 'umd',
},
},
};
参考 https://webpack.docschina.org/guides/author-libraries/
打包发布
# 打包
$ npm run build
# 发布
$ npm publish
npm地址:https://www.npmjs.com/package/more-echo
Node环境中使用
npm i more-echo
示例
const { echo } = require('more-echo');
echo('Hello World!');
浏览器中使用
<script src="./dist/more-echo.js"></script>
<script>
moreEcho.echo('Hello World');
</script>
最后
以上就是英俊鼠标为你收集整理的webpack打包library发布到npmwebpack打包library发布到npm的全部内容,希望文章能够帮你解决webpack打包library发布到npmwebpack打包library发布到npm所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复