Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

159
Visualizações
Can I monkeypatch a function in another webpack bundle at runtime?

I am writing a plugin for a third-party web application, whose code I can see but can't modify. I'm running it in Chrome. The main webapp and the plugin are both (separate) webpack bundles. At runtime when the page loads, the webapp fetches the plugin bundles from the same server, and initialises them.

My objective is to make my plugin patch/wrap a function in the third-party application, in the module webapp/utils/target.tsx, such that calls to that function from within the webapp have my modified behaviour. Something like this:

// somehow import the `target` module (this is the problem, see below...)

oldFunc = target.targetFunc;
target.targetFunc = function targetFunc(args) {
    // do extra stuff here
    return oldFunc(args);
}

But I don't know how to import the target module or whether this is possible. Specifically:

  • I can't just import target, because application/webapp is not a dependency of my plugin
    • Plugins are meant to access limited entrypoints that get attached to window by the webapp, so they have no direct dependency on the webapp
  • I don't think I can add application/webapp as a dependency because
    1. it's not a published package (perhaps I can add it as a github link?) and
    2. I don't want webpack to include it in the bundle, so I think I'd have to specify it as an external dependency, but I don't know how to do that...
  • I can't modify application to do any extra things in its webpack (like exposing target in a different way)
  • I thought perhaps I could import it dynamically at runtime:
import(/*webpackIgnore: true*/ '/application/webapp/utils/target').then({
    ...
})

This gives me the error Expected a JavaScript module script but the server responded with a MIME type of "text/html".

If it helps, when the page is fully loaded and the app has loaded my plugin, in Chrome developer tools under Sources -> Page, I see a tree structure like this:

- localhost:port
- .
- com.mydomain.myplugin
  - <modules for my plugin>
- application
  - webapp
    - .
    - <other modules>
    - utils
        - target.tsx
        - <other files>
    - webpack

Meanwhile the original html page source seems to load the webapp via this tag in the header:

    <script defer="defer" src="/static/main.c4e2eaf1d8c47b01fa6c.js"></script>

The Chrome devtools say "Source map detected".

Is it even possible to do what I'm trying to do?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

From what i understand, you can import the module object, edit with the Object. library and return the edited module. If you don't understand nothing but knows the core, these references should helpful to solve this.

https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Statements/import
https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Statements/export
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperties

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda