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

some code disappeared when build project #7541

Closed
7 tasks done
Dedicatus546 opened this issue Mar 31, 2022 · 4 comments
Closed
7 tasks done

some code disappeared when build project #7541

Dedicatus546 opened this issue Mar 31, 2022 · 4 comments
Labels
bug: upstream Bug in a dependency of Vite p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@Dedicatus546
Copy link

Describe the bug

i use @elastic/apm-rum-vue , and use its export function ApmVuePlugin

import { createApp } from 'vue'
import App from './App.vue'

import { ApmVuePlugin } from "@elastic/apm-rum-vue";

const app = createApp(App);
app.use(ApmVuePlugin);

app.mount('#app')

and then i build project

but the function patchEventTarget in file named event-target-patch.js is not build into the dist file.

source file in @elastic/apm-rum-core/dist/es/common/patching/index.js

function patchAll() {
  if (!alreadyPatched) {
    alreadyPatched = true;
    patchXMLHttpRequest(function (event, task) {
      patchEventHandler.send(XMLHTTPREQUEST, [event, task]);
    });
    patchFetch(function (event, task) {
      patchEventHandler.send(FETCH, [event, task]);
    });
    patchHistory(function (event, task) {
      patchEventHandler.send(HISTORY, [event, task]);
    });
    patchEventTarget(function (event, task) {
      patchEventHandler.send(EVENT_TARGET, [event, task]);
    });
  }

  return patchEventHandler;
}

dist file index.xxxxxx.js (build.minify = false)

function patchAll() {
  if (!alreadyPatched) {
    alreadyPatched = true;
    patchXMLHttpRequest(function(event, task) {
      patchEventHandler.send(XMLHTTPREQUEST, [event, task]);
    });
    patchFetch(function(event, task) {
      patchEventHandler.send(FETCH, [event, task]);
    });
    patchHistory(function(event, task) {
      patchEventHandler.send(HISTORY, [event, task]);
    });
    // patchEventTarget disappeared ...
  }
  return patchEventHandler;
}

but when i add a console.log() , build it again and function patchEventTarget is exist in dist file...

add console.log in @elastic/apm-rum-core/dist/es/common/patching/index.js

function patchAll() {
  if (!alreadyPatched) {
    alreadyPatched = true;
    patchXMLHttpRequest(function (event, task) {
      patchEventHandler.send(XMLHTTPREQUEST, [event, task]);
    });
    patchFetch(function (event, task) {
      patchEventHandler.send(FETCH, [event, task]);
    });
    patchHistory(function (event, task) {
      patchEventHandler.send(HISTORY, [event, task]);
    });
    // add a console to read patchEventTarget
    console.log(patchEventTarget);
    patchEventTarget(function (event, task) {
      patchEventHandler.send(EVENT_TARGET, [event, task]);
    });
  }

  return patchEventHandler;
}

or add console.log in @elastic/apm-rum-core/dist/es/common/patching/event-target-patch.js

export function patchEventTarget(callback) {
  if (!window.EventTarget) {
    return;
  }
  
  // add a console
  console.log('hello');

  var proto = window.EventTarget.prototype;
  var nativeAddEventListener = proto[ADD_EVENT_LISTENER_STR];
  var nativeRemoveEventListener = proto[REMOVE_EVENT_LISTENER_STR];

  // other code 
}

last i set the build.rollupOptions.treeshake = false to solve problem.

it seems that function patchEventTarget is regarded as something which can be treeshake...

Reproduction

https://github.com/Dedicatus546/vite-build-lack-code-demo

System Info

System:
    OS: Windows 10 10.0.22581
    CPU: (8) x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
    Memory: 6.26 GB / 15.92 GB
  Binaries:
    Node: 16.13.1 - D:\nodejs\node.EXE
    npm: 8.1.2 - D:\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.22581.100.0), Chromium (99.0.1150.55)
    Internet Explorer: 11.0.22581.1
  npmPackages:
    @vitejs/plugin-vue: ^2.2.2 => 2.3.1
    vite: ^2.8.4 => 2.9.0

Used Package Manager

pnpm

Logs

No response

Validations

@sapphi-red
Copy link
Member

sapphi-red commented Apr 13, 2022

https://stackblitz.com/edit/node-e5wfch?file=src/main.js
It was reproducible without vite. Looks like it is a bug of rollup or the library. (not sure which)

Maybe something around sideEffect: false is the reason?
https://github.com/elastic/apm-agent-rum-js/blob/41654e4b835e599343ef00fef725980095e03948/packages/rum-core/package.json#L9

No it was not. I removed sideEffect: false and tried but it did not change.
Now I feel it is a bug around rollup.

@sapphi-red sapphi-red added the bug: upstream Bug in a dependency of Vite label Apr 13, 2022
@sapphi-red sapphi-red added pending triage bug: upstream Bug in a dependency of Vite p3-minor-bug An edge case that only affects very specific usage (priority) and removed bug: upstream Bug in a dependency of Vite pending triage labels Apr 13, 2022
@sapphi-red
Copy link
Member

upstream issue: rollup/rollup#4468

@Dedicatus546
Copy link
Author

rollup fix this bug, please up rollup version to 2.72.0.

@sapphi-red
Copy link
Member

Closing as it is updated by #8074. It will be released as 3.0.0-beta.0 in future.

@github-actions github-actions bot locked and limited conversation to collaborators May 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug: upstream Bug in a dependency of Vite p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

No branches or pull requests

2 participants