Skip to content

Instantly share code, notes, and snippets.

@srquinn21
Last active December 15, 2020 16:19
Show Gist options
  • Select an option

  • Save srquinn21/fa9dd13d1f1efbda6423 to your computer and use it in GitHub Desktop.

Select an option

Save srquinn21/fa9dd13d1f1efbda6423 to your computer and use it in GitHub Desktop.
Frontend Development with Makefile

##Makefiles vs. Grunt/Gulp

The above is meant to demonstrate why Makefiles can be just as powerful (if not more) than Grunt/Gulp for frontend development.

The beauty of node.js is that the Unix philosophy is at its core. While Grunt/Gulp practice this philosophy with small cores and an extendable plugin system, the amount of boilerplate and glue code needed to wrap common CLI tools adds too much maintenance and complexity to a build system. Bash and Makefiles can be intimidating to learn at first, but once learned can be a powerful utility belt that can solve nearly any problem.

###Usage First copy the above Makefile to the root of your project and save as "Makefile". Then simply run:

$ npm install --save-dev make-livereload browserify less csso serve uglify-js
$ make develop
#==========================================================
# Environment
#==========================================================
# For project consistency, its better to depend on npm binaries loaded locally than
# globally, so we add .node_modules/.bin to the path for shorthand references. This
# means you should add any binaries you need to "devDependencies" in package.json.
export PATH := ./node_modules/.bin/:$(PATH)
# Since we depend on node already, we can use its binary plus package.json to
# automatically pull in the name and version
NAME ?= $(shell node -e "console.log(require('./package.json').name || 'app');")
VERSION = $(shell node -e "console.log(require('./package.json').version);")
#==========================================================
# Livereload
#==========================================================
# http://github.com/mklabs/make-livereload
# make-livereload uses the tiny-lr server written in node and curl for
# triggering changes. You'll want to overload the two variables below
DIR ?= .
LIVERELOAD_DIR ?= ./dist
include ./node_modules/make-livereload/index.mk
#==========================================================
# Default
#==========================================================
default: build
#==========================================================
# Phony targets
#==========================================================
.PHONY: clean compress develop static build
#==========================================================
# Tasks
#==========================================================
# Clean the build directory
clean:
rm -rf build/*
# Build all dynamic assets
build: static dist/js/$(NAME).js dist/css/$(NAME).css
# Copy over all static assets
static: $(wildcard src/assets/*)
mkdir -p dist dist/js dist/css
cp -av src/assets/* dist/
# Minify built assets
compress: dist/css/$(NAME).min.css dist/js/$(NAME).min.js
# Startup web server and LiveReload server.
develop: build
make livereload
serve ./dist &
watch make --quiet reload
#==========================================================
# JS Build Targets
#==========================================================
# Local javascript build
dist/js/$(NAME).js: src/main.js $(wildcard src/*.js src/**/*.js)
browserify -t [hbsfy -e html] src/main.js > dist/js/$(NAME).js
# Concat and minify scripts
dist/js/$(NAME).min.js: dist/js/$(NAME).js
cat dist/js/$(NAME).js | uglifyjs -o dist/js/$(NAME).min.js
#==========================================================
# CSS Build Targets
#==========================================================
# Compile LESS files
dist/css/$(NAME).css: src/main.less $(wildcard src/*.less, src/**/*.less)
lessc src/main.less > dist/css/$(NAME).css
# Minify CSS
dist/css/$(NAME).min.css: dist/css/$(NAME).css
csso dist/css/$(NAME).css > dist/css/$(NAME).min.css
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment