elastic / apm-agent-rum-js

https://www.elastic.co/guide/en/apm/agent/rum-js/current/index.html
MIT License
278 stars 133 forks source link

@elastic/apm-rum causes bundling issue with @web/dev-server #1486

Closed gabrielslach closed 6 months ago

gabrielslach commented 6 months ago

I installed the rum package with npm. I run my app locally with @web/dev-server. Now, when importing this library, I get the following error.

image

This is my config.

image

But now when I add these, it just produce additional errors.

image
devcorpio commented 6 months ago

Hi @gabrielslach,

Thanks for exposing your case.

We are not aware of the web-dev-server specifics. Despite so, I would like to provide you with a workaround that I tested personally, this is the content of my web-dev-server.config.mjs file:

import { fromRollup } from '@web/dev-server-rollup';
import rollupCommonjs from '@rollup/plugin-commonjs';
import rollupReplace from '@rollup/plugin-replace';

const commonjs = fromRollup(rollupCommonjs);
const replace = fromRollup(rollupReplace);

export default {
  plugins: [
    replace({
      "process.env.NODE_ENV": "'development'",
      "import { createTracer":  "var createTracer = function (){ /* noop */} //",
      preventAssignment: false
    }),
    commonjs(),
  ],
};
Screenshot 2024-03-13 at 13 53 18

noop.ts belongs to opentracing which is not maintained anymore for us, so you can get rid of it like I'm doing in the snippet:

import { createTracer":  "var createTracer = function (){ /* noop */} //

Not sure about your context specifics, maybe you will want to add the process.env.NODE_ENV thingy as well.

But at least with that config file setup, RUM works, the screenshot below corresponds to a beacon:

Screenshot 2024-03-13 at 13 51 51

Cheers, Alberto

gabrielslach commented 6 months ago

Thank you @devcorpio, I just realized that I provided the wrong snippet of my config. Nevertheless, your fix works! This fix is conflicting with the web-mocks. (@web/mocks/browser.js) So I had to limit the replace action to @elastic**.

image