Slide 1

Slide 1 text

by Jennifer Bland NPM as a Build Tool

Slide 2

Slide 2 text

2 Who  am  I? h)p://www.jenniferbland.com   h)ps://github.com/ratracegrad/   [email protected]

Slide 3

Slide 3 text

3 What  is  a  Build  Tool? Pipeline  converts  applicaBon  to  deployable  unit   Build  Tool  handles   compile   test   minify   bundle   build     deployment

Slide 4

Slide 4 text

4 YEOMAN BOWER.IO GRUNT GULP

Slide 5

Slide 5 text

5 Why  use  NPM? Already  using  npm  in  your  applicaBon   Keep  all  build  scripts  within  your  package.json   No  addiBonal  configuraBon  needed   Don’t  have  to  rely  on  plugins

Slide 6

Slide 6 text

6 GeSng  Started npm  init

Slide 7

Slide 7 text

7 How  to  run  npm  Scripts npm  scripts  are  in  the  package.json   Execute  by  running  either   npm  run-­‐script  test     npm  run  test

Slide 8

Slide 8 text

8 Well  Known  Scripts npm  built  shortcuts  for  well-­‐known  Scripts   npm  run  test   npm  test   npm  tst   npm  t

Slide 9

Slide 9 text

9 Other  Well  Known  Commands npm  start   npm  stop   npm  restart

Slide 10

Slide 10 text

10 Pre  and  Post  Hooks A  hook  is  a  script  that  has  the  same  name  as  another   script  but  is  prefixed  with  either  pre  or  post.   “pretest”:  “Echo  “this  is  the  pretest  script\””,   “pos)est”:  “Echo  “this  is  the  pos)est  script\””   Use  the  -­‐s  flag  to  ‘silent’  the  script   1-­‐one

Slide 11

Slide 11 text

11 Order  of  Hooks  Irrelevant You  can  put  the  scripts  in  any  order  in  the   package.json  file.  Npm  will  run  them  in  the   correct  order.

Slide 12

Slide 12 text

12 Build  a  Test One  of  the  most  commonly  used  test  libraries  is  Mocha.   npm  install  mocha  should  superset  —save-­‐dev

Slide 13

Slide 13 text

13 Available  Commands ls  node_modules/.bin

Slide 14

Slide 14 text

14 Command  to  run  Mocha mocha      Default  it  looks  for  a  test  directory                                              Uses  the  reporter  ‘spec’   2-­‐two

Slide 15

Slide 15 text

15 LinBng  Code JSHINT  is  a  tool  that  helps  to  detect  errors  and   potenBal  problems  in  your  JavaScript  code.

Slide 16

Slide 16 text

16 Install  JSHINT npm  install  jshint  —save-­‐dev   jshint  *.js  **/*.js

Slide 17

Slide 17 text

17 Building  Lint  Script “lint”:  “jshint  *.js  **/*.js

Slide 18

Slide 18 text

18 Lint  as  pretest  Script Lint  is  a  script  that  you  should  run  frequently.  This     script  is  a  good  example  of  a  script  that  should  be  run   before  your  tests  in  a  pre-­‐hook  script.   “pretest”:  “npm  run  lint”   3-­‐three

Slide 19

Slide 19 text

19 Front  End  Scripts compile  less  code  to  css   bundle  and  minify  all  scripts   compile  coffeeScript  and  typeScript   From  my  example  I  will  be    using  Browserify  to  do   the  bundling  and  minify.

Slide 20

Slide 20 text

20 Install  Less npm  install  less  —save-­‐dev

Slide 21

Slide 21 text

21 Compiling  Less  to  CSS To  compile  less  code  you  tell  it  where  is  your  source   less  code  and  then  where  you  want  to  output  the  CSS   code  that  is  generated   lessc  client/less/demo.less  public/css/site.css

Slide 22

Slide 22 text

22 Create  less  Script “build:less”:  “lessc  client/less/demo.less                    public/css/site.css”

Slide 23

Slide 23 text

23 Browserify npm  install  browserify  —save-­‐dev  

Slide 24

Slide 24 text

24 Running  Browserify Browserify  is  similar  to  Less.  you  tell  it  where  is  your     sources  code  and  then  where  you  want  to  output     bundled  code  that  is  generated.   browersify  ./client/js/app.js  -­‐o  ./public/js/bundle.js 24

Slide 25

Slide 25 text

25 Create  Browserify  Script “build:bundle”:  “browersify  ./client/js/app.js                -­‐o  ./public/js/bundle.js”

Slide 26

Slide 26 text

26 Minify Minify  is  an  npm  module  that  will  minify  the  bundle   created  by  Browserify.   npm  install  uglify  —save-­‐dev

Slide 27

Slide 27 text

27 Minify  Script uglify  ./client/js/app.js  —mc  -­‐o  ./public/js/out.min.js   “build:uglify”:  “uglify  ./client/js/app.js  —mc              -­‐o  ./public/js/out.min.js”

Slide 28

Slide 28 text

28 Refactoring  Uglify We  don’t  want  to  send  the  app.js  file  into  uglify.  We   want  to  send  the  file  generated  by  browserify  into   uglify.  So  let’s  refactor  our  script  to  combine  these.   “build:bundle”:  “browserify  ./client/js/app.js  |            uglifyjs  =mc  >  ./public/js/bundle.js”

Slide 29

Slide 29 text

29 Explaining  the  Script The  pipe  command  tells  to  take  the  output  of  the   first  command  -­‐  browserify  -­‐  and  use  that  as  the   input  to  the  next  command  -­‐  uglify.   The  -­‐mc  parameter  tells  us  to  mangle  and  compress   the  files.   The  greater  than  -­‐  >  -­‐  command  tells  the  output  to     be  redirected  to  ./public/js/bundle.js

Slide 30

Slide 30 text

30 Clean  Code Every  Bme  we  run  bundle  we  want  to  make  sure   that  the  old  code  is  deleted  before  the  new  code   is  created.  We  can  create  a  clean  script  to  do  this.   npm  install  rimraf  —save-­‐dev   “build:clean”:  “rimraf  public/css/*,  public/js/*”

Slide 31

Slide 31 text

31 Running  the  Clean  Script We  want  to  run  the  clean  script  right  before  we   run  the  bundle  and  minify  scripts.  We  can  accomplish   this  by  creaBng  a  build  script  and  then  running  the   clean  script  in  a  “prebuild”  script.

Slide 32

Slide 32 text

32 Build  Script “build”:  “npm  run  build:less  &&  npm  run  build:bundle”   “prebuild”:  “npm  run  build:clean”   4-­‐four

Slide 33

Slide 33 text

33 What  scripts  are  available? So  far  we  have  created  almost  a  dozen  scripts  to   handle  linBng,  test,  compiling  less,  bundle,  minify   and  building.   How  do  you  know  what  scripts  are  available?   npm  run

Slide 34

Slide 34 text

34 Server  Side  Scripts During  development  process  you  need  to  have   scripts  that  are  watching  for  certain  events  to  occur.   Compiling  Less  code  into  CSS  is  an  example  of  script   that  you  can  convert  into  a  watch  script.  Every  Bme   there  is  a  change  to  the  less  code  then  it  compiles   into  CSS.  All  you  need  to  do  is  refresh  your  browser   to  see  the  results  of  your  code  change.

Slide 35

Slide 35 text

35 Server  Side  Watching Some  of  the  npm  modules  we  have  installed,  have   flags  that  will  allow  you  to  watch  for  changes.  Mocha   has  this  feature.   “mocha  test  -­‐u  bdd  -­‐R  min  -­‐w”   I  am  using  the  min  Reporter.  The  -­‐w  flag  is  the  watch.

Slide 36

Slide 36 text

36 CreaBng  Test  Watch  Script “watch:test”:  “mocha  test  -­‐R  min  -­‐w”

Slide 37

Slide 37 text

37 Refactor  Watch  Test We  can  improve  our  watch  script  since  it  is  using   the  same  command  as  our  test  script.   “watch:test”:  “npm  run  test  —  -­‐w  -­‐R  min”   The  dash  dash  space  allows  us  to  pass  through   arguments  to  the  underlying  command  which   is  mocha.

Slide 38

Slide 38 text

38 Watching  without  BuilBn  OpBon If  an  npm  module  does  not  have  a  watch  opBon  then   you  can  use  an  npm  module  “watch”  that  can  watch   for  them.   npm  install  watch  —save-­‐dev

Slide 39

Slide 39 text

39 Watching  Lint We  want  a  watch  script  that  checks  for  any  change   to  the  code  and  then  runs  lint  to  make  sure  our  code   passes.  

Slide 40

Slide 40 text

40 Watch  Lint  Script “watch:lint”  “watch  ‘npm  run  lint’  .”   This  will  watch  for  any  changes  to  files  in  the  current   directory  and  if  there  is  a  change  then  run  the  lint   script.

Slide 41

Slide 41 text

41 Versioning Usually  when  you  do  a  deploy  you  want  to  change   the  version  of  your  sopware.  Most  versioning  uses   semanBc  versioning.

Slide 42

Slide 42 text

42 SemanBc  Versioning SemanBc  versioning  consists  of  MAJOR.MINOR.PATCH   numbering  system.  You  increment  the   1. MAJOR  when  you  make  incompaBble  API  changes   2. MINOR  when  you  add  funcBonality  in  a  backwards
 compaBble  manner   3. PATCH  when  you  make  backwards  compaBble
 bug  fixes


Slide 43

Slide 43 text

43 Version  Scripts “version:major”:  “npm  version  major”   “version:minor”:  “npm  version  minor”   “version:patch”:  “npm  version  patch”

Slide 44

Slide 44 text

44 Pushing  to  Github When  you  make  changes  to  your  code  you  want  to   push  those  changes  to  Github.   git  push  origin  master

Slide 45

Slide 45 text

45 Pushing  to  Github “push:origin”:  “git  push  —tags  origin  master”  

Slide 46

Slide 46 text

46 Deploying  to  Heroku First  download  the  heroku  tool  belt  from   h)ps://toolbelt.heroku.com   You  create  an  applicaBon  on  heroic  using  command   heroku  create  appname

Slide 47

Slide 47 text

47 Heroku  Push  Script CreaBng  a  heroic  applicaBon  will  create  a  new  remote   for  you.  You  can  verify  that  with  the  command   npm  remote  -­‐v

Slide 48

Slide 48 text

48 Heroku  Push  Script To  push  code  to  producBon  on  Heroku  you  would   use  the  command   git  push  heroku  master   “push:heroku”:  “git  push  heroku  master”

Slide 49

Slide 49 text

49 Push  Script When  we  deploy  our  code  we  want  to  push  the   latest  version  to  github  and  to  heroic   We  can  combine  our  two  push  scripts  into  one   “push”:  “npm  run  push:origin  &&  npm  run  push:heroku

Slide 50

Slide 50 text

50 Final  Deploy  Script Our  Final  Deploy  Script  should  do  the  following:   1. Link,  compile  and  test  server  side  JavaScript   2. Bundle  and  minify  client  side  JavaScript   3. Compile  LESS  to  CSS   4. New  version   5. Push  to  Github   6. Deploy  to  Heroku

Slide 51

Slide 51 text

51 Deploy  Script “deploy:prod”:  “npm  run  test  -­‐s  &&                                                  npm  run  build  -­‐s  &&        npm  run  version:patch  &&        npm  run  push   5-­‐five      

Slide 52

Slide 52 text

52 LimitaBons  of  using  npm No  comments   Commands  can  be  different  on  Mac  vs  Windows   Harder  to  understand

Slide 53

Slide 53 text

53 Slide  and  Code github.com/ratracegrad/npm-­‐as-­‐a-­‐build-­‐tool    

Slide 54

Slide 54 text

54 NPM  Config  Variables npm  has  a  config  direcBve  which  allows  you  to  set   values  that  can  be  picked  up  as  environment   variables  in  your  scripts    

Slide 55

Slide 55 text

55 NPM  Config  Variables “config”:  {   “reporter”:  “landing”   },   “scripts”:  {   “test”:  “mocha  -­‐r  $npm_package_config_reporter”,   “test:dev”:  “mocha”   }