使用 esbuild 加载 bootstrap JS

发布于 2025-01-13 04:01:58 字数 1426 浏览 2 评论 0原文

我正在尝试导入 bootstrap JS 在我的 esbuild 构建中:

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

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

构建目标是 esm。问题是运行以下引导代码:

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

这不使用我的全局 window.jQuery 而是需要它自己的,这意味着我的全局 window.$.fn不包括 Bootstrap 扩展。另外,我认为这会使 jQuery 加载两次。

有没有办法在不调整引导源代码的情况下解决这个问题?


更多背景知识:

引导程序源代码以以下语句开头(为了更好的可读性而稍微调整):

(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';

我尝试注释掉各个选项,并且 1、2 或 3 都不能通过仅采用已经全局定义的 来工作window.jQuery

非常感谢您的帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玻璃人 2025-01-20 04:01:58

它不起作用的原因是由于某种原因您的 esbuild 正在尝试导入 UMD 文件(通常用于旧的 Node 和浏览器混合代码)。

查看 bootstrap 的 package.json 后,似乎 "module": "dist/js/bootstrap.js" 将 bootstrap 导出为模块,这可能是 esbuild 应该引用的内容。

因此,要在不修复 esbuild 配置的情况下修复此问题,我猜想您可以尝试:

import 'bootstrap/dist/js/bootstrap'

...或者使用 esbuild 配置修复它,添加以下内容:

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

此设置告诉捆绑器优先考虑模块,然后是主要入口点。

通常这是 main 之前的模块,但也可能相反,因为您设置了 platform到“节点”。

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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文