Building with Grunt and Requirejs

313 Views Asked by At

I am creating a grunt task for building a javascript project with requirejs using grunt-contrib-requirejs

https://github.com/gruntjs/grunt-contrib-requirejs

Here is the config:

requirejs:
      compile:
        options:
          #appDir: './'
          baseUrl: "client"
          mainConfigFile: "client/main.js"
          name: "main"
          out: "build/main.js"
          wrap:
            start: ""
            end: ""

The main.js file requires 2 other files inside subdirectories. Althrough this task does not throw errors, the resulting built file does not run the browser. The files seem to be concatenated since the require calls still exist in the built file. I expect the js files called by require to substitute the require calls, and then be optimized. how can I achieve that?

PS: The config above is written in coffeescript.

1

There are 1 best solutions below

3
On BEST ANSWER

If you want your compiled javascript file to not contain and require() or define() calls you can use the AMDclean npm package and simple add this to your options object:

onModuleBundleComplete: function (data) {
  var fs = require('fs'),
    amdclean = require('amdclean'),
    outputFile = data.path;
  fs.writeFileSync(outputFile, amdclean.clean({
    'filePath': outputFile
  }));
}