Skip to content

Bootstrap's modal.js + utile.js import #338

@CyrilKrylatov

Description

@CyrilKrylatov

Hello team,

first of all, I want to thank you for having webpack-encore created. Webpack was a big issue for me but with Encore, everything seems to be easy to use. Very good job to y'all. Thank you again.

However, it seems like I have a trouble with Bootstrap's modal.js, its utile.js he is using and compilation with Webpack.

I'll try to make myself as clear as possible. Webpack and JavaScript are not exactly my thing and English is not my mother tongue, so, please, do tell me if you need any clarification or something.

In a first place, I tried to follow this tutorial about Bootstrap x Encore. But bootstrap-sass is no longer supported for bootstrap v4 as you can see in their readme and I'm working with bootstrap 4.1.1 ("bootstrap": "^4.1.1", from my package.json).

So, my webpack.config.js looks like this:

const Encore = require('@symfony/webpack-encore');
Encore

   .addEntry(
        'base', [
            
            'bootstrap/js/dist/util.js',
            'bootstrap/js/dist/modal.js',
            
        ]
    )

;

It compils successfully as we can see here:

Running webpack ...

 DONE  Compiled successfully in 6215ms                                    14:41:09

 I  34 files written to web/built
✨  Done in 7.46s.

But when I try to open a modal on my website, I have this error: Uncaught ReferenceError: Util is not defined.

So I assume that the Util global variable that bootstrap is using is missing, since Webpack create functions around file it is importing and all.

So I added the following code in my webpack.conf.js:

    .autoProvideVariables({
        Util: 'bootstrap/js/dist/util.js',
    });

It works well for Popper (required to by Bootstrap) but not with Util since I have the same Uncaught ReferenceError: Util is not defined.

Soooo, after a few searches on GitHub and all, I found this comment.

I removed Util from both autoProvideVariables() and my addEntry(), modal.js as well to put them both in a bootstrap.js file:

const $ = require('jquery');
import Util from 'bootstrap/js/src/util'
import Modal from 'bootstrap/js/src/modal'

and added an entry for it in webpack.config.js:

    .addEntry('bootstrap', './assets/js/webpack/bootstrap.js')

And now, I have a error compilation:

 ERROR  Failed to compile with 1 errors                                   14:48:39

 error  in ./node_modules/bootstrap/js/src/modal.js

Module parse failed: Unexpected token (222:8)
You may need an appropriate loader to handle this file type.
|     _getConfig(config) {
|       config = {
|         ...Default,
|         ...config
|       }

 @ ./assets/js/webpack/bootstrap.js 3:0-43

So I tried to add babel following this documentation since it seems like it don't like the spread operator and it's not going well:

Running webpack ...

Error: configureBabel() cannot be called because your app already has Babel conf  iguration (a `.babelrc` file, `.babelrc.js` file or `babel` key in `package.json  `). Either put all of your Babel configuration in that file, or delete it and us  e this function.

So here I am, opening an issue, asking for help.

I don't know what can I do more. If anyone had the same issue I'd be glad to hear for a solution.

Thank you for your time.

Edit I tried this one but I still have a Uncaught TypeError: Util.getSelectorFromElement is not a function when I try to open my modal.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions