Install node.js and then grunt and bower
Q1. With grunt and bower, do I need to navigate to my roots theme directory first, or does it not matter where I install?
A1. You need all three installed globally. NPM is the package manager for Node.js modules. NPM installs the dependencies listed in your package.json
file. This includes Bower, Grunt, and Grunt modules (by default). You need to run $ npm install
inside the directory that contains package.json
or NPM will throw an error because it won’t know which modules to install.
Run 'bower install ’ from command promt.
Q2. Again, should I do this from my roots theme directory?
A2. Yes, because much like NPM, Bower is a package manager that relies on bower.json
to install Javascript package dependencies. Running $ bower install
will only install packages listed in bower.json
. If you then want to install additional packages just use the $ bower install <pkg>
command. The optional flags --save
and --save-dev
will save your new package as a dependency into bower.json
if you want to keep the dependencies as part of your Git repo (recommended).
Q3. So what is supposed to have happened here? I read that files such as package.json, bowercc are involved, but how? Does it plonk the plugin somewhere?
A3. What’s supposed to have happened is that Bower should’ve installed the dependencies in bower.json
. By default, Bower installs new modules into the bower_components/
directory. This behavior can be overridden by .bowerrc
file. Roots does currently change the destination directory. More info on .bowerrc
here.
Q4.Am I still suppoed to reference my plugin, I assume not as otherwise surely it is quicker just to drag-and-drop the file into assets/js.
A4. In this iteration of Roots, yes, you must add your plugin’s main
scripts to jsFileList
inside your Gruntfile.js
. There are Grunt (and gulp) modules that will automatically lint, contcat, and minify packages’ Javascript without the need for any manual tinkering. Roots with gulp will use one of those modules, but Grunt isn’t part of the future of Roots so the Roots + Grunt + Bower workflow has not been updated to accommodate Bower + Grunt automation (but it’s a relatively trivial step to accomplish if you wanted to, see main-bower-files). Of course you can always drag-and-drop, but command line install is actually faster, keeps everything organized, and makes version updates or rollbacks faster and easier. Installing (and saving) packages via Bower also means that anyone else who might work on your project only has to clone your repo, install NPM modules, and install Bower modules with two simple commands and their theme directory will then match yours. Doing it the drag-and-drop way means a collaborator would need to go out and manually find and download each Javascript plugin, then drag-and-drop it into place and enqueue the assets… all before they even begin any dev work. Of course you could include those packages in your repo, but then you’d need to keep them updated manually and your repo could end up being very large if you use many packages.
Q5. Do I still HAVE to reference the script in the gruntfile, or is that just for validation/minification (which obviously I want and will use).
A5. No, you don’t have to, but if you use Bower to install Javascript plugins and you’re using Roots + Grunt then yes, you do. Grunt doesn’t just lint and minify your Javascript, it also concatenates all of your scripts together into one file—scripts.min.js
—to reduce the number of server requests your site needs to make on page loads. This is all part of the Roots mission to modernize WordPress theme development workflow. If you want to drag-and-drop like it’s 1999 then knock yourself out!