NPM As Build Tool
Development progress
compile/transform -> bundle -> optimize -> test -> deploy
Why use npm as build tool
Run Test
$ npm run-script test
$ npm run test
$ npm test
$ npm tst
$ npm t
Run npm script in silent mode
$ npm test -s
Run script pre- and post-
$ npm run pretest
$ npm test -s
customize hooks
$ npm run greet -s
Run npm script sequence as following:
prescriptname -> scriptname -> postscriptname
Run many script
"scripts": {
"compile": "npm run compile:coffee && npm run compile:es6"
}
$ npm run compile
If the first script run failed, and then left script will not run.
clean files or directories
For windows platform, we could use rimraf package
$ npm install rimraf --save-dev
NPM operators
- &&
If the left operation failed, the right operation will not run
- |
The left operation result will be the right command first parameter
-
-
--
It could allow to pass argument throught underline commander
The left operation result will be writen to right destination file.
NPM Commander
- npm --help
To see all of avaiable npm commanders
- npm run
To check which script command you could use
pipeline
Before script
"build:bundle": "browserify ./src/app.js -o ./public/js/bundle.js",
"build:uglify": "uglifyjs ./src/app.js -mc -o ./public/js/bundle.min.js"
After pipeline
"build:bundle": "browserify ./src/app.js | uglifyjs -mc > ./public/js/bundle.js"
The | left commander output as second command first parameter
build script procedure
"build:clean": "rimraf public/css/* public/js/*",
"prebuild": "npm run build:clean",
"build": "npm run compile:less && npm run build:bundle"
-
run "prebuild": "npm run build:clean"
-
run "rimraf public/css/* public/js/*"
-
run "npm run compile:less" and related hooks script
-
run npm run build:bundle and related hooks script
watch
-
client watch(watch, watchify)
-
server watch(nodemon)
Version migration
The common version follows semver.org
Major.Minor.Patch
But we could remember as following:
Breaking.Feature.Bugfix
How to migration version?, firstly, you could check help info
$ npm version --help
Then add script task.
$ npm run version:patch
This will change package.json version value.
Git tag
Check current version or check differencs
$ git remote -v
$ git tag
$ git show v1.0.1
Deploy to cloud
- heroku
- aws
- azure