11#! /bin/bash
22set -e
33
4- # Argument 1 (Optional) : Path to a pre-built tarball.
4+ # Argument 1: Path to a pre-built tarball.
55# If not provided, the script will run 'npm run build' and 'npm pack' locally.
66PREBUILT_TARBALL=" $1 "
77
@@ -15,6 +15,7 @@ trap cleanup EXIT
1515
1616# Save current directory to resolve relative paths later
1717START_DIR=" $( pwd) "
18+ SCRIPT_DIR=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd) "
1819
1920if [ -n " $PREBUILT_TARBALL " ]; then
2021 echo " Using prebuilt tarball: $PREBUILT_TARBALL "
@@ -25,8 +26,6 @@ if [ -n "$PREBUILT_TARBALL" ]; then
2526 TARBALL_PATH=" $PREBUILT_TARBALL "
2627else
2728 echo " Building project..."
28- # Ensure we are in the project root for building
29- SCRIPT_DIR=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd ) "
3029 cd " $SCRIPT_DIR /.."
3130 npm run build
3231
@@ -41,34 +40,10 @@ pushd "$WORK_DIR" > /dev/null
4140npm init -y > /dev/null
4241npm install " $TARBALL_PATH " > /dev/null
4342
44- echo " Generating verification scripts..."
45- node -e '
46- const fs = require("fs");
47- // Read the package.json of the INSTALLED package to verify what was actually packed
48- const pkg = require("./node_modules/firebase-functions/package.json");
49- const exports = Object.keys(pkg.exports);
50-
51- // Filter out non-code entrypoints (e.g. package.json if it were exported)
52- const entryPoints = exports.filter(e => !e.endsWith(".json"));
53-
54- let cjsContent = "console.log(\"Verifying CJS exports...\");\n";
55- let esmContent = "console.log(\"Verifying ESM exports...\");\n";
56-
57- for (const exp of entryPoints) {
58- const importPath = exp === "." ? "firebase-functions" : `firebase-functions/${exp.replace("./", "")}`;
59- cjsContent += `try { require("${importPath}"); console.log("✅ CJS: ${importPath}"); } catch (e) { console.error("❌ CJS Failed: ${importPath}", e); process.exit(1); }\n`;
60- esmContent += `try { await import("${importPath}"); console.log("✅ ESM: ${importPath}"); } catch (e) { console.error("❌ ESM Failed: ${importPath}", e); process.exit(1); }\n`;
61- }
62-
63- fs.writeFileSync("verify-cjs.cjs", cjsContent);
64- fs.writeFileSync("verify-esm.mjs", esmContent);
65- '
66-
67- echo " Running CJS verification..."
68- node verify-cjs.cjs
69-
70- echo " Running ESM verification..."
71- node verify-esm.mjs
43+ echo " Running verification script..."
44+ # Copy the verification script to the work directory to ensure it resolves
45+ # the locally installed 'firebase-functions' package.
46+ cp " $SCRIPT_DIR /verify-exports.mjs" .
47+ node verify-exports.mjs
7248
7349popd > /dev/null
74- echo " ✨ Packaging test passed!"
0 commit comments