Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sails.js + RequireJS configuration

Having all kinds of problems getting Sails to work with RequireJS, mainly because I can't find any definitive source on the best way to do this. There are several posts out there that discuss this, but they are older and all do things differently. Would really love to see the Creators enlighten the community on the proper way to do this given the changes to the Sails application structure, linker process etc. in the latter versions (.0.9.9, ,0.10)

So, first question would be...if I am planning on using an AMD/RequireJS + Backbone approach for my client-side code, and want to use the R.js Optimizer in grunt to build my production JS file and resolve all the nested dependencies automatically (rather than have to list them out manually), should I not create the application with the --linker option and manually manage the grunt build process myself?

Also, where in the directory structure should the "vendor" directory be placed that contains all the dependent JS libs like Underscore, jQuery, Backbone etc. reside?

like image 954
Randy Brown Avatar asked Nov 10 '22 12:11

Randy Brown


1 Answers

I decided this problem:

  1. Install the plugin for grunt-requirejs

  2. wrote config to run build in a folder /tasks/config/requirejs.js

Example:

module.exports = function(grunt) {
  grunt.config.set('requirejs', {
    dev: {
        options: {
            baseUrl: "assets/",
            name: 'main',
            optimize: "uglify2",//'none',//"uglify2",
            //wrap: true,
            paths: {
                // Major libraries
                    jquery: '../vendor/jquery',
                    underscore: '../vendor/underscore',
                    backbone: '../vendor/backbone',
                    // Require.js plugins

                },
                removeCombined: true,
                inlineText: true,
                useStrict: true,
                out: "build/main.js",
                waitSeconds: 200
            },
        }
    });

  grunt.loadNpmTasks('grunt-contrib-requirejs');
};
  1. added to autostart in tasks/register/compileAssets.js

Example:

module.exports = function (grunt) {
    grunt.registerTask('compileAssets', [
        'clean:dev',
        'jst:dev',
        'less:dev',
        'copy:dev',
        'coffee:dev',
        'requirejs:dev'
    ]);
};

You also have to adjust just grunt at yourself and do not be afraid to change anything. At design time, better to store scripts in the Assets folder because it is convenient.

like image 178
becon Avatar answered Nov 30 '22 05:11

becon