如何打包vue並在npm上發佈套件

上次在npm發布了自己寫的 vue-scheduler-lite 套件後,忘記記錄一下怎麼將自己寫好的東西打包並發布在npm上。所以補一下內容。

初始化 npm 專案(用vue-cli建立專案的話這部分會不一樣)

mkdir vue-scheduler-lite
cd vue-scheduler-lite
npm init

package name: (vue-scheduler-lite) 
version: (1.0.0) 
description: A very simple scheduler for vue.js
entry point: (index.js) dist/vue-scheduler-lite.js
test command: 
git repository: https://github.com/linmasahiro/vue-scheduler-lite
keywords: scheduler
author: Lin Masahiro <k80092@hotmail.com>
license: (ISC) MIT

修改 package.json 的相關依賴(如果是直接用vue-cli建立起專案的話,這部分可跳過)

{
    "name": "vue-scheduler-lite",
    "description": "A very simple scheduler for vue.js",
    "version": "1.0.0",
    "author": "Lin Masahiro <k80092@hotmail.com>",
    "license": "MIT",
    "main": "dist/vue-scheduler-lite.js",
    "jsnext:main": "dist/vue-scheduler-lite.js",
    "private": false,
    "repository": {
        "type": "git",
        "url": "https://github.com/linmasahiro/vue-scheduler-lite"
    },
    "bugs": {
        "url": "https://github.com/linmasahiro/vue-scheduler-lite/issues"
    },
    "homepage": "https://github.com/linmasahiro/vue-scheduler-lite",
    "keywords": [
        "vue",
        "scheduler",
        "schedule",
        "lite",
        "calendar",
        "timeline",
        "drag",
        "drop",
        "mobile"
    ],
    "scripts": {
        "dev": "cross-env NODE_ENV=development webpack-dev-server --open --hot",
        "build": "cross-env NODE_ENV=production webpack --progress --hide-modules"
    },
    "dependencies": {
        "vue": "^2.5.11"
    },
    "browserslist": [
        "> 1%",
        "last 2 versions",
        "not ie <= 8"
    ],
    "devDependencies": {
        "babel-core": "^6.26.0",
        "babel-loader": "^7.1.2",
        "babel-preset-env": "^1.6.0",
        "babel-preset-stage-3": "^6.24.1",
        "cross-env": "^5.0.5",
        "css-loader": "^0.28.7",
        "file-loader": "^1.1.4",
        "vue-loader": "^13.0.5",
        "vue-template-compiler": "^2.4.4",
        "webpack": "^3.6.0",
        "webpack-dev-server": "^2.9.1"
    }
}

安裝相關依賴

npm install

建立目錄(dist是實際發布時的目錄,src是開發用的目錄。)

mkdir dist
mkdir src

把自己寫的代碼移到src底下,這時候目錄結構會是

node_modules/
dist/
src/
    components/
        schedulerLite.vue
    main.js
    vue-scheduler-lite.js
    App.vue
package.json

建立打包用的 webpack.config.js

var path = require('path')
var webpack = require('webpack')

module.exports = {
entry: './src/vue-scheduler-lite.js', // 自己專案的起始檔案
output: {
    path: path.resolve(__dirname, './dist'), // 打包後輸出路徑
    publicPath: '/dist/',
    filename: 'vue-scheduler-lite.js', // 輸出後的檔案名稱
    libraryTarget: 'umd',
    umdNamedDefine: true
},
module: {
    rules: [
    {
        test: /\.css$/,
        use: [
        'vue-style-loader',
        'css-loader'
        ],
    },      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
        loaders: {
        }
        // other vue-loader options go here
        }
    },
    {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/
    },
    {
        test: /\.(png|jpg|gif|svg)$/,
        loader: 'file-loader',
        options: {
        name: '[name].[ext]?[hash]'
        }
    }
    ]
},
resolve: {
    alias: {
    'vue$': 'vue/dist/vue.esm.js'
    },
    extensions: ['*', '.js', '.vue', '.json']
},
devServer: {
    historyApiFallback: true,
    noInfo: true,
    overlay: true
},
performance: {
    hints: false
},
devtool: '#eval-source-map'
}

if (process.env.NODE_ENV === 'production') {
module.exports.devtool = '#source-map'
// http://vue-loader.vuejs.org/en/workflow/production.html
module.exports.plugins = (module.exports.plugins || []).concat([
    new webpack.DefinePlugin({
    'process.env': {
        NODE_ENV: '"production"'
    }
    }),
    new webpack.optimize.UglifyJsPlugin({
    sourceMap: true,
    compress: {
        warnings: false
    }
    }),
    new webpack.LoaderOptionsPlugin({
    minimize: true
    })
])
}

進行打包

npm run build

不必要的檔案不需要上傳,所以建立一個.gitgnore

vi .gitgnore

.DS_Store
node_modules/
npm-debug.log
yarn-error.log

# Editor directories and files
.idea
*.suo
*.ntvs*
*.njsproj
*.sln

寫一個 README.md

這個如果不寫的話,發布上去後套件的首頁會是一片空白…也不會有人知道這個套件在做什麼。建議還是寫一下:P

發布

npm adduser // 新增使用者
npm login // 登入
npm whoami // 確認身份
npm publish // 發布

參考:

我的第一個 npm 套件:把 vue component 打包到 npm 吧