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

286
Visualizações
Load bootstrap JS with esbuild

I'm trying to import the bootstrap JS in my esbuild build:

// index.js
import 'jquery-import'
import 'bootstrap'

// jquery-import.js
import jquery from 'jquery'
window.jQuery = window.$ = jquery;

The build target is esm. The problem is that the following bootstrap code is run:

factory(exports, require('jquery'), require('popper.js'));

This does not use my global window.jQuery but rather requires its own, which means that my global window.$.fn doesn't include the Bootstrap extensions. Also, I think that this makes jQuery load twice.

Is there a way of fixing this without adapting the bootstrap source code?


A little more background on this:

The bootstrap source code begins with the following statements (lightly adapted for better readability):

(function (global, factory) {
  if (typeof exports === 'object' && typeof module !== 'undefined') {
    // Option 1: The route taken by esbuild
    factory(exports, require('jquery'), require('popper.js'));
  } else if (typeof define === 'function' && define.amd) {
    // Option 2
    define(['exports', 'jquery', 'popper.js'], factory);
  } else {
    // Option 2
    (global = global || self, factory(global.bootstrap = {}, global.jQuery, global.Popper));
  }
}(this, function (exports, $, Popper) { 'use strict';

I have tried commenting out individual options, and neither 1, 2 or 3 work by just taking the already globally defined window.jQuery.

Many thanks for your help!

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

0

The reason it's not working, is for some reason your esbuild is trying to import a UMD file (typically used for old Node and browser hybrid code).

Having looked at bootstrap's package.json, it seems that "module": "dist/js/bootstrap.js", which exports bootstrap as modules, which is probably what esbuild should be referencing.

So to fix this without fixing the esbuild config, I'd conjecture you could try:

import 'bootstrap/dist/js/bootstrap'

...or to fix it with esbuild config, add this:

const buildOpts = {
  mainFields: ['module', 'main'],
}

This setting tells the bundler to prioritises module, then main entrypoints.

Normally this is module before main, but it may be the other way round because you set platform to "node".

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