Title: [v8] Update for ESM monkeypatch + ESM file structure · Issue #10046 · getsentry/sentry-javascript · GitHub
Open Graph Title: [v8] Update for ESM monkeypatch + ESM file structure · Issue #10046 · getsentry/sentry-javascript
X Title: [v8] Update for ESM monkeypatch + ESM file structure · Issue #10046 · getsentry/sentry-javascript
Description: We’re going to be adding proper support for ESM in the Sentry repo. Requirement: There is no require in ESM bundles at all ESM files have .mjs file extension CJS files have .cjs file extension ### Tasks - [ ] https://github.com/getsentry...
Open Graph Description: We’re going to be adding proper support for ESM in the Sentry repo. Requirement: There is no require in ESM bundles at all ESM files have .mjs file extension CJS files have .cjs file extension ### ...
X Description: We’re going to be adding proper support for ESM in the Sentry repo. Requirement: There is no require in ESM bundles at all ESM files have .mjs file extension CJS files have .cjs file extension ### ...
Opengraph URL: https://github.com/getsentry/sentry-javascript/issues/10046
X: @github
Domain: github.com
IPv4 address: 140.82.113.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[v8] Update for ESM monkeypatch + ESM file structure","articleBody":"We’re going to be adding proper support for ESM in the Sentry repo.\n\nRequirement:\n\n- There is no `require` in ESM bundles at all\n- ESM files have `.mjs` file extension\n- CJS files have `.cjs` file extension\n\n```[tasklist]\n### Tasks\n- [ ] https://github.com/getsentry/sentry-javascript/pull/10069\n- [x] (pre-v8) Figure out how to use `dynamicRequire` with ESM\n- [x] (pre-v8) Remove usage of `require.main.filename` for module name resolution #10061\n- [x] (v8) Vendor `https-proxy-agent` which is cjs only #10088\n- [x] (v8) Change build to output `mjs`/`cjs` for esm/cjs\n- [x] (v8) Remove conditional import of `worker_threads` #10791\n- [x] (v8) Update `inspector.d.ts` to work with esm and cjs\n- [x] (v8) Remove require call in `require('inspector')` in `LocalVariables` integration\n- [x] (v8) Update Rollup\n- [ ] https://github.com/getsentry/sentry-javascript/pull/10928\n- [ ] https://github.com/getsentry/sentry-javascript/issues/11066\n- [ ] https://github.com/getsentry/sentry-javascript/issues/11067\n```\n\nRead below for some justification and information\n\n## Module Customization Hooks for Monkey Patching\n\nFor ESM bundles on server-side, we’ll require a minimum Node version of `18.6.0` or `20.6.0` or higher. Otherwise we’ll support CJS bundles for Node 14+ generally. This is because we want access to [Node customization hooks](https://nodejs.org/api/module.html#customization-hooks), which allows us to add monkeypatching for esm modules programatically (doesn't require command-line `loader`).\n\nWe can take some inspiration here from https://github.com/DataDog/import-in-the-middle\n\n```js\nimport { register } from 'node:module';\n\nregister('./sentry-patch.mjs', import.meta.url);\n```\n\nRegistering hooks to affect an import still needs to happen before the import is resolved, so there are ordering issues, but I think we can work around this by recommending that `Sentry.init` is called as soon as possible, and encouraging `Sentry.addIntegration` patterns for integrations that require objects from different libraries (like express with `app`).\n\nThe modules we need register via `register` API:\n\n- ~Node `node:http` and `node:https` (diagnostics channels only works well Node 16+, so we need to maintain this for Node 14~\n- Remix `@remix-run/server-runtime` and ~`react-router-dom`~ - monkeypatch\n- OTEL esm instrumentation\n- `GoogleCloudGrpc` integration `require('google-gax')`\n- `GoogleCloudHttp` integration with `require('@google-cloud/common')`\n- `AWSServices` integration with `require('aws-sdk/global')`\n\n~We need to re-evaluate if we need monkeypatching for the database integrations now that we have OpenTelemetry.~\n\n## Change File Structure\n\nWe’ll need to move our file structure to the following:\n\n```js\n{\n // at build time we strip `build/X`\n \"main\": \"build/cjs/index.cjs\",\n \"module\": \"build/esm/index.mjs\",\n \"types\": \"build/types/index.d.cts\",\n // note: this is an opportunity for us to explore multiple subpaths, like\n // exporting from `@sentry/browser/utils`\n \"exports\": {\n \"./package.json\": \"./package.json\",\n \".\": {\n // esm\n \"import\": {\n // path must be relative\n \"types\": \"./build/types/index.d.mts\",\n \"default\": \"./build/esm/index.mjs\"\n },\n // cjs\n \"require\": {\n // path must be relative\n \"types\": \"./build/types/index.d.cts\",\n \"default\": \"./build/esm/index.mjs\"\n }\n // we might need to expose an ESM only export for customization hooks\n // to use via node --loader=... API\n }\n },\n \"typesVersions\": {\n \"\u003c4.9\": {\n \"build/types/index.d.cts\": [\n \"build/types-ts3.8/index.d.cts\"\n ],\n \"build/types/index.d.mts\": [\n \"build/types-ts3.8/index.d.mts\"\n ]\n }\n },\n \"files\": [\n \"cjs\",\n \"esm\",\n \"types\",\n \"types-ts3.8\"\n ]\n}\n```\n\nShameless promo: [Watch my talk](https://www.youtube.com/watch?v=fpTa6oZR0ms) if you want more details about this.\n\nWe also should change all node standard library imports to import from `node:X`.\n\n## Make `dynamicRequire`/`loadModule` work with ESM import\n\nThis exists to trick webpack, but we use it all over the codebase. We need to add an esm compatible way.\n\nOne thing we can do is introduce an async `dynamicRequire` that simulates `await import`, and then build time replace the functionality of `dynamicRequire` to use `import` or `require` under the hood?\n\n- `packages/node/src/integrations/anr/index.ts` - to conditionally import `worker_threads`. This is just because of Node 12 support, we’ll remove this import entirely\n- `packages/node/src/integrations/local-variables/local-variables-async.ts` - to conditionally import `node:inspector/promises`.\n- All of our database integrations - these are all going away\n- `packages/utils/src/time.ts` - this needs to be refactored entirely. We still want an isomorphic solution because we need date-time helpers that work across core/browser/node and all other packages, but what should happen is that we look for the `globalThis.performance` instead of relying on `perf_hooks`. This means that if the global performance API is not available we will fall back to plain `[Date.now](http://Date.now)` and similar helpers (basically only Node 14 for us). We also need to add a smarter timeOrigin calculation that reset’s itself to help alleviate issues with drifting monotonic clock. See https://github.com/open-telemetry/opentelemetry-js/issues/3279#issuecomment-1320064182\n\n","author":{"url":"https://github.com/AbhiPrasad","@type":"Person","name":"AbhiPrasad"},"datePublished":"2024-01-04T03:30:21Z","url":"https://github.com/getsentry/sentry-javascript/issues/10046"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:cda5a4b9-e0eb-9af8-305a-559d2c2c3b0a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 917C:1EE375:6F6D76:9C69A9:6B042057 |
| html-safe-nonce | 9a8dd6ab57cb81e56135ba99da9218d0e65e15bcc0746a1c9c76901a02b2965e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MTdDOjFFRTM3NTo2RjZENzY6OUM2OUE5OjZCMDQyMDU3IiwidmlzaXRvcl9pZCI6Ijg4NDY1MTAxODQ4MDIyOTU4OTUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 5d13a062a76894fd02843fdc765be6a92d134e7fab135fd4827b0783a8b784a7 |
| hovercard-subject-tag | issue:2064936029 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/getsentry/sentry-javascript/10046/issue_layout |
| twitter:image | https://opengraph.githubassets.com/55da47acb87d95c6b463592d3a4432d53aa055c136338efdbb63326d82a34298/getsentry/sentry-javascript/issues/10046 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/55da47acb87d95c6b463592d3a4432d53aa055c136338efdbb63326d82a34298/getsentry/sentry-javascript/issues/10046 |
| og:image:alt | We’re going to be adding proper support for ESM in the Sentry repo. Requirement: There is no require in ESM bundles at all ESM files have .mjs file extension CJS files have .cjs file extension ### ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | AbhiPrasad |
| hostname | github.com |
| expected-hostname | github.com |
| None | f8d9f404447ab5df341c8a92cf5fc927730ad1321e64ac57656444d2438097f1 |
| turbo-cache-control | no-preview |
| go-import | github.com/getsentry/sentry-javascript git https://github.com/getsentry/sentry-javascript.git |
| octolytics-dimension-user_id | 1396951 |
| octolytics-dimension-user_login | getsentry |
| octolytics-dimension-repository_id | 3328572 |
| octolytics-dimension-repository_nwo | getsentry/sentry-javascript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 3328572 |
| octolytics-dimension-repository_network_root_nwo | getsentry/sentry-javascript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | f7edccce04851591ee529948701779ce6819bc53 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width