How do I connect bower components with sails.js?

前端 未结 5 545
执念已碎
执念已碎 2020-12-07 12:28

I\'d like to be able to install Javascript dependencies through bower and use them in a sails.js app, but I can\'t figure out a way to do this with out just

5条回答
  •  萌比男神i
    2020-12-07 13:26

    In Sails 0.10 the 'assets/linker' directory no longer exists, however I found a lead on simple solution that gives some automation to the bower -> asset linker workflow while also allowing some granular control over what exactly ends up getting linked.

    The solution is adding grunt-bower to your Sails.js compileAssets task

    grunt.registerTask('compileAssets', [
        'clean:dev',
        'bower:dev',
        'jst:dev',
        'less:dev',
        'copy:dev',
        'coffee:dev'
    ]);
    

    Then configure your grunt-bower task like so (as tasks/config/bower.js):

    module.exports = function(grunt) {
      grunt.config.set('bower', {
        dev: {
            dest: '.tmp/public',
            js_dest: '.tmp/public/js',
            css_dest: '.tmp/public/styles'
        }
      });
    
      grunt.loadNpmTasks('grunt-bower');
    
    };
    

    This will automatically copy your bower js and css files to the proper place for Sail's asset linker to find and automatically add to your project's layout template. Any other js or css files will still automatically be added after your bower files.

    However this is still no silver bullet as this setup has 2 big caveats to it:

    1 - The files that are added through bower-grunt have to be listed in bower.json's main array. If you see a file isn't being loaded you would expect to be, you must either edit that packages bower.json file OR add the dependency manually through grunt-bower's packageSpecific options.

    2 - The order of bower files in the asset linker is currently alphabetical. My only recourse to adjust this order so far is tinkering around with an additional grunt task to manually re-order files prior to the rest of Sail's compileAssets task. However this one I'm confident there is something grunt-bower could do by supporting package copy ordering.

提交回复
热议问题