Is it possible to require JS files from bower_components, saying "require (" library ")"?

I am new to RequireJS and it seems that this is actually not possible, but I will continue anyway and ask to skip if I missed something.

The docs say:

This setting assumes that you save all your JavaScript files in the scripts directory in your project.

  • Project Catalog /
    • project.html
    • Scenarios /
      • main.js
      • assistant /
        • util.js

But what if I need to require files from my installed file bower in bower_components:

  • Project Catalog /
    • bower_components /
      • JQuery-MouseWheel
        • jquery.mousewheel.js
      • lodash
        • distance
          • lodash.js

As you can see, not all libraries have the same directory hierarchy and naming convention.

So, I was wondering if there is an easy way to require these bower libraries without knowing where their main files are located, maybe just saying

require('jquery-mousewheel'); require('loadash'); 

?

+6
source share
2 answers

configure requirejs configuration to use paths

 requirejs.config({ // ... config ... paths: { jquery-mousewheel: 'bower_components/jquery-mousewheel/jquery.mousewheel', loadash: 'bower_components/lodash/dist/lodash' } // ... config ... }); 

for reference

+6
source

I think this is the best solution ...

 requirejs.config({ baseUrl: 'bower_components/', paths: { // path to your app app: '../' } }); requirejs( [ 'imagesloaded/imagesloaded', 'app/my-component.js' ], function( imagesLoaded, myComp ) { imagesLoaded( '#container', function() { ... }); }); 
+3
source

Source: https://habr.com/ru/post/974310/


All Articles