New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support native ES6 module loading #31944
Comments
Popper 1.6 does support ES modules |
Any PRs to improve things are welcome. It's been a while since we set up ESM in v5 and things have progressed since then. |
I do not see this working with popper in the near future. The best move preventing any confusion would be to adjust the docs to explain that this only works when using a bundler like webpack or similar. The problem is that popper (even though it supports es modules) is not able to be resolved by the browser. The spec currently only covers relative references like The only way I know to fix this would be to use a module bundler which correctly resolves these modules, generate code which uses a relative reference (and therefore locks the location of the dependency in place) or just creating a module with all components except Popper. The first is what I propose we mention in the docs while the last one might be something we could consider for v5.1 or similar although I do not see a big benefit as bundler should solve this IIRC. |
How to use Bootstrap as a module in Angular Project? |
I'm confused; isn't this supposed to be the EJS module? Why are there non-EJS things in the EJS-only module? Why has someone/something been compiling non-EJS compliant code into a specifically EJS-compliant CDN link for the past two years? ??? Can someone advise me? |
This isn't an issue specific to Bootstrap - in order to distribute a library with external dependencies, some indirection around resolution is clearly needed (or otherwise hardcoded CDN URIs) Native ES6 module loading in the browser does work, if you use an importmap (... by native, I mean you'll need a shim for Firefox for a while.
The above works in Chrome without the es-module-shims script, and in both Chrome and Firefox as is. |
@earcam could you open a PR to add this in docs please? |
Doc change for twbs#31944
* Docs: dependencies mgmt for vanilla ESM in browser Doc change for #31944 * Update javascript.md * Update javascript.md * Update .cspell.json * Update javascript.md * Update javascript.md * rewrite * Add link * edit * eslint-skip Co-authored-by: Caspar MacRae <earcam@gmail.com> Co-authored-by: XhmikosR <xhmikosr@gmail.com>
Wouldn't it be much easier to import popper as a esm module rather than deal with import maps? |
Here's how I did it:
You can now |
According to v5.0 documentation Bootstrap provides a version of Bootstrap built as ESM (bootstrap.esm.js and bootstrap.esm.min.js) which allows you to use Bootstrap as a module in your browser. It's not true if you want to imort bootstrap.esm.js module in vanilla Javascript (tested in 5.0.0-alpha2). In documentation we have example with such module import:
It wouldn't work in pure Javascript because so-called “bare” module specifiers are currently not supported in ECMAScript modules specification. Module specifiers must be full URLs, or relative URLs starting with
/
,./
, or../
. It's not a problem to set proper URL:Unfortunately such import raises browser exception Uncaught TypeError: Failed to resolve module specifier "popper.js". Relative references must start with either "/", "./", or "../"..
Changing
import Popper from 'popper.js'
in toimport Popper from './popper.js'
doesn't help. Such import raises another browser exception Uncaught SyntaxError: The requested module './popper.js' does not provide an export named 'default'.As far I find out Propper 1.16 doesn't provide native ES6 module. So for testing I commented out Propper import from bootstrap.esm.js source code:
// import Popper from 'popper.js';
And now native ES6 import system is working!
Demo page: http://localhost:5500/bootstrap-v5/bootstrap-v5.0.0-alpha2-esm-test/
I know removing Popper causes Dropdowns and Tooltips not working but for me it's not a problem. I think it would be great to have each component (Toast, Alert, Button, ...) in separate ESM module. Folks loving vanilla Javascript like me would be able to import classes that don't depend on Propper without modifying Bootstrap's source code.
I also think that writing in documentation that Bootstrap provides a version of Bootstrap built as ESM is misleading. It's rather UMD module.
Se also #28386 and #27119
The text was updated successfully, but these errors were encountered: