Grunt & requirejs optimizer for a multi app project

落花浮王杯 提交于 2019-12-05 02:16:46

Solved, by passing multiple sets of options to the requirejs task, thanks to this article for the final pointers I needed:

module.exports = function(grunt) {
    var files = grunt.file.expand('static/js/apps/*.js');
    var requirejsOptions = {};

    files.forEach(function(file) {
        var filename = file.split('/').pop();
        requirejsOptions[filename] = {
            options: {
                baseUrl: 'static/js/',
                include: './apps/'+filename,
                out: 'static/js/build/'+filename
            }
        };
    });

    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        requirejs: requirejsOptions,
    });
};

Then the ['requirejs'] task can be run as normal and will output the appropriate compiled .js file as per each of the options: {} blocks that were specified in requirejsOptions, eg:

grunt.registerTask('default', ['requirejs']);

You need to change baseUrl to static/js from apps in requirejs build config. As currently baseUrl is pointing to apps directory, require is trying to search dependency files in apps directory. If you change the baseUrl to static/js it will find those dependency files.

   requirejs: {
     compile: {
       options: {
         appDir: 'static/js/',
         baseUrl: 'static/js',
         dir: 'static/js/build/',
         modules: [
          {
                name: 'app',
          }
         ]
       }
     }
   }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!