k8s.io/test-infra@v0.0.0-20240520184403-27c6b4c223d8/hack/rollup-js.sh (about)

     1  #!/usr/bin/env bash
     2  # Copyright 2021 The Kubernetes Authors.
     3  #
     4  # Licensed under the Apache License, Version 2.0 (the "License");
     5  # you may not use this file except in compliance with the License.
     6  # You may obtain a copy of the License at
     7  #
     8  #     http://www.apache.org/licenses/LICENSE-2.0
     9  #
    10  # Unless required by applicable law or agreed to in writing, software
    11  # distributed under the License is distributed on an "AS IS" BASIS,
    12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  # See the License for the specific language governing permissions and
    14  # limitations under the License.
    15  
    16  set -o errexit
    17  set -o nounset
    18  set -o pipefail
    19  
    20  REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)"
    21  cd "${REPO_ROOT}"
    22  
    23  readonly ROLLUP_CONFIG="rollup.config.js"
    24  
    25  ROLLUP_ENTRYPOINT_DIR="${1:-}"
    26  if [[ -z $ROLLUP_ENTRYPOINT_DIR ]]; then
    27      echo "ERROR: rollup entrypoint dir must be provided."
    28      exit 1
    29  fi
    30  
    31  ROLLUP_ENTRYPOINT_FILE="${2:-}"
    32  if [[ -z $ROLLUP_ENTRYPOINT_FILE ]]; then
    33      echo "ERROR: rollup entrypoint file must be provided."
    34      exit 1
    35  fi
    36  
    37  readonly JS_OUTPUT_DIR="_output/js"
    38  mkdir -p "${JS_OUTPUT_DIR}"
    39  
    40  # ensure deps are installed
    41  hack/build/ensure-node_modules.sh
    42  
    43  hack/run-in-node-container.sh \
    44      node_modules/rimraf/bin.js dist
    45  
    46  echo "Running tsc"
    47  TSCONFIG="${ROLLUP_ENTRYPOINT_DIR}/tsconfig.json"
    48  # tsc by default output to `<outDIR>/<rel-path>/<basename>.js`, for example for
    49  # `prow/cmd/deck/static/job-history` dir, the out file is
    50  # `_output/js/prow/cmd/deck/static/job-history/job-history.js`
    51  ENTRYPOINT_BASENAME="$(basename $ROLLUP_ENTRYPOINT_DIR)"
    52  JS_OUTPUT_FILE="${JS_OUTPUT_DIR}/${ROLLUP_ENTRYPOINT_DIR}/${ROLLUP_ENTRYPOINT_FILE}.js"
    53  hack/run-in-node-container.sh \
    54      node_modules/typescript/bin/tsc -p "${TSCONFIG}" --outDir "${JS_OUTPUT_DIR}"
    55  
    56  echo "Running rollup"
    57  BUNDLE_OUTPUT_DIR="${JS_OUTPUT_DIR}/${ROLLUP_ENTRYPOINT_DIR}"
    58  ROLLUP_OUT_FILE="${BUNDLE_OUTPUT_DIR}/bundle.js"
    59  hack/run-in-node-container.sh \
    60      node_modules/rollup/dist/bin/rollup --environment "ROLLUP_OUT_FILE:${ROLLUP_OUT_FILE},ROLLUP_ENTRYPOINT:${JS_OUTPUT_FILE}" -c "${ROLLUP_CONFIG}" --preserveSymlinks
    61  
    62  echo "Running terser"
    63  TERSER_CONFIG_FILE="${REPO_ROOT}/hack/ts.rollup_bundle.min.minify_options.json"
    64  TERSER_OUT_FILE="${BUNDLE_OUTPUT_DIR}/bundle.min.js"
    65  hack/run-in-node-container.sh \
    66      node_modules/terser/bin/terser "${ROLLUP_OUT_FILE}" --output "${TERSER_OUT_FILE}" --config-file "${TERSER_CONFIG_FILE}"
    67  
    68  if [[ -n "${OUT:-}" ]]; then
    69      echo "Output is at ${OUT}"
    70      cp "${TERSER_OUT_FILE}" "${OUT}"
    71  fi