Skip to content

Bootstrap and Require.js? #2579

Description

@geuis

Is Bootstrap.js compatible with Require.js? If so, can a reference to configuration code be shared?

Activity

  1. BenjiZombie commented on Mar 14, 2012

    @BenjiZombie

    I've done this. Assuming you are also using jQuery v1.7.1:

    1. Create a "require-jquery.js" file by concatenating requireJS and jQuery. 
       In Windows, the command is:
        type require.js jquery.js > require-jquery.js
       In linux, replace "type" with "cat"
    
    2. You'll need to concatenate all the JS file of bootstrap into a single file as well. 
       Look into the bootstrap package for a minified version of bootstrap's javascript or 
       do it yourself with the command shown above and your favorite minimizer.
    
    3. Create a new JS file, let's call it "core.js":
        require.config({
            baseUrl: '/assets/js/',
            paths: {
                bootstrap: 'bootstrap', // Omitted the .js as requireJs will add it automagically
                // And so on for other frameworks/utils
            }
        });
    
        define(
            'core',
            [
                'jquery',
                'bootstrap',
            ],
            function ( $ ) {
                // jQuery and bootstrap have been loaded
                return $;
            }
        );
    
    4. Put all your js files in /assets/js
    
    5. Remove all your script references in your index.html file, and add this one instead:
        <script data-main="/assets/js/core.js" src="assets/js/require-jquery.js"></script>
    

    BenjiZombie

  2. bkingcis commented on Feb 11, 2013

    @bkingcis

    I think the more appropriate way is to use the shim feature of requireJS. See stackoverflow article:
    http://stackoverflow.com/questions/13377373/shim-twitter-bootstrap-for-requirejs

    Also, you can use the Bootstrap CDN - http://www.bootstrapcdn.com/

  3. odahcam commented on Oct 2, 2017

    @odahcam

    How can this be done in BS4?

  4. youradds commented on Nov 14, 2017

    @youradds

    @odahcam ditto - looking into this myself as well (still playing with RequireJS for a re-design in BS4)

  5. odahcam commented on Nov 14, 2017

    @odahcam

    Well, I made the whole thing work, but I'm sure that I did not use the best approach.

  6. youradds commented on Nov 15, 2017

    @youradds

    I ended up getting it going using:

    requirejs.config({
        "baseUrl": "js/lib",
        "paths": {
          "jquery": "jquery",
          "app": "../app",
          "bootstrap": "bootstrap/js/bootstrap.bundle",
          "bootbox": "bootbox.min"
        },
        "shim": {
            "bootstrap": {
            	"deps": ["jquery"],
    			"exports": "$"
            	 },
            // 'underscore': {
            //     exports: '_'
            // },
            "main": { "deps": ["jquery","bootstrap"] }
        },
        map: {
        	// When bootbox requires jquery, give it bootstrap instead. This makes it so that
        	// bootstrap is **necessarily** loaded before bootbox.
        	bootbox: {
          		jquery: "bootstrap",
        	}
        }
    });
    
    require(['jquery','bootbox'], function($, bootbox){
    
        $(function(jquery) {
        	bootbox.alert("bla")
        });
    
    });
    

    Seems to do the trick :)

  7. odahcam commented on Nov 15, 2017

    @odahcam

    Looking this way, it wouldn't be more simple to just define bootstrap as a bootbox dependency in the shim object?

  8. youradds commented on Nov 16, 2017

    @youradds

    I'm not sure - I tried that and couldn't get it to work like that (ended up posting a question on StackOverflow)

    Cheers

    Andy

  9. odahcam commented on Nov 16, 2017

    @odahcam

    Could ya link the question here?

  10. Johann-S commented on Nov 16, 2017

    @Johann-S
    Member

    Bootstrap 4 will be fully compatible in our next beta (3) with require.js thanks to this PR : #24783 which convert Bootstrap dist files in UMD

  11. youradds commented on Nov 16, 2017

    @youradds
  12. odahcam commented on Mar 20, 2018

    @odahcam

    I'm using SystemJS instead of requireJS now. System works a lot better than require and it is easy to switch from one to another. 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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