用
ES6
編寫代碼,使用Webpack
打包導出模塊,併發布到NPM社區,添加基於Travis-CI
和Coveralls
的持續集成到Github項目中javascript
$ git clone https://github.com/eteplus/typeof
複製代碼
root = true
[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.yml]
indent_style = space
indent_size = 2
複製代碼
+-- src 源碼目錄
| +-- typeof.js
| +-- types.js
+-- test 測試用例
| +--- test.js
+-- dist 輸出目錄
.
.
複製代碼
使用NPM做用域管理髮布模塊,避免同名模塊,通常使用用戶名來註冊私有模塊(
@eteplus/<package name>
)html
$ npm init --scope=eteplus
複製代碼
package name: (@eteplus/typeof)
version: (1.0.0) 0.1.0
description: The typeOf method returns a string indicating the type of the value
entry point: (index.js) dist/typeof.js
test command:
git repository: https://github.com/eteplus/typeof.git
keywords: node,javascript,typeof
author: eteplus
license: (ISC) MIT
About to write to /Users/eteplus/Workspace/Node/study/typeof/package.json:
{
"name": "@eteplus/typeof",
"version": "0.1.0",
"description": "The typeOf method returns a string indicating the type of the value",
"main": "dist/typeof.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/eteplus/typeof.git"
},
"keywords": [
"node",
"javascript",
"typeof"
],
"author": "eteplus",
"license": "MIT",
"bugs": {
"url": "https://github.com/eteplus/typeof/issues"
},
"homepage": "https://github.com/eteplus/typeof#readme"
}
複製代碼
自動生成
.eslintrc.js
文件並安裝相關依賴(可根據本身喜愛選擇代碼規範)java
# or use yarn
$ npm install eslint -D
複製代碼
$ npm install eslint -g # 全局安裝ESLint
$ eslint --init
? How would you like to configure ESLint? Use a popular style guide
? Which style guide do you want to follow? Standard
? What format do you want your config file to be in? JavaScript
複製代碼
.eslintignore
文件忽略對輸出目錄的代碼檢測dist/
複製代碼
$ npm install webpack webpack-cli uglifyjs-webpack-plugin -D
$ npm install babel-loader babel-core babel-plugin-transform-runtime babel-preset-env -D
$ npm install eslint-loader eslint-friendly-formatter -D
複製代碼
webpack output 配置項說明 webpack.js.org/configurati…node
'use strict'
const path = require('path')
const webpack = require('webpack')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
const resolve = dir => path.join(__dirname, '.', dir)
const isProd = process.env.NODE_ENV === 'production'
module.exports = {
entry: {
typeof: './src/typeof.js'
},
output: {
path: resolve('dist'), // 輸出目錄
filename: '[name].js', // 輸出文件
libraryTarget: 'umd', // 採用通用模塊定義
library: 'typeOf', // 庫名稱
libraryExport: 'default', // 兼容 ES6(ES2015) 的模塊系統、CommonJS 和 AMD 模塊規範
globalObject: 'this' // 兼容node和瀏覽器運行,避免window is not undefined狀況
},
devtool: '#source-map',
module: {
rules: [
{
test: /\.(js)$/,
loader: 'eslint-loader',
enforce: 'pre',
include: [resolve('src'), resolve('test')],
options: {
formatter: require('eslint-friendly-formatter')
}
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /(node_modules)/
}
]
},
plugins: isProd
? [
new UglifyJsPlugin({
parallel: true,
uglifyOptions: {
compress: {
warnings: false
},
mangle: true
},
sourceMap: true
})
]
: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin(),
new webpack.NoEmitOnErrorsPlugin()
]
}
複製代碼
heplers和polyfill根據實際狀況開啓/關閉,具體配置參考(babel-plugin-transform-runtime)webpack
{
"presets": ["env"],
"plugins": [
["transform-runtime", {
"helpers": false,
"polyfill": false
}
]
]
}
複製代碼
在package.json上添加npm命令,經過npm run [name] 執行任務git
{
"scripts": {
"dev": "NODE_ENV=development webpack --config webpack.config.js --mode=development -w",
"build": "NODE_ENV=production webpack --config webpack.config.js --mode=production",
"lint": "eslint ."
}
}
複製代碼
$ npm install ava nyc -D
複製代碼
AVA:web
其餘配置項:npm
nyc:json
{
"ava": {
"files": [
"test/test.js"
],
"verbose": true,
"babel": "inherit",
"require": [
"babel-core/register"
]
},
"nyc": {
"exclude": [
"test/*.js"
]
}
}
複製代碼
{
"scripts": {
...
"test": "npm run lint && nyc ava",
"test:watch": "ava --watch"
}
}
複製代碼
travis-ci.org,使用Github受權登錄
.travis.yml
在項目根目錄下添加
.travis.yml
說明:
其它配置項:
language: node_js
node_js:
- '9'
- '8'
- '7'
複製代碼
.travis.yml
提交
.travis.yml
到Github, Travis-CI根據提交自動觸發持續集成,可在設置中取消根據提交自動觸發持續集成
獲取持續集成結果徽章添加到
README.md
中
coveralls.io,使用Github受權登錄
安全起見,repo_token不該該明文寫到
.travis.yml
中,coveralls提供了非對稱加密repo_token的方法
# 更改成國內的安裝源
$ gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/
$ gem sources -l
複製代碼
# 安裝travis
$ sudo gem install travis -v 1.8.8 --no-rdoc --no-ri
$ travis version
1.8.8
複製代碼
# 加密
travis encrypt COVERALLS_REPO_TOKEN=your_repo_token
複製代碼
.travis.yml
修改
.travis.yml
,設置env環境變量(travis提供的repo_token安全方式之一),也可使用 --add 自動添加到.travis.yml
中。
$ npm install coveralls -D
複製代碼
nyc生成覆蓋率報告推送給coveralls
{
"scripts": {
...
"coverage": "nyc report --reporter=text-lcov | coveralls"
}
}
複製代碼
.travis.yml
after_success: npm run coverage
after_success: script階段成功時執行, script階段默認腳本爲
npm test
,能夠省略
$ npm run build
複製代碼
須要先到www.npmjs.com註冊一個帳號
$ npm adduser
Username: your username
Password: your password
Email: your email
複製代碼
無做用域包始終是公開的。有做用域默認爲受限制的,可使用
–-access public
發佈的時候設置權限爲公開
$ npm publish --access public
複製代碼
npm社區版本號規則採用的是semver(語義化版本),主要規則以下:
版本格式:主版號.次版號.修訂號
版號遞增規則以下:
推薦 shields.io/
爲項目添加上各類各樣的徽章,例如:
該項目包含教程全部的完整配置
Github地址: github.com/eteplus/typ…