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 and Just Work™.

###Usage First copy the above Makefile to the root of your project and save as "Makefile". If you don't have watch(1) installed:

$ git clone https://github.com/tj/watch.git /tmp
$ cd /tmp/watch
$ make install
or
$ sudo make install

Then simply run:

$ npm install --save-dev make-livereload browserify less csso serve uglify-js
$ make develop
#==========================================================
# Environment/Configuration
#==========================================================
# 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 it in a mini shell script to automatically
# pull in the name and version from package.json. The name will default to "app" if not set.
NAME ?= $(shell node -e "console.log(require('./package.json').name || 'app');")
VERSION = $(shell node -e "console.log(require('./package.json').version);")
# Here we configure the names of our project's directories. Feel free to
# overload for your own setup. You'll need to follow the convention that there
# are "main.js" and "main.less" files in the root of the SRC directory.
DIR ?= .
SRC ?= ./src
SRC_STATIC ?= $(SRC)/assets
DEST ?= ./dist
DEST_JS ?= $(DEST)/js
DEST_CSS ?= $(DEST)/css
# Here set some options for our binaries
BROWSERIFY_OPTS ?= "-t [hbsfy -e html]"
UGLIFY_OPTS ?= ""
#==========================================================
# Livereload
#==========================================================
# http://github.com/mklabs/make-livereload
# make-livereload uses the tiny-lr server written in node and curl for
# triggering changes.
LIVERELOAD_DIR ?= $(DEST)
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 $(DEST)/*
# Build all dynamic assets
build: static $(DEST_JS)/$(NAME).js $(DEST_CSS)/$(NAME).css
# Copy over all static assets
static: $(wildcard, $(SRC_STATIC)/*, $(SRC_STATIC)/**/*)
mkdir -p $(DEST) $(DEST_JS) $(DEST_CSS)
cp -av $(SRC_STATIC)/* $(DEST)/
# Minify built assets
compress: build $(DEST_JS)/$(NAME).min.js $(DEST_CSS)/$(NAME).min.css
# Startup web server and LiveReload server.
develop: build
$(MAKE) livereload
serve ./dist &
watch $(MAKE) --quiet reload
#==========================================================
# JS Build Targets
#==========================================================
# Local javascript build
$(DEST_JS)/$(NAME).js: $(SRC)/main.js $(wildcard $(SRC)/*.js $(SRC)/**/*.js)
browserify $(BROSERIFY_OPTS) $(SRC)/main.js > $(DEST_JS)/$(NAME).js
# Concat and minify scripts
$(DEST_JS)/$(NAME).min.js: $(DEST_JS)/$(NAME).js
cat $(DEST_JS)/$(NAME).js | uglifyjs $(UGLIFY_OPTS) -o $(DEST_JS)/$(NAME).min.js
#==========================================================
# CSS Build Targets
#==========================================================
# Compile LESS files
$(DEST_CSS)/$(NAME).css: $(SRC)/main.less $(wildcard $(SRC)/*.less, $(SRC)/**/*.less)
lessc $(SRC)/main.less > $(DEST_CSS)/$(NAME).css
# Minify CSS
$(DEST_CSS)/$(NAME).min.css: $(DEST_CSS)/$(NAME).css
csso $(DEST_CSS)/$(NAME).css > $(DEST_CSS)/$(NAME).min.css
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment