Skip to content

Instantly share code, notes, and snippets.

@rfletcher
Created August 3, 2011 18:47
Show Gist options
  • Save rfletcher/1123463 to your computer and use it in GitHub Desktop.
Save rfletcher/1123463 to your computer and use it in GitHub Desktop.
Simple build-time javascript dependency resolution
( function() {
console.log( 'one' );
//= require two.js
}() );
#!/usr/bin/env bash
##
# a crude solution for resolving dependencies
#
# this script looks for "//= require foo", and replaces it with the contents
# of the file "foo" inline. it makes no effort to prevent cyclical
# dependencies, so be careful there.
#
DIRECTIVE="//= require "
function resolve_dependencies {
local FILE="$1"
local DIR=$(dirname "$FILE")
if grep -q "$DIRECTIVE" "$FILE"; then
cat "$FILE" | while IFS='' read -r LINE; do
local DEPENDENCY=$(expr "$LINE" : ".*$DIRECTIVE\(.*\)")
if [[ "$DEPENDENCY" != "" ]]; then
DEPENDENCY="${DIR}/${DEPENDENCY}"
if [ -f "$DEPENDENCY" ]; then
echo -n "${LINE%%$DIRECTIVE*}"
resolve_dependencies "$DEPENDENCY"
else
echo "error: no such file: $DEPENDENCY (required by $FILE)" >&2
exit 1
fi
else
echo "$LINE"
fi
done
else
cat "$FILE"
fi
}
if [[ "$1" == "" ]]; then
echo "usage: $0 <file.js>" >&2
else
resolve_dependencies "$1"
fi
( function() {
console.log( 'one' );
console.log( "two" );
}() );
console.log( "two" );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment