Using AMDclean with grunt

630 Views Asked by At

I am trying to build a project with requirejs and AMDClean to completely remove the need for amd in the production version. Although I am able to get the requirejs optimizer to put my files together I am not able to remove require/define from it using amdclean. Here is my requirejs code (written in grunt using coffeescript):

requirejs:
      compile:
        options:
          baseUrl: './client'
          mainConfigFile: "client/main.js"
          name: 'main'
          out: 'build/main.js'

Here client/main.js is the entry point to my application.I can call this file using the script tag:

<script src="client/vendor/require.js" data-main="build/main.js"></script>

Here is the requirejs task for grunt with amdclean

fs = require 'fs'
amdclean = require 'amdclean'

module.exports = (grunt) ->
  'use strict'
  grunt.initConfig(

    #Read Package.json
    pkg: grunt.file.readJSON('package.json')

    #Build task
    requirejs:
      compile:
        options:
          baseUrl: './client'
          mainConfigFile: "client/main.js"
          name: 'main'
          out: 'build/main.js'
          onModuleBundleComplete: (data) ->
            outputFile = data.path
            fs.writeFileSync(outputFile, amdclean.clean(
              'filePath': 'client/main.js'
              'globalModules': []
              'wrap':
                'start': '(function(){\n',
                'end': '\n}());'
            ))

I expect to be able to call this file using this tag:

<script src="build/main.js"></script>

Which is not happening. It throws an error saying: models_MenuItem is not defined which is the error I usually get when the modules are not loaded in the right order. The file built with requirejs optimizer and the original pre-build javascript work normally. Please help me correct my amdclean configuration or identify where the problem really is.

1

There are 1 best solutions below

0
On

Make sure to set your filePath AMDclean option correctly:

'filePath': outputFile

There could also be a couple of other reasons:

Make sure to set the Require.js skipModuleInsertion option to true. By default, Require.js creates a define() wrapper for files that are not wrapped in a define(). This can cause issues with AMDclean.

Make sure you are not pointing to minified files when building with AMDclean. This will definitely cause issues.