Skip to content

Instantly share code, notes, and snippets.

@arichiardi
Last active June 19, 2018 19:50
Show Gist options
  • Save arichiardi/d4b316ea898e19886277ccbd0d5d1621 to your computer and use it in GitHub Desktop.
Save arichiardi/d4b316ea898e19886277ccbd0d5d1621 to your computer and use it in GitHub Desktop.
This is an example of how to bundle a ClojureScript artifact with webpack on node
#!/bin/bash
do_usage() {
echo
echo "Package the zip ready for lambda deployment."
echo
echo " -s is the stage, will default to dev is absent."
echo " -p accepts an dir path where the package will be copied, it defaults
to \"./.serverless\"".
echo " -B option skips compilation, in case you already have compiled
and you just need to deploy."
echo
echo "Usage:"
echo "package [-B] [-s <stage>] [-p <package_dir>]"
exit 1
}
set -euo pipefail
stage="dev"
package_dir=".serverless"
no_compile=
while getopts ":s:Bp:" opt; do
case $opt in
s) stage="$OPTARG"
;;
B) no_compile=1
;;
p) package_dir="$OPTARG"
;;
\?) do_usage
;;
esac
done
out_dir="./out"
mkdir -vp "$package_dir"
package_dir=$(readlink -f "$package_dir")
if [ -z $no_compile ]; then
# compile
./scripts/build $stage
fi
# install only production deps
# if [[ $stage == "prod" ]]; then
# rm -r node_modules
# yarn install --production
# else
# yarn install
# fi
# bundle
webpack_mode=development
if [[ "$stage" == "prod" ]]; then
webpack_mode=production
fi
declare -a function_names
function_names=( $("./scripts/function-names") )
if [[ "$stage" == "prod" ]]; then
for fn in "${function_names[@]}"; do
function_dir="./$fn"
compiled_file="$function_dir/index.js"
bundled_file="$function_dir/index.js"
yarn webpack --mode "$webpack_mode" --config-name "$fn" --config webpack.config.js
done
fi
artifact_name="ep-cloud-lambda-cqrs.zip"
artifact_zip="$package_dir"/"$artifact_name"
artifact_file_list="./scripts/azure-package-files.txt"
# zip the files we have to
cat "$artifact_file_list" | zip -@ "$artifact_zip"
if [[ ! "$stage" == "prod" ]]; then
# zip node_modules
zip -ur "$artifact_zip" node_modules
# zip shadow's cljs runtime
for fn in "${function_names[@]}"; do
runtime_dir="./$fn/cljs-runtime"
zip -ur "$artifact_zip" "$runtime_dir"
done
fi
const path = require("path");
const webpack = require("webpack");
module.exports = {
target: "node",
entry: {
"init-store": "./out/unbundled.js"
},
output: {
filename: "./[name]/index.js",
path: __dirname,
libraryTarget: "commonjs2",
auxiliaryComment: "Bundled artifact."
},
plugins: [
new webpack.IgnorePlugin(/pg-native/, /\/pg\//),
new webpack.IgnorePlugin(/pg-native/),
new webpack.IgnorePlugin(/dtrace-provider/)
]
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment