nvbn / django-bower

Easy way to use bower with your django project
https://django-bower.readthedocs.io/
518 stars 74 forks source link

jquery install example only works with link to dist/jquery.js #11

Open stuaxo opened 10 years ago

stuaxo commented 10 years ago

I installed jquery, but

/static/jquery/jquery.js did not work, however /static/jquery/dist/jquery.js did work - is this a configuration issue on my side ?

stuaxo commented 10 years ago

The same for angular-ui - I have to use angular-ui/build/angular-ui.js

It's a bit confusing, I have to check each component to find it's files.

stuaxo commented 10 years ago

I'm new to bower, but just wondering if the finder should be able to use the 'main' component of bower.json ?

In the jquery case, it points to dist/jquery.js

So, the finder should possibly be able to return it if you do jquery/jquery.js ?

gsong commented 10 years ago

@stuaxo main could be an array such as is the case with Bootstrap. How would you propose that the finder behaves then?

stuaxo commented 10 years ago

Ah, interesting .. I'm very new to bower .. (as of 3 days ago in fact).

How about template tags instead ?

{% bower_scripts %}

To find out if something is javascript, python mimetypes could be used at installation time:

import mimetypes
mimetypes.guess_type("bootstap.js")
('application/javascript', None)

{% bower_stylesheets %}

Could do the same for stylesheets.

These could have options to limit them to certain bower apps

{% bower_scripts "angularjs", "ng-grid" %}

(I'm not entirely sure if that notation will work for templates, but basically some way of passing the script names).

gsong commented 10 years ago

I don't think an automatic way of discovery is the way to go, since there's no guarantee how each bower package structures its code and artifacts. I think the best you can hope for is to manually add your own mapping to alias the package structure, something like:

BOWER_COMPONENTS_MAPPING = {
    'jquery': 'jquery/dist',
    'angular': 'angular-ui/build',
    'bootstrap': 'bootstrap-sass-official/vendor/assets',
}

I would hook that into the behavior for djangobower.finders.BowerFinder (or your own custom finder) for it to know how to untangle the aliases, or fall back to searching within os.path.join(BOWER_COMPONENTS_ROOT, bower_components).

For certain purposes, template tags are too late, such as django-pipeline.

Your other options are:

  1. Add those paths to your STATICFILES_DIRS:

    STATICFILES_DIRS = (
       os.path.join(BASE_DIR, 'my_project/assets'),
       os.path.join(BOWER_COMPONENTS_ROOT, 'bower_components/jquery/dist'),
       os.path.join(BOWER_COMPONENTS_ROOT, 'bower_components/angular-ui/build'),
       os.path.join(BOOTSTRAP_ROOT, 'bower_components/bootstrap-sass-official/vendor/assets'),
    )

    But you'll have to be pretty sure that the packages don't collide with each other in terms of their component names.

  2. Use something like Vendorer, which gives you the chance to manipulate the directory structure however you want (I haven't tested this code, but you get the idea).

    file 'build/jquery.zip',
        'https://github.com/jquery/jquery/archive/2.1.0.zip' do |fp|
     build_dir = File.dirname(fp)
     unzipped_dir = File.join(build_dir, 'jquery-2.1.0')
     static_dir = 'bower_components/jquery'
    
     system "cd #{build_dir} && unzip -o #{File.basename(fp)}"
     system "rm -rf #{static_dir}"
     system "mkdir -p #{static_dir}"
     system "cp -r #{unzipped_dir}/dist/* #{static_dir}"
    end
stuaxo commented 10 years ago

Ahh.. yes, I didn't think about the fact that something listed in main could call resources not listed.

Once I've added more bower dependencies to my project I might have a go at auto discovery and see if I can make it work.

gsong commented 10 years ago

Good luck! ✌️

On Mar 7, 2014, at 6:26 AM, Stuart Axon notifications@github.com wrote:

Ahh.. yes, I didn't think about the fact that something listed in main could call resources not listed.

Once I've added more bower dependencies to my project I might have a go at auto discovery and see if I can make it work.

— Reply to this email directly or view it on GitHub.

MHellmund commented 10 years ago

Please change the documentation of django-bower and replace the path in the example line < script type="text/javascript" src='{% static 'jquery/jquery.js' %}'> with 'jquery/dist/jquery.min.js'

It may avoid some initial frustration with this great package and everyone understands that one has to look for the exact path inside bower_components.