Skip to content

Commit 7fb0fa3

Browse files
committed
feat: enable ESM support via dual-publishing
- Configure tsdown to build both CJS and ESM outputs. - Update package.json exports to support both 'require' and 'import'. - Fix type hygiene issues by using explicit 'export type'. - Update protos/update.sh to generate ESM version of compiledFirestore. - Configure build aliases to handle relative paths to protos correctly.
1 parent 64d9985 commit 7fb0fa3

File tree

14 files changed

+4623
-261
lines changed

14 files changed

+4623
-261
lines changed

package-lock.json

Lines changed: 752 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 268 additions & 214 deletions
Large diffs are not rendered by default.

protos/compiledFirestore.mjs

Lines changed: 3512 additions & 0 deletions
Large diffs are not rendered by default.

protos/update.sh

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
# vars
2626
PROTOS_DIR="$(pwd)"
27-
WORK_DIR=`mktemp -d`
27+
WORK_DIR=$(mktemp -d)
2828

2929
# deletes the temp directory on exit
3030
function cleanup {
@@ -37,10 +37,6 @@ function cleanup {
3737
# register the cleanup function to be called on the EXIT signal
3838
trap cleanup EXIT
3939

40-
# Capture location of pbjs / pbts before we pushd.
41-
PBJS="$(npm bin)/pbjs"
42-
PBTS="$(npm bin)/pbts"
43-
4440
# enter working directory
4541
pushd "$WORK_DIR"
4642

@@ -69,7 +65,32 @@ cp googleapis/google/type/latlng.proto \
6965

7066
popd
7167

72-
"${PBJS}" -t static-module -w commonjs -o compiledFirestore.js \
68+
PBJS="npx pbjs"
69+
PBTS="npx pbts"
70+
71+
# Generate CommonJS
72+
${PBJS} -t static-module -w commonjs -o compiledFirestore.js \
73+
data.proto any.proto
74+
75+
# Generate ESM
76+
${PBJS} -t static-module -w es6 -o compiledFirestore.mjs \
7377
data.proto any.proto
7478

75-
"${PBTS}" -o compiledFirestore.d.ts compiledFirestore.js
79+
# Generate Types
80+
${PBTS} -o compiledFirestore.d.ts compiledFirestore.js
81+
#
82+
# Fix imports for Node ESM in the generated .mjs file.
83+
# See: https://github.com/protobufjs/protobuf.js/issues/1929
84+
if [[ "$OSTYPE" == "darwin"* ]]; then
85+
# 1. Append .js extension: Node ESM requires full paths for subpath imports not in 'exports'.
86+
sed -i '' 's|protobufjs/minimal|protobufjs/minimal.js|g' compiledFirestore.mjs
87+
# 2. Use default import: protobufjs is CJS. 'import * as' creates a namespace where
88+
# module.exports is under .default. Generated code expects $protobuf to be module.exports directly.
89+
sed -i '' 's|import \* as \$protobuf|import \$protobuf|g' compiledFirestore.mjs
90+
else
91+
# 1. Append .js extension.
92+
sed -i 's|protobufjs/minimal|protobufjs/minimal.js|g' compiledFirestore.mjs
93+
# 2. Use default import.
94+
sed -i 's|import \* as \$protobuf|import \$protobuf|g' compiledFirestore.mjs
95+
fi
96+

src/params/index.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,11 @@ import {
3939
InternalExpression,
4040
} from "./types";
4141

42-
export {
43-
BUCKET_PICKER,
44-
TextInput,
45-
SelectInput,
46-
SelectOptions,
47-
MultiSelectInput,
48-
select,
49-
multiSelect,
50-
} from "./types";
42+
export { BUCKET_PICKER, select, multiSelect } from "./types";
43+
export type { TextInput, SelectInput, SelectOptions, MultiSelectInput } from "./types";
5144

52-
export { ParamOptions, Expression };
45+
export { Expression };
46+
export type { ParamOptions };
5347

5448
type SecretOrExpr = Param<any> | SecretParam | JsonSecretParam<any>;
5549
export const declaredParams: SecretOrExpr[] = [];

src/v1/cloud-functions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
// SOFTWARE.
2222

23-
import { Request, Response } from "express";
23+
import type { Request, Response } from "express";
2424
import { warn } from "../logger";
2525
import {
2626
DEFAULT_FAILURE_POLICY,
@@ -29,7 +29,7 @@ import {
2929
FailurePolicy,
3030
Schedule,
3131
} from "./function-configuration";
32-
export { Request, Response };
32+
export type { Request, Response };
3333
import {
3434
convertIfPresent,
3535
copyIfPresent,

src/v1/providers/auth.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ import { DeploymentOptions } from "../function-configuration";
5050
import { initV1Endpoint } from "../../runtime/manifest";
5151

5252
// TODO: yank in next breaking change release
53-
export { UserRecord, UserInfo, UserRecordMetadata, userRecordConstructor };
53+
export { UserRecordMetadata, userRecordConstructor };
54+
export type { UserRecord, UserInfo };
5455

5556
export { HttpsError };
5657

src/v1/providers/https.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ import { initV1Endpoint } from "../../runtime/manifest";
3636
import { withInit } from "../../common/onInit";
3737
import { wrapTraceContext } from "../../v2/trace";
3838

39-
export { Request, CallableContext, FunctionsErrorCode, HttpsError };
39+
export { HttpsError };
40+
export type { Request, CallableContext, FunctionsErrorCode };
4041

4142
/**
4243
* Handle HTTP requests.

src/v1/providers/tasks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import {
3939
import { optionsToEndpoint, optionsToTrigger } from "../cloud-functions";
4040
import { DeploymentOptions } from "../function-configuration";
4141

42-
export { RetryConfig, RateLimits, TaskContext };
42+
export type { RetryConfig, RateLimits, TaskContext };
4343

4444
/**
4545
* Options for configuring the task queue to listen to.

src/v2/core.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import { ManifestEndpoint } from "../runtime/manifest";
3030

3131
export { Change };
3232

33-
export { ParamsOf } from "../common/params";
33+
export type { ParamsOf } from "../common/params";
3434
export { onInit } from "../common/onInit";
3535

3636
/** @internal */

0 commit comments

Comments
 (0)