Documentation error
While this is not the answer you want, it does seem that the correct answer is, "Bootstrap's documentation has an error."
For umd
output, if I import with this syntax:
import Dropdown from '../../node_modules/bootstrap/js/dist/dropdown';
the build fails with the following error:
Error: 'default' is not exported by node_modules\bootstrap\js\dist\dropdown.js, imported by src\js\bootstrap-build.js
Which is contrary to this: Files in bootstrap/js/dist use the default export,.
What does work (includes Popper):
To the reader, assuming you came here to find a solution to a build problem you are having, below is what works for me.
This answer assumes all dependencies were installed using NPM as a dev dependency except for Bootstrap 5, which was installed with this command:
npm install bootstrap
This is rollup.config.js:
import {terser} from 'rollup-plugin-terser';
export default {
input: './src/js/bootstrap-build.js',
output: {
file: 'dist/js/bootstrap.umd.min.js',
format: "umd",
name: "bsbundle", // this is the name of the global object
esModule: false,
sourcemap: true,
},
plugins: [terser({compress: {drop_console: true, module: true}})]
};
This is the project's entry script, i.e. bootstrap-build.js, located in <project root>/src/js/:
import * as Popper from '../../node_modules/@popperjs/core/dist/umd/popper.js';
// from 'js/src/*' source which works
import Modal from '../../node_modules/bootstrap/js/src/modal';
import Tab from '../../node_modules/bootstrap/js/src/tab';
import Dropdown from '../../node_modules/bootstrap/js/src/dropdown';
import Tooltip from '../../node_modules/bootstrap/js/src/tooltip';
export default {
// Alert,
// Button,
// Carousel,
// Collapse,
Dropdown,
Modal,
// Offcanvas,
// Popover,
// ScrollSpy,
Tab,
// Toast,
// Popper,
Tooltip
}
And the package.json:
{
"name": "bscomponents",
"version": "1.0.0",
"description": "Project for building Bootstrap 5 components.",
"main": "index.js",
"scripts": {
"build-bs": "rollup -c"
},
"keywords": [],
"author": "",
"license": "",
"dependencies": {
"bootstrap": "^5.1.3"
},
"devDependencies": {
"@rollup/plugin-multi-entry": "^4.1.0",
"autoprefixer": "^10.4.0",
"cross-env": "^7.0.3",
"postcss": "^8.3.11",
"rollup": "^2.60.0",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-terser": "^7.0.2",
}
}
NPM build command:
NPM run build-bs
Build note: The line importing Popper in the Bootstrap Dropdown and Tooltip source files is commented out.
Bundle includes Popper, Dropdown, Modal, Tab, and Tooltip
File index.html in project root for testing bundle.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bootstrap 5 components</title>
<!-- custom Bootstrap bundle saves about 21kb -->
<link rel="stylesheet" href="./node_modules/bootstrap/dist/css/bootstrap-reboot.min.css">
<link rel="stylesheet" href="./node_modules/bootstrap/dist/css/bootstrap.min.css">
<!-- and a few simple styles -->
<style>
body {
padding: 5rem;
margin: 1rem auto;
}
.btn-wrapper {
display: inline-block;
margin: 1rem auto;
}
.btn-wrapper li {
padding: 1rem
}
.tab-pane {
padding: 2rem;
}
ul {
margin: 3rem
}
</style>
</head>
<body>
<h1>Bootstrap 5 components</h1>
<ul>
<li>Dropdown</li>
<li>Modal (an option in the dropdown)</li>
<li>Tabs</li>
<li>Tooltip</li>
</ul>
<div class="btn-wrapper dropdown" data-bs-toggle="tooltip" data-bs-placement="top" title="This is a Bootstrap 5 dropdown.">
<button type="button" id="btnDropDown" class="btn btn-primary btn-calculator dropdown-toggle" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="true">Options <span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="btnDropDown">
<li data-bs-toggle="modal" data-bs-target="#MYMODAL">
<b>Modal</b>
</li>
<li>
Second option
</li>
<li>
And a final option
</li>
</ul>
</div>
<div>
<nav>
<div class="nav nav-tabs" id="nav-tab" role="tablist">
<button class="nav-link active" id="nav-tab-1" data-bs-toggle="tab" data-bs-target="#nav-calc-1" type="button" role="tab" aria-controls="nav-calc-1" aria-selected="true">Tab 1</button>
<button class="nav-link" id="nav-tab-2" data-bs-toggle="tab" data-bs-target="#nav-options-1" type="button" role="tab" aria-controls="nav-options-1" aria-selected="false">Tab 2</button>
</div>
</nav>
<!-- Tab panes -->
<div class="tab-content" id="nav-tabContent-1">
<div class="tab-pane fade show active" id="nav-calc-1" role="tabpanel" aria-labelledby="nav-tab-1">
<p>tab 1</p>
</div>
<!--role="tabpanel"-->
<div class="tab-pane fade show" id="nav-options-1" role="tabpanel" aria-labelledby="nav-tab-2">
<!-- option tab content -->
<p>tab 2</p>
</div>
<!-- role tabpanel -->
</div>
<!--end tab-content-->
</div>
<!-- modal -->
<div class="modal fade" tabindex="-1" id="MYMODAL">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<p>Before you leave, hover over the below buttons.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal" data-bs-toggle="tooltip" data-bs-placement="bottom" title="Click to close">Seconday</button>
<button type="button" class="btn btn-primary" data-bs-toggle="tooltip" data-bs-placement="right" title="I do nothing!">Primary</button>
</div>
</div>
</div>
</div>
<!-- end modal -->
<script src="./dist/js/bootstrap.umd.min.js"></script>
<script>
var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
return new bsbundle.Tooltip(tooltipTriggerEl)
})
</script>
</body>
</html>
Development environment:
- Windows 10
- Rollup v2.6
- Node v16.10.0
- NPM 8.1.3
- Bootstrap 5.1.3
Build run in VS Code v1.62's Bash terminal.
The result:
The build bundle including Popper
, <project root>/dist/js/bootstrap.umd.min.js, saves about 21kb over Bootstrap's distributed minified bundle.
Probable documentation issue reported on GitHub