Skip to content

Instantly share code, notes, and snippets.

@ptgolden
Created June 1, 2015 18:36
Show Gist options
  • Save ptgolden/53a2ef7aa0a79f549438 to your computer and use it in GitHub Desktop.
Save ptgolden/53a2ef7aa0a79f549438 to your computer and use it in GitHub Desktop.
Browserify project setup
# Initialize project
npm init -y
# Install dependences
npm install --save-dev browserify watchify babelify
# Set browserify commands
BROWSERIFY_ARGS='src/index -o dist/bundle.js'
MKDIR='mkdir -p dist'
BABELIFY_TRANSFORM='["babelify", { "sourceMapRelative": ".", "only": "src/**" }]'
cat package.json | \
jq ".browserify={\"transform\": [$BABELIFY_TRANSFORM]}" | \
jq ".scripts.build=\"$MKDIR && browserify $BROWSERIFY_ARGS\"" | \
jq ".scripts[\"build-watch\"]=\"$MKDIR && watchify $BROWSERIFY_ARGS -dv\"" \
> package.json.tmp
mv package.json.tmp package.json
# Add file to be compiled
mkdir -p src
cat <<EOF > src/write_greeting.js
module.exports = greeting => document.write(\`<h1>\${greeting}</h1>\`);
EOF
cat <<EOF > src/index.js
var writeGreeting = require('./write_greeting');
writeGreeting('Hello from browserify');
EOF
# Add html file to serve bundle
cat <<EOF > index.html
<!doctype html>
<html>
<body><script src="dist/bundle.js"></script></body>
</html>
EOF
# Build browserify bundle
npm run build
# Open index.html in browser
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment