Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: support CJS #883

Merged
merged 15 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/generate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ jobs:
node-version: 20
cache: "pnpm"

- name: Install rename
run: |
sudo apt-get update
sudo apt-get install rename

- name: Install JS dependencies
run: pnpm install

Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ jobs:
node-version: 20
cache: "pnpm"

- name: Install rename
run: |
sudo apt-get update
sudo apt-get install rename

- name: Install dependencies
run: pnpm install

Expand Down
2 changes: 1 addition & 1 deletion packages/javascript/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
node_modules/
src/gen
!src/gen/version.d.ts
!src/gen/dts/version.d.ts
18 changes: 13 additions & 5 deletions packages/javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,31 @@
"description": "",
"type": "module",
nbsp marked this conversation as resolved.
Show resolved Hide resolved
"main": "src/index.js",
"import": "src/index.cjs",
"types": "src/index.d.ts",
"files": [
"src"
],
"exports": {
".": {
"types": "./src/index.d.ts",
"default": "./src/index.js"
"import": "./src/index.js",
"require": "./src/index.cjs"
},
"./*": {
"types": "./src/gen/livekit_*_pb.d.ts",
"default": "./src/gen/livekit_*_pb.js"
"types": "./src/gen/dts/livekit_*_pb.d.ts",
"import": "./src/gen/esm/livekit_*_pb.js",
"require": "./src/gen/cjs/livekit_*_pb.js"
nbsp marked this conversation as resolved.
Show resolved Hide resolved
}
},
"scripts": {
"generate:version": "genversion --esm --semi src/gen/version.js",
"generate:proto": "protoc --es_out src/gen --es_opt target=js+dts -I=../../protobufs ../../protobufs/livekit_*.proto",
"generate:version:esm": "genversion --esm --semi src/gen/esm/version.js",
"generate:version:cjs": "genversion --semi src/gen/cjs/version.js",
"generate:version": "pnpm generate:version:esm && pnpm generate:version:cjs",
"generate:proto:esm": "mkdir -p src/gen/esm && protoc --es_out src/gen/esm --es_opt target=js -I=../../protobufs ../../protobufs/livekit_*.proto",
"generate:proto:cjs": "mkdir -p src/gen/cjs && protoc --es_out src/gen/cjs --es_opt target=js,js_import_style=legacy_commonjs -I=../../protobufs ../../protobufs/livekit_*.proto && rename \"s/js$/cjs/\" src/gen/cjs/*.js && sed -i 's/\\.js\")/.cjs\")/' src/gen/cjs/*.cjs",
"generate:proto:dts": "protoc --es_out src/gen/dts --es_opt target=dts -I=../../protobufs ../../protobufs/livekit_*.proto",
"generate:proto": "pnpm generate:proto:esm && pnpm generate:proto:cjs && pnpm generate:proto:dts",
"build": "pnpm generate:version && pnpm generate:proto"
},
"keywords": [],
Expand Down
28 changes: 28 additions & 0 deletions packages/javascript/src/index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// @ts-check
const { protoInt64 } = require("@bufbuild/protobuf");
const agentDispatch = require("./gen/cjs/livekit_agent_dispatch_pb.cjs");
const agent = require("./gen/cjs/livekit_agent_pb.cjs");
const egress = require("./gen/cjs/livekit_egress_pb.cjs");
const ingress = require("./gen/cjs/livekit_ingress_pb.cjs");
const metrics = require("./gen/cjs/livekit_metrics_pb.cjs");
const models = require("./gen/cjs/livekit_models_pb.cjs");
const room = require("./gen/cjs/livekit_room_pb.cjs");
const rtc = require("./gen/cjs/livekit_rtc_pb.cjs");
const sip = require("./gen/cjs/livekit_sip_pb.cjs");
const webhook = require("./gen/cjs/livekit_webhook_pb.cjs");
const version = require("./gen/cjs/version.cjs");

module.exports = {
protoInt64,
...agentDispatch,
...agent,
...egress,
...ingress,
...metrics,
...models,
...room,
...rtc,
...sip,
...webhook,
version,
}
22 changes: 11 additions & 11 deletions packages/javascript/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
export { protoInt64 } from "@bufbuild/protobuf";
export * from "./gen/livekit_agent_dispatch_pb.js";
export * from "./gen/livekit_agent_pb.js";
export * from "./gen/livekit_egress_pb.js";
export * from "./gen/livekit_ingress_pb.js";
export * from "./gen/livekit_metrics_pb.js";
export * from "./gen/livekit_models_pb.js";
export * from "./gen/livekit_room_pb.js";
export * from "./gen/livekit_rtc_pb.js";
export * from "./gen/livekit_sip_pb.js";
export * from "./gen/livekit_webhook_pb.js";
export * from "./gen/version.js";
export type * from "./gen/dts/livekit_agent_dispatch_pb.d.ts";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm, don't remember ever seeing this import pattern 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the LSP didn't complain, but i'll double check to make sure this works

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

works great, except the files under cjs need to be renamed to .cjs in order to be required as CommonJS. i don't know if GH Actions comes built in with any rename utility

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok so i did a bulkrename but now i need to sed through all the files and replace the names as well

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be nice if we could configure that as part of the protocol es plugin, I think file extension is an option there?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only able to set js/ts/d.ts on there unfortunately

export type * from "./gen/dts/livekit_agent_pb.d.ts";
export type * from "./gen/dts/livekit_egress_pb.d.ts";
export type * from "./gen/dts/livekit_ingress_pb.d.ts";
export type * from "./gen/dts/livekit_metrics_pb.d.ts";
export type * from "./gen/dts/livekit_models_pb.d.ts";
export type * from "./gen/dts/livekit_room_pb.d.ts";
export type * from "./gen/dts/livekit_rtc_pb.d.ts";
export type * from "./gen/dts/livekit_sip_pb.d.ts";
export type * from "./gen/dts/livekit_webhook_pb.d.ts";
export type * from "./gen/dts/version.d.ts";
22 changes: 11 additions & 11 deletions packages/javascript/src/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// @ts-check
export { protoInt64 } from "@bufbuild/protobuf";
export * from "./gen/livekit_agent_dispatch_pb.js";
export * from "./gen/livekit_agent_pb.js";
export * from "./gen/livekit_egress_pb.js";
export * from "./gen/livekit_ingress_pb.js";
export * from "./gen/livekit_metrics_pb.js";
export * from "./gen/livekit_models_pb.js";
export * from "./gen/livekit_room_pb.js";
export * from "./gen/livekit_rtc_pb.js";
export * from "./gen/livekit_sip_pb.js";
export * from "./gen/livekit_webhook_pb.js";
export * from "./gen/version.js";
export * from "./gen/esm/livekit_agent_dispatch_pb.js";
export * from "./gen/esm/livekit_agent_pb.js";
export * from "./gen/esm/livekit_egress_pb.js";
export * from "./gen/esm/livekit_ingress_pb.js";
export * from "./gen/esm/livekit_metrics_pb.js";
export * from "./gen/esm/livekit_models_pb.js";
export * from "./gen/esm/livekit_room_pb.js";
export * from "./gen/esm/livekit_rtc_pb.js";
export * from "./gen/esm/livekit_sip_pb.js";
export * from "./gen/esm/livekit_webhook_pb.js";
export * from "./gen/esm/version.js";
Loading