about? • One way to organise source code for web projects • Past approaches • Desired pattern & need for a tool • An implementation of the pattern using GruntJS as the tool • Key features of the tool • How to extend & use tool Image Credit
this is for? • You are sick of re-creating build processes every time you start a new project • You want to have common workflow steps across projects • You want debugging to be easy • You want to write code & tests, not build tools
your files were grouped into a folder structure that would be uploaded to your web server – input source-tree === output source tree – Worked OK for static sites or server- rendered sites Image Credit
• Good for small codebases/demos • Similar/same as how files will be deployed Cons • Global code is next to module-specific code • Hard to identify which files are part of a module >> doesn’t scale well Image Credit
want the input tree to look like? • Group related-things together into packages/ folders/modules • If modules are complex, break into smaller sub-modules • Move global code into a module • Everything is inside a module – avoid “special” files (e.g. /lib, /test, /util) Image Credit
Pros • Related code is all together (JS, HTML, CSS, tests, fixtures) • No special files – (almost) everything can live inside a module • Build tool can optimise-away non-production files (like tests, included files) Cons • You need to transform your input-tree into a structure suitable for deployment to a website… you need a build tool • It can be time-consuming to create a build tool from scratch Image Credit
Rescue Note: You don’t need to use GruntJS (or GulpJS, Broccoli or NPM) if your input-source code structure is identical to your output code structure... Image Credit
Game • They allow the input code tree to be different to the output tree (by allowing files to be moved around, copied, concatenated, etc) • Now we have the freedom to design our input-tree shape to meet our tastes/needs, but still meet needs of DEVOPS team – yay! Image Credit
grunt-modular-project • A series of grunt tasks • Tasks grouped into workflow-tasks • Default configuration • Override defaults in your own Gruntfile.js • https://github.com/uglow/grunt-modular- project Image Credit
grunt dev – development with recompilation • grunt build – production build • grunt build:serve – as above + web server • grunt test – run unit tests • grunt verify – run style/syntax tests • grunt release<:patch|minor|major> - build + changelog + bump version Image Credit
a work in progress (ie: not perfect) • Hopefully this workflow pattern (dev/build/ test/verify) can be better-implemented in GulpJS or Broccoli or <x> • Having a consistent development approach is more important than the technology used to produce it – don’t get hung up on “Grunt vs X”
(there’s only one) • Q. We build from source code straight to uglified code and source-maps. Why do you suggest a debug build? • A. Because there will be bugs, and source-maps don’t always load in different/older browsers. Additionally, uglifying your code after every JS-change adds time to your debug-loop. Walk first, then run.