npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@cuimingda/hello-npm

v2.0.4

Published

创建组件目录 ``` mkdir -p ~/packages/hello-npm cd ~/packages/hello-npm ```

Downloads

4

Readme

如何从零创建一个Vue组件并且部署到npm上?

How to create a component of Vue.js and publish to npm.com from scatch?

创建组件目录

mkdir -p ~/packages/hello-npm
cd ~/packages/hello-npm

初始化组件

npm init --scope=@cuimingda --yes

初始化git,根据需要提交代码

git init
git remote add origin [email protected]:cuimingda/hello-npm.git
git add .
git commit -m 'init npm'
git push -u origin master

创建示例组件

mkdir -p src
vi src/HelloNpm.vue

我们在示例组件src/HelloNpm.vue中包含最简单的template、script和style

<template>
  <h1 class="demo-title">{{ title }}</h1>
</template>

<script>
export default {
  data() {
    return {
      title: 'Hello npm!'
    }
  }
}
</script>

<style>
.demo-title {
  color: red;
}
</style>

这时候其实就可以测试这个页面了

vue serve --open src/HelloNpm.vue

创建vue组件的包裹脚本src/index.js,这里没有使用install的方式,只是简单封装

import HelloNpm from './HelloNpm.vue'
export default HelloNpm

接下来创建Webpack的配置文件webpack.config.js

const path = require('path')
const VueLoaderPlugin = require('vue-loader/lib/plugin')

module.exports = {

    entry: path.resolve(__dirname + '/src/index.js'),

    output: {
        path: path.resolve(__dirname + '/dist'),
        filename: 'index.js',
        libraryTarget: 'commonjs2',
    },

    plugins: [
        new VueLoaderPlugin(),
    ],

    module: {
        rules: [
            {
                test: /\.vue$/,
                loader: 'vue-loader',
            },
            {
                test: /\.css$/,
                use: [
                    'style-loader',
                    'css-loader',
                ]
            },
        ]
    }
}

修改package.json,增加一条脚本,顺便修改入口文件地址

"main": "dist/index.js",
"scripts": {
  "build": "webpack --mode production"
},

这时候先安装一下Webpack需要的组件,注意只要安装成开发依赖就可以:

yarn add webpack webpack-cli vue-loader vue-template-compiler css-loader style-loader --dev

这时候就可以直接用Webpack编译组件了,编译完成后,会发现dist目录多出了一个index.js文件。

yarn build

这时候其实我们就可以在本地引用这个组件了,只是不使用组件名称,而是使用本地路径

yarn add ~/packages/hello-npm

调用方式跟真实的组件是一样的:

<template>
    <div class="container">
        <hello-npm></hello-npm>
    </div>
</template>

<script>
import HelloNpm from '@cuimingda/hello-npm'
export default {
    components: {
        "hello-npm": HelloNpm,
    }
}
</script>

本地测试如果没有问题,就可以准备发布了,先登陆,会提示输入用户名、密码和邮箱,如果没有npm账号,甚至可以用adduser在命令行注册一个

npm login

如果不确定是否登陆过,可以用whoami命令验证,登陆了也可以用logout注销登录

npm whoami
npm logout

登陆以后就可以发布了,记得在发布之前要build

yarn build
npm publish --access public

如果之后更新了组件,首先是在build以后在本地测试,然后提交git,然后更新npm版本,注意npm version patch会自动在第三个版本上加一,最后发布。

yarn build
git add .
git commit -m 'new patch'
npm version patch
npm publish
git push -u origin master