Created
August 3, 2011 18:47
-
-
Save rfletcher/1123463 to your computer and use it in GitHub Desktop.
Simple build-time javascript dependency resolution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
( function() { | |
console.log( 'one' ); | |
//= require two.js | |
}() ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
( function() { | |
console.log( 'one' ); | |
console.log( "two" ); | |
}() ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log( "two" ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment