Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

129
Vistas
Ignore or replace dependencies' imports when bundling with Rollup

I'm bundling a JS library using Rollup. This lib has a dependency on @tensorflow/tfjs-core.

On tfjs's code, there's a function that fetches a URL. If it's in the browser environment, it uses the global fetch function; if it's not, it tries to import node-fetch.

Something among these lines:

fetch(path: string, requestInits?: RequestInit): Promise<Response> {
  if (env().global.fetch != null) {
    return env().global.fetch(path, requestInits);
  }

  if (systemFetch == null) {
    systemFetch = require('node-fetch');
  }

  return systemFetch(path, requestInits);
}

My library is made to run in the browser, so it always uses the global fetch function. However, Rollup still bundles node-fetch's require in my lib's assets.

It should not be an issue, but some consumers are reporting errors when using the library in a React project that uses webpack:

Failed to compile.

./node_modules/[my lib]/index.js Cannot find module: 'node-fetch'. Make sure this package is installed.

You can install this package by running: npm install node-fetch.

Question is: is there some way I can tell Rollup not no bundle this?

I thought about replacing the require('node-fetch') by undefined after the bundle is generated, but it feels like a dirty hack. Any other sugestions?

PS: I believe marking node-fetch as external on consumer projects would fix the issue, but since I do not use node-fetch in my lib, it would be nice to remove it from final output.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Other package managers can include or exclude files based on the environment, test, development, production, etc.

There is any number of ways of implementing this, even going so far as

# Makefile

ENVIRONMENT ?= test

ROLLUP = $(which rollup)

ENVSUBST = $(which envsubst)

rollup.config.js: src/$(ENVIRONMENT)
    ${ENVSUBST} < $@ > $^
    ${ROLLUP} $^ -o $(ENVIRONMENT).js

If you created files named after your environments, you could compile them using

make -e environment=browser

I don't expect my code to work, only to express ideas.

about 4 years ago · Juan Pablo Isaza Denunciar

0

There is this loc which is used to exclude node-fetch from the bundle. You could consider a similar approach in your rollup configuration. (I think) If you add that, node-fetch will/should not be a part of your minified library.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda