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

feat: add ESM tools in gax #1459

Merged
merged 33 commits into from
Sep 19, 2023

Conversation

ddelgrosso1
Copy link
Contributor

@ddelgrosso1 ddelgrosso1 commented Jun 20, 2023

This PR does the following:

  1. Adds 3 babel plugins to help transform ESM code to CJS: one to transform path.dirname(fileURLToPath(import.meta) to __dirname, another to turn isEsm to false, and lastly a final one to turn proxyquire into esmock.
  2. Adds an ESM-path in compileProtos (searches for source code one level deeper, and generates protos.js and protos.cjs in es6 and amd, respectively)

@product-auto-label product-auto-label bot added the size: s Pull request size is small. label Jun 20, 2023
@ddelgrosso1
Copy link
Contributor Author

Want to get some eyes on this before I dive deeper into other transformations. Will this be acceptable for our needs?

@ddelgrosso1 ddelgrosso1 requested review from sofisl and bcoe June 20, 2023 15:55
@product-auto-label product-auto-label bot added size: m Pull request size is medium. and removed size: s Pull request size is small. labels Jun 20, 2023
@sofisl
Copy link
Contributor

sofisl commented Jun 20, 2023

Thanks @ddelgrosso1, this is looking awesome! A few questions/things that I think this still needs to do:

  1. Hopefully this transform will rename .js file extensions in the build/cjs directory to .cjs
  2. It would change this variable to false, if compiling from tsconfig.json: https://github.com/googleapis/google-cloud-node/blob/6dc9253cda1931d9c9538364d8ddb0c592b9dc69/packages/google-cloud-billing/esm/src/v1/cloud_catalog_client.ts#L170
  3. It would rename the esm aspect in all of these paths: https://github.com/googleapis/google-cloud-node/pull/4354/files to cjs when compiling to commonJS

@product-auto-label product-auto-label bot added size: l Pull request size is large. and removed size: m Pull request size is medium. labels Jun 22, 2023
@ddelgrosso1
Copy link
Contributor Author

@sofisl regarding your points above 1. I think this can be handled by specifying the appropriate babel cli arguments and won't require custom plugin code. 2. Changing the variable should be easy but are you saying we need to be aware when a tsconfig is in use? If so, that might prove more challenging. 3. Let me look into this one further.

@sofisl
Copy link
Contributor

sofisl commented Jun 28, 2023

@ddelgrosso1 awesome! For #2, ideally we'd know whether we compiled using tsconfig.esm, and that would determine whether that variable was true or not.

@ddelgrosso1 ddelgrosso1 added the owlbot:run Add this label to trigger the Owlbot post processor. label Jul 19, 2023
@gcf-owl-bot gcf-owl-bot bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Jul 19, 2023
@ddelgrosso1 ddelgrosso1 added the owlbot:run Add this label to trigger the Owlbot post processor. label Jul 19, 2023
@gcf-owl-bot gcf-owl-bot bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Jul 19, 2023
Comment on lines +175 to +176
assert(ts.toString().includes('import Long = require'));
assert(!ts.toString().includes('import * as Long'));
Copy link
Contributor

Choose a reason for hiding this comment

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

Just curious - would import Long from work here?

Copy link
Contributor

Choose a reason for hiding this comment

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

Good call out! I think it would, but I'm just looking to preserve functionality here, since I'm not making any changes to the .ts file.

@ddelgrosso1 ddelgrosso1 added the owlbot:run Add this label to trigger the Owlbot post processor. label Sep 12, 2023
@gcf-owl-bot gcf-owl-bot bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Sep 12, 2023
@@ -163,6 +163,13 @@ function updateDtsTypes(dts: string, enums: Set<string>): string {
}

function fixJsFile(js: string): string {
// 1. fix protobufjs import: we don't want the libraries to
Copy link
Contributor

Choose a reason for hiding this comment

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

super nit but let's either make this "0." or increment the numbers below :)

// depend on protobufjs, so we re-export it from google-gax
js = js.replace(
'import * as $protobuf from "protobufjs/minimal"',
'import { protobufMinimal as $protobuf} from "google-gax/build/src/protobuf.js"'
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: formatting, remove space after {

@sofisl sofisl added automerge Merge the pull request once unit tests and other checks pass. owlbot:run Add this label to trigger the Owlbot post processor. labels Sep 19, 2023
@gcf-owl-bot gcf-owl-bot bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Sep 19, 2023
@gcf-merge-on-green gcf-merge-on-green bot merged commit 0fb1cf9 into googleapis:main Sep 19, 2023
15 checks passed
@gcf-merge-on-green gcf-merge-on-green bot removed the automerge Merge the pull request once unit tests and other checks pass. label Sep 19, 2023
@ddelgrosso1 ddelgrosso1 deleted the esm-cjs-tools branch September 19, 2023 14:17
@sofisl sofisl mentioned this pull request Sep 19, 2023
@sofisl sofisl added the release-please:force-run To run release-please label Sep 19, 2023
@release-please release-please bot removed the release-please:force-run To run release-please label Sep 19, 2023
@release-please release-please bot mentioned this pull request Sep 19, 2023
@sofisl sofisl added the release-please:force-run To run release-please label Sep 19, 2023
@release-please release-please bot removed the release-please:force-run To run release-please label Sep 19, 2023
@release-please release-please bot mentioned this pull request Jan 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: xl Pull request size is extra large.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants