blob: 2753787dc92bafb0e2bd05724282fe28bd3353b3 [file] [log] [blame]
#!/usr/bin/env bash
# Vendor in files from npm based on /third_party/npm/package.json & /third_party/npm/yarn.lock
set -eu -o pipefail
# -e: exits if a command fails
# -u: errors if an variable is referenced before being set
# -o pipefail: causes a pipeline to produce a failure return code if any command errors
# sedi makes `sed -i` work on both OSX & Linux
# See https://stackoverflow.com/questions/2320564/i-need-my-sed-i-command-for-in-place-editing-to-work-with-both-gnu-sed-and-bsd
sedi () {
case $(uname) in
Darwin*) sedi=('-i' '') ;;
*) sedi='-i' ;;
esac
sed "${sedi[@]}" "$@"
}
readonly RULES_NODEJS_DIR=$(cd $(dirname "$0")/..; pwd)
readonly THIRD_PARTY_DIR=${RULES_NODEJS_DIR}/third_party
readonly DATE=$(date)
readonly PACKAGE_JSON_SHA256=$(cat ${THIRD_PARTY_DIR}/package.json | openssl dgst -sha256)
readonly YARN_LOCK_SHA256=$(cat ${THIRD_PARTY_DIR}/yarn.lock | openssl dgst -sha256)
# runs and adds command run to CMDS for later user
capture_and_run() {
CMDS="${CMDS}# "$(echo "$@" | sed "s#${RULES_NODEJS_DIR}/##g")$'\n'
"$@"
}
# preps for vendoring a package
# args:
# $1 - package name
prep() {
CMDS=""
PKG=$1
SRC_DIR=${THIRD_PARTY_DIR}/node_modules/${PKG}
DST_DIR=${THIRD_PARTY_DIR}/npm/node_modules/${PKG}
printf "\n\nVendoring ${PKG} to ${DST_DIR}\n"
rm -rf ${DST_DIR}
mkdir -p ${DST_DIR}
}
# runs ncc
# args:
# $1 - entry point
# $2 - (optional) additional args
ncc() {
echo "- compiling ${PKG} with ncc"
capture_and_run ${THIRD_PARTY_DIR}/node_modules/.bin/ncc build ${SRC_DIR}/$1 ${2:-} -o ${DST_DIR}
}
# minifies with terser
# args:
# $1 - input file
# $1 - output file
minify() {
echo "- minifying ${PKG} with terser"
capture_and_run ${THIRD_PARTY_DIR}/node_modules/.bin/terser --compress --mangle --comments '/(^!|@license|@preserve|Copyright)/' -- ${DST_DIR}/$1 > ${DST_DIR}/$2
}
# copies a file
# args:
# $1 - input & output file
copy() {
echo "- copy $1"
capture_and_run cp -f ${SRC_DIR}/$1 ${DST_DIR}
}
# copies a file
# args:
# $1 - input file
# $2 - output file
copy_to() {
echo "- copy $1 to $2"
capture_and_run cp -f ${SRC_DIR}/$1 ${DST_DIR}/$2
}
# moves a file
# args:
# $1 - input file
# $2 - output file
rename() {
echo "- rename $1 to $2"
capture_and_run mv ${DST_DIR}/$1 ${DST_DIR}/$2
}
# create a BUILD file and adds the common header portions
# args:
# $1 - license file
build_file_header() {
echo """# Generated by /scripts/vendor_npm.sh
load(\"//internal/js_library:js_library.bzl\", \"js_library\")
package(default_visibility = [\"//visibility:public\"])
licenses([\"notice\"])
# Vendored on ${DATE} from npm with /scripts/vendor_npm.sh
exports_files([\"$1\"])
js_library(
name = \"$(basename ${PKG})\",
srcs = glob([\"**/*.js\"]),
package_name = \"${PKG}\",
)
""" > ${DST_DIR}/BUILD.bazel
}
# adds the common footer to the BUILD file
build_file_footer() {
echo """# shell commands:
${CMDS}
# package.json (SHA256 ${PACKAGE_JSON_SHA256}):
$(cat ${THIRD_PARTY_DIR}/package.json | awk '{print "# "$0}')
# yarn.lock (SHA256 ${YARN_LOCK_SHA256}):
$(cat ${THIRD_PARTY_DIR}/yarn.lock | awk '{print "# "$0}')
""" >> ${DST_DIR}/BUILD.bazel
}
(
cd ${THIRD_PARTY_DIR}
yarn install
##############################################################################
# Vendor browserify
##############################################################################
(
prep browserify
ncc bin/cmd.js
echo "- local mod: revert https://github.com/browserify/browserify/pull/1801"
capture_and_run sedi 's#parent.id !== self._mdeps.top.id#parent.id#' ${DST_DIR}/index.js
echo "- local mod: workaround https://github.com/zeit/ncc/issues/461"
capture_and_run sedi "s#require('process/browser.js')#require('./browser1')#" ${DST_DIR}/main.js
rename index.js index.debug.js
minify index.debug.js index.js
copy LICENSE
build_file_header LICENSE
echo """filegroup(
name = \"package_contents\",
# don't include index.debug.js to keep release smaller
srcs = glob([\"**/*.js\"], exclude=[\"index.debug.js\"]) + [
\"advanced.txt\",
\"BUILD.bazel\",
\"LICENSE\",
\"usage.txt\",
],
)
""" >> ${DST_DIR}/BUILD.bazel
build_file_footer
)
##############################################################################
# Vendor ieee754
# (required by browserify)
##############################################################################
(
prep ieee754
copy index.js
copy LICENSE
build_file_header LICENSE
echo """filegroup(
name = \"package_contents\",
srcs = glob([\"**/*.js\"]) + [
\"BUILD.bazel\",
\"LICENSE\",
],
)
""" >> ${DST_DIR}/BUILD.bazel
build_file_footer
)
##############################################################################
# Vendor base64-js
# (required by browserify)
##############################################################################
(
prep base64-js
copy index.js
copy LICENSE
build_file_header LICENSE
echo """filegroup(
name = \"package_contents\",
srcs = glob([\"**/*.js\"]) + [
\"BUILD.bazel\",
\"LICENSE\",
],
)
""" >> ${DST_DIR}/BUILD.bazel
build_file_footer
)
##############################################################################
# Vendor @babel/core
##############################################################################
(
prep @babel/core
ncc lib/index.js
rename index.js index.debug.js
minify index.debug.js index.js
copy LICENSE
build_file_header LICENSE
echo """filegroup(
name = \"package_contents\",
# don't include index.debug.js to keep release smaller
srcs = glob([\"**/*.js\"], exclude=[\"index.debug.js\"]) + [
\"BUILD.bazel\",
\"LICENSE\",
],
)
""" >> ${DST_DIR}/BUILD.bazel
build_file_footer
)
##############################################################################
# Vendor babelify
##############################################################################
(
prep babelify
copy index.js
copy LICENSE
build_file_header LICENSE
echo """filegroup(
name = \"package_contents\",
srcs = glob([\"**/*.js\"]) + [
\"BUILD.bazel\",
\"LICENSE\",
],
)
""" >> ${DST_DIR}/BUILD.bazel
build_file_footer
)
##############################################################################
# Vendor named-amd
##############################################################################
(
prep named-amd
copy_to named-amd.js index.js
copy LICENSE.md
build_file_header LICENSE.md
echo """filegroup(
name = \"package_contents\",
srcs = glob([\"**/*.js\"]) + [
\"BUILD.bazel\",
\"LICENSE.md\",
],
)
""" >> ${DST_DIR}/BUILD.bazel
build_file_footer
)
##############################################################################
# Vendor babelify plugins
##############################################################################
readonly BROWSERIFY_PLUGINS=(
'@babel/plugin-transform-modules-commonjs'
)
for PLUGIN in "${BROWSERIFY_PLUGINS[@]}"; do
(
prep ${PLUGIN}
ncc lib/index.js "-e @babel/core"
echo "- copy babel LICENSE"
capture_and_run cp -f ${THIRD_PARTY_DIR}/node_modules/@babel/core/LICENSE ${DST_DIR}
build_file_header LICENSE
echo """filegroup(
name = \"package_contents\",
srcs = glob([\"**/*.js\"]) + [
\"BUILD.bazel\",
\"LICENSE\",
],
)
""" >> ${DST_DIR}/BUILD.bazel
build_file_footer
)
done
rm -rf node_modules
)