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

210
Visualizações
using es6 module exports in browser from HTML script

I am converting old javascript code to es6 modeules and am finding many issues because my old technique was to dynamically generate a lot of HTML tags which included hard-coded event handlers in the tags like this:

const html = '<input type="text" onclick="doThisOnClick();"/>';
getElementFromID('hostId').innerHTML = html;

Those handlers are in es6 modules and get cut out because there is no direct use of them in any scripts at load time nor are they called by other module code. The only way to fix this I have found is to redesign my code to use dynamic event handler injection after generating the HTML tags. This must be done asynchronouosly via a setTimeout() call. I have been trying to discover other ways to make es6 modules accessible to potentially generated HTML code. Here is a specific issue I am having:

<script type="module">
  import { deepFunctionUsedByPotentialEventHandler } from './bar.js';
</script>

The website is being hosted by nodejs:dev-server locally. I get this error from chrome:

htmlFile.html:17 Uncaught SyntaxError: 
The requested module './bar.js' does not provide an export named 
'deepFunctionUsedByPotentialEventHandler ' (at htmlFile.html:17:10)

But the function is clearly exported in bar.js and is defined in bar.js:

bar.js...
function deepFunctionUsedByPotentialEventHandler() {
  ...
}

export { deepFunctionUsedByPotentialEventHandler, foo, bar };

I suspect that bacause there is no path to deepFunctionUsedByPotentialEventHandler at load time that the code has been stripped from the module for faster loading. This is NOT using webpack or any other building type tool.

I would prefer to keep the global scope clean by not using window.deepFunctionUsedByPotentialEventHandler.

Seems to me that the ES6 spec should have a way to force keeping code available like:

export { foo keep, bar, bob }

I have tried a host of ideas but so far I have not found a quick-easy way to convert this code.

about 4 years ago · Juan Pablo Isaza
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