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

96
Visualizações
How to be explicit about imports but keep prefix syntax?

I read here that import * as blah from 'blah' is not the best idea because it imports everything, which can bloat your JS files. I presume because Javascript isn't typed the unused functions are difficult to optimise away.

Firstly, is this true, or should I not worry about this as "webpack" (which I think is the thing that builds my React app) will just remove all the unused functions?

And secondly, if I really should avoid import * as blah from 'blah', how can I change this so that I only import the functions I need without refactoring all my code? I'd prefer to keep the blah.??? syntax, but import { alice, bob } as blah from 'blah' doesn't work and import { alice, bob } from 'blah' breaks all my code that was previously referring to blah.alice but now just be changed to be just alice only (I actually prefer the prefix syntax for this collection of functions).

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

0

The best thing I can think of is to make your own object and make sure to keep it in sync with the imports

// blah.js
const alice = () => "alice";
const bob = () => "bob";
const charlie = () => "charlie";

export { alice, bob, charlie };
// index.js
import { alice, bob } from "./blah";
const blah = { alice, bob };
// Even better, do this:
// const blah = Object.freeze({ alice, bob });
// to avoid accidentally rewriting the methods

console.log(blah.alice());
console.log(blah.bob());

You can see this working in a CodeSandbox here

This is definitely the simplest solution, though probably not the best. However, it does get the desired result.

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