Randy Brown
Randy Brown

Reputation: 121

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?

Upvotes: 5

Views: 1843

Answers (2)

becon
becon

Reputation: 46

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.

Upvotes: 1

ncksllvn
ncksllvn

Reputation: 5769

For others having the same problem, a quick but only partial fix is to disable the script injection by removing the following from layout.ejs:

<!-- SCRIPTS -->

<!-- SCRIPTS END -->

Then just place direct links to your require.js file:

<script src="/linker/js/components/requirejs/require.js"></script>

I say this is only a partial fix because the GruntFile will need need to implement a require task in order to concatenate the files correctly.

Upvotes: 0

Related Questions