4
votes

I'm working on a project where the main code based should be used by a bunch of different client. So we have a requirejs project and my initial idea is to have simple bootstrap.js file that will require an app.js files that is different for every client.

bootstrap.js

requirejs(['app'],function(app){
  //some initial code here
  app.start();
}

So the project structure will be look like this:

|_bootstrap.js
|_commonModules
  |_someModule.js
|_client1
  |_app.js
  |_modules
    |_module.js
|_client2
  |_app.js
  |_modules
    |_module.js

So my ideas is to compile the app for every client using requirejs' r compiler and set the path to app in every compilation to clientX/app.js by crearting a new build.js for every step like this:

({    
  paths: {
    "app": "client1/app"
  }
}) 

So at the moment I have a grunt build task that is using a bunch of other tasks like uglify, usemin, md5 and so on. Can I create a new task that use this task but changing the requireJs settings for every client? Or is there a better way to achieve my goals?

1

1 Answers

6
votes

So after all it wasn't that hard. The cool thing is that you can change the configuration for the actual running task and that you can call previous defined task in a running task.

//this was the old task to build one distribution
grunt.registerTask('build', ['clean:build', 'copy:build', 'useminPrepare', 'usemin', 'requirejs', 'concat', 'uglify', 'mincss', 'md5', 'manifest', 'copy:toClientFolder']); 

grunt.registerTask('buildAll', function() {
  ['client1', 'client2'].forEach(function(client) {
    //before every build task run a task to change the config
    grunt.task.run('updateConfig:' + client, 'build');
  });
});

  //we need to change the config in a separate task,
  //otherwise, change the config just in the forEach, would result in the same 
  //config for both task, using the client2 settings
  grunt.registerTask('updateConfig', function(client) {
    var requireJsName = 'requirejs.compile.options.name';
    var clientFolder = 'copy.toClientFolder.files';
    grunt.config(requireJsName, 'clients/' + client + '/Bootstrap');
    grunt.config(clientFolder, [
      {expand: true, cwd: 'dist', src: '**', dest: 'dist_' + client}
    ]);
  });

And a app.js file for a client looks like this:

requirejs.config({
  paths: {
    'commonModules/someModule': 'clients1/modules/module'
  }
});

requirejs(['boootstrap',
  'commonModules/someModule1'],

  function(boootstrap, someModule1) {
    $(function() {
      boootstrap();
      someModule1();
    });
  });